Wrapper around v8's Profiler APIs used to gather coverage data for CircleCI's Smarter Testing Javascript plugins.
The coverage collector can be used to collect precise coverage during the test run lifecycles.
Add the package as a dependency
pnpm i jsr:@circleci/v8-coverage-collectorUse the provided APIs to collect and reset coverage during the test lifecycle events.
import {V8CoverageCollector} from '@circleci/v8-coverage-collector';
export default class TestHookExample {
private inspector: V8CoverageCollector;
constructor() {
this.inspector = new V8CoverageCollector();
}
async onBeforeAll(): Promise<void> {
await this.inspector.connect()
}
async onBeforeTest(): Promise<void> {
await this.inspector.resetCoverage();
}
async onAfterTest(test: ExampleTest): Promise<void> {
await this.inspector
.collectCoverage(process.cwd(), test.file, test.name)
.then((result) => {
test.meta.testKey = result.testKey;
test.meta.coveredFiles = result.coveredFiles;
});
}
async onAfterAll(): Promise<void> {
await this.inspector.disconnect()
}
}Install and use current node version.
NODE_VER=$(cat ./.nvmrc)
nvm install $NODE_VER
nvm use $NODE_VERInstall dependencies with pnpm.
pnpm installBuild the plugin.
pnpm buildRun tests.
pnpm test