Skip to content

Add vitest support to @temporalio/nyc-test-coverage #1690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/nyc-test-coverage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ worker = await Worker.create(workflowCoverage.augmentWorkerOptions({
afterAll(() => {
workflowCoverage.mergeIntoGlobalCoverage();
});
```
```

## Usage with vitest

This package works with [vitest](https://vitest.dev/) code coverage. Make sure you have configured vitest to use the
`instanbul` provider https://vitest.dev/guide/coverage.html#coverage-providers.
2 changes: 2 additions & 0 deletions packages/nyc-test-coverage/src/globalCoverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { CoverageMapData } from 'istanbul-lib-coverage';
declare global {
// eslint-disable-next-line no-var
var __coverage__: CoverageMapData;
// eslint-disable-next-line no-var
var __VITEST_COVERAGE__: CoverageMapData;
}

export {};
6 changes: 5 additions & 1 deletion packages/nyc-test-coverage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class WorkflowCoverage {
// Check if running through nyc or some other Istanbul-based tool.
// If not, any `workflowCoverage()` tools are a no-op.
private hasCoverageGlobal() {
return '__coverage__' in global;
return coverage() !== undefined;
}

/**
Expand Down Expand Up @@ -205,3 +205,7 @@ export class WorkflowCoverage {
global.__coverage__ = coverageMap.data;
}
}

function coverage() {
return global.__coverage__ ?? global.__VITEST_COVERAGE__;
}