From ea64eb889526e292375ec0ebf8640393ddf27f10 Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Sun, 5 Apr 2026 17:28:43 -0300 Subject: [PATCH 1/2] feat: support dynamic `coverage` flag --- README.md | 42 +++++++++++++++---- src/index.ts | 6 +++ src/types.ts | 10 +++++ .../e2e/configs/require-flag.config.ts | 13 ++++++ test/e2e/coverage-require-flag.test.ts | 31 ++++++++++++++ 5 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 test/__fixtures__/e2e/configs/require-flag.config.ts create mode 100644 test/e2e/coverage-require-flag.test.ts diff --git a/README.md b/README.md index 98ed2a9..83267e2 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,17 @@ Enjoying **Poku**? [Give him a star to show your support](https://github.com/wel npm i -D @pokujs/c8 ``` -### Enable the Plugin +### Usage + +Run `poku` and a coverage summary will be printed after your test results. + +> [!IMPORTANT] +> +> This plugin relies on **Node.js**' built-in `NODE_V8_COVERAGE` environment variable to collect coverage data. **Bun** and **Deno** do not support this mechanism, so coverage data will not be collected when running tests with these runtimes. + +--- + +### Customize Plugin ```js // poku.config.js @@ -39,18 +49,13 @@ export default defineConfig({ }); ``` -That's it! Run `poku` and a coverage summary will be printed after your test results. - -> [!IMPORTANT] -> -> This plugin relies on **Node.js**' built-in `NODE_V8_COVERAGE` environment variable to collect coverage data. **Bun** and **Deno** do not support this mechanism, so coverage data will not be collected when running tests with these runtimes. - ---- - ## Options ```js coverage({ + // Activation + requireFlag: true, // default: false + // Reporters reporter: ['text', 'lcov'], // default: ['text'] @@ -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 diff --git a/src/index.ts b/src/index.ts index 05dd3a4..e31ddc6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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.` @@ -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; diff --git a/src/types.ts b/src/types.ts index 9fc1ea6..7dca450 100644 --- a/src/types.ts +++ b/src/types.ts @@ -31,6 +31,16 @@ type KnownExtension = type Extension = KnownExtension | (string & NonNullable); 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[]; diff --git a/test/__fixtures__/e2e/configs/require-flag.config.ts b/test/__fixtures__/e2e/configs/require-flag.config.ts new file mode 100644 index 0000000..200f897 --- /dev/null +++ b/test/__fixtures__/e2e/configs/require-flag.config.ts @@ -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, + }), + ], +}); diff --git a/test/e2e/coverage-require-flag.test.ts b/test/e2e/coverage-require-flag.test.ts new file mode 100644 index 0000000..7cdd96f --- /dev/null +++ b/test/e2e/coverage-require-flag.test.ts @@ -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('%')); +}); From 80fd9955bac1fb0b5068d3d3111d48ecbd8f7457 Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Sun, 5 Apr 2026 17:34:32 -0300 Subject: [PATCH 2/2] docs: fix usage --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 83267e2..e03e676 100644 --- a/README.md +++ b/README.md @@ -29,15 +29,7 @@ npm i -D @pokujs/c8 ### Usage -Run `poku` and a coverage summary will be printed after your test results. - -> [!IMPORTANT] -> -> This plugin relies on **Node.js**' built-in `NODE_V8_COVERAGE` environment variable to collect coverage data. **Bun** and **Deno** do not support this mechanism, so coverage data will not be collected when running tests with these runtimes. - ---- - -### Customize Plugin +#### Enable the Plugin ```js // poku.config.js @@ -49,6 +41,14 @@ export default defineConfig({ }); ``` +Run `poku` and a coverage summary will be printed after your test results. + +> [!IMPORTANT] +> +> This plugin relies on **Node.js**' built-in `NODE_V8_COVERAGE` environment variable to collect coverage data. **Bun** and **Deno** do not support this mechanism, so coverage data will not be collected when running tests with these runtimes. + +--- + ## Options ```js