Skip to content
Merged
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
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Enjoying **Poku**? [Give him a star to show your support](https://github.com/wel
npm i -D @pokujs/c8
```

### Enable the Plugin
### Usage

#### Enable the Plugin

```js
// poku.config.js
Expand All @@ -39,7 +41,7 @@ export default defineConfig({
});
```

That's it! Run `poku` and a coverage summary will be printed after your test results.
Run `poku` and a coverage summary will be printed after your test results.

> [!IMPORTANT]
>
Expand All @@ -51,6 +53,9 @@ That's it! Run `poku` and a coverage summary will be printed after your test res

```js
coverage({
// Activation
requireFlag: true, // default: false

// Reporters
reporter: ['text', 'lcov'], // default: ['text']

Expand Down Expand Up @@ -141,6 +146,25 @@ coverage({
});
```

### Require `--coverage` flag

By default, coverage runs whenever the plugin is active. Use `requireFlag` to only collect coverage when `--coverage` is passed to the CLI, keeping watch mode, debugging, and filtered runs fast:

```js
coverage({
include: ['src/**'],
requireFlag: true,
});
```

```bash
# No coverage (plugin is a no-op)
poku test/

# With coverage
poku --coverage test/
```

### Extending Monocart reporters

```bash
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type { CoverageOptions } from './types.js';
export const coverage = (
options: CoverageOptions = Object.create(null)
): PokuPlugin => {
let enabled = false;
let tempDir: string;
let originalEnv: string | undefined;
let userProvidedTempDir: boolean;
Expand All @@ -18,6 +19,9 @@ export const coverage = (
name: '@pokujs/c8',

setup(context) {
if (options.requireFlag && !process.argv.includes('--coverage')) return;
enabled = true;

if (context.runtime !== 'node')
console.warn(
`[@pokujs/c8] V8 coverage is only supported on Node.js (current runtime: ${context.runtime}). Coverage data may not be collected.`
Expand All @@ -44,6 +48,8 @@ export const coverage = (
},

async teardown(context) {
if (!enabled) return;

if (originalEnv !== undefined) process.env.NODE_V8_COVERAGE = originalEnv;
else delete process.env.NODE_V8_COVERAGE;

Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ type KnownExtension =
type Extension = KnownExtension | (string & NonNullable<unknown>);

export type CoverageOptions = {
/**
* Require the `--coverage` CLI flag to activate coverage collection.
*
* When `true`, coverage only runs if `--coverage` is passed to the CLI.
* When `false`, coverage runs whenever the plugin is active.
*
* @default false
*/
requireFlag?: boolean;

/** Coverage reporters to use. */
reporter?: Reporter | Reporter[];

Expand Down
13 changes: 13 additions & 0 deletions test/__fixtures__/e2e/configs/require-flag.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'poku';
import { coverage } from '../../../../src/index.ts';

export default defineConfig({
include: ['test/'],
plugins: [
coverage({
include: ['src/**'],
reporter: ['text'],
requireFlag: true,
}),
],
});
31 changes: 31 additions & 0 deletions test/e2e/coverage-require-flag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assert, test } from 'poku';
import { inspectPoku } from 'poku/plugins';

const fixtureDir = 'test/__fixtures__/e2e';
const pokuBin = 'node_modules/poku/lib/bin/index.js';

test('coverage is skipped without --coverage flag when requireFlag is true', async () => {
const result = await inspectPoku({
command: '-c=configs/require-flag.config.ts',
spawnOptions: { cwd: fixtureDir },
bin: pokuBin,
});

assert.strictEqual(result.exitCode, 0);
assert(
!result.stdout.includes('%'),
'coverage report should not be generated'
);
});

test('coverage runs with --coverage flag when requireFlag is true', async () => {
const result = await inspectPoku({
command: '--coverage -c=configs/require-flag.config.ts',
spawnOptions: { cwd: fixtureDir },
bin: pokuBin,
});

assert.strictEqual(result.exitCode, 0);
assert(result.stdout.includes('math.ts'));
assert(result.stdout.includes('%'));
});
Loading