From 6ee338cf586216177e7c78cbdd97bd8cbae5e061 Mon Sep 17 00:00:00 2001 From: Vitalii Tverdokhlib Date: Fri, 17 Dec 2021 08:45:36 +0200 Subject: [PATCH 1/3] add IGNORE_COVERAGE env for only show coverage --- readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/readme.md b/readme.md index e0b8b97..f4ea06b 100644 --- a/readme.md +++ b/readme.md @@ -59,6 +59,12 @@ if (false) console.log("Never runs."); Substitutes the normal `node` command; any [`node` CLI options](https://nodejs.org/api/cli.html#cli_options) can be used to run a test script. If the script doesn’t error a code coverage analysis is reported to the console, and if coverage is incomplete the exit code is `1`. +If environment IGNORE_COVERAGE is set, coverage-node show result to console, but return last run code from test process: +```sh +IGNORE_COVERAGE=true npx coverage-node test.mjs +echo $? # return 0, if test is successful +``` + #### Examples [`npx`](https://docs.npmjs.com/cli/v8/commands/npx) can be used to quickly check code coverage for a script: From 6b04b41aea54fa2156d048795f218f96e09ac155 Mon Sep 17 00:00:00 2001 From: vitaliytv Date: Mon, 7 Feb 2022 06:07:18 +0200 Subject: [PATCH 2/3] env for only show coverage --- coverage-node.mjs | 2 +- readme.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coverage-node.mjs b/coverage-node.mjs index f3ef3d3..4308c6e 100755 --- a/coverage-node.mjs +++ b/coverage-node.mjs @@ -41,7 +41,7 @@ async function coverageNode() { if (exitCode === 0) { const analysis = await analyseCoverage(tempDirPath); reportCoverage(analysis); - if (analysis.uncovered.length) process.exitCode = 1; + if (analysis.uncovered.length && !process.env.TOLERATE_INCOMPLETE_COVERAGE) process.exitCode = 1; } else if (exitCode !== null) process.exitCode = exitCode; }); // coverage ignore next line diff --git a/readme.md b/readme.md index f4ea06b..af6e573 100644 --- a/readme.md +++ b/readme.md @@ -59,9 +59,9 @@ if (false) console.log("Never runs."); Substitutes the normal `node` command; any [`node` CLI options](https://nodejs.org/api/cli.html#cli_options) can be used to run a test script. If the script doesn’t error a code coverage analysis is reported to the console, and if coverage is incomplete the exit code is `1`. -If environment IGNORE_COVERAGE is set, coverage-node show result to console, but return last run code from test process: +If environment TOLERATE_INCOMPLETE_COVERAGE is set, coverage-node show result to console, but return last run code from test process: ```sh -IGNORE_COVERAGE=true npx coverage-node test.mjs +TOLERATE_INCOMPLETE_COVERAGE=true npx coverage-node test.mjs echo $? # return 0, if test is successful ``` From 709f69ea4e74a7a31c5acca2cf7e88f3b1309e05 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Thu, 17 Feb 2022 21:48:12 +1100 Subject: [PATCH 3/3] Rename the env var, add tests, improve docs, and update the changelog. --- changelog.md | 4 + coverage-node.mjs | 6 +- coverage-node.test.mjs | 137 +++++++++++++----- readme.md | 18 ++- ...ERAGE-falsy-coverage-supported-stderr.ans} | 0 ...ERAGE-falsy-coverage-supported-stdout.ans} | 0 ...AGE-falsy-coverage-unsupported-stdout.ans} | 0 ...ERAGE-truthy-coverage-supported-stderr.ans | 4 + ...ERAGE-truthy-coverage-supported-stdout.ans | 3 + ...AGE-truthy-coverage-unsupported-stdout.ans | 3 + 10 files changed, 127 insertions(+), 48 deletions(-) rename test/snapshots/coverage-node/{1-uncovered-file-coverage-supported-stderr.ans => 1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stderr.ans} (100%) rename test/snapshots/coverage-node/{1-uncovered-file-coverage-supported-stdout.ans => 1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stdout.ans} (100%) rename test/snapshots/coverage-node/{1-uncovered-file-coverage-unsupported-stdout.ans => 1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-unsupported-stdout.ans} (100%) create mode 100644 test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stderr.ans create mode 100644 test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stdout.ans create mode 100644 test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-unsupported-stdout.ans diff --git a/changelog.md b/changelog.md index a81e05c..06059fa 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## Next +### Minor + +- A truthy `ALLOW_MISSING_COVERAGE` environment variable can now be used with the `coverage-node` CLI to prevent missing coverage from causing the process to exit with code `1`, via [#2](https://github.com/jaydenseric/coverage-node/pull/2). + ### Patch - Updated dependencies. diff --git a/coverage-node.mjs b/coverage-node.mjs index 4308c6e..ddb5556 100755 --- a/coverage-node.mjs +++ b/coverage-node.mjs @@ -16,7 +16,8 @@ import reportCoverage from "./reportCoverage.mjs"; /** * Powers the `coverage-node` CLI. Runs Node.js with the given arguments and * coverage enabled. An analysis of the coverage is reported to the console, and - * if coverage isn’t complete the process exits with code `1`. + * if coverage is incomplete and there isn’t a truthy `ALLOW_MISSING_COVERAGE` + * environment variable the process exits with code `1`. * @returns {Promise} Resolves when all work is complete. */ async function coverageNode() { @@ -41,7 +42,8 @@ async function coverageNode() { if (exitCode === 0) { const analysis = await analyseCoverage(tempDirPath); reportCoverage(analysis); - if (analysis.uncovered.length && !process.env.TOLERATE_INCOMPLETE_COVERAGE) process.exitCode = 1; + if (analysis.uncovered.length && !process.env.ALLOW_MISSING_COVERAGE) + process.exitCode = 1; } else if (exitCode !== null) process.exitCode = exitCode; }); // coverage ignore next line diff --git a/coverage-node.test.mjs b/coverage-node.test.mjs index 6f14fd8..a9a3b38 100644 --- a/coverage-node.test.mjs +++ b/coverage-node.test.mjs @@ -113,57 +113,116 @@ export default (tests) => { }); }); - tests.add("`coverage-node` CLI with 1 uncovered file.", async () => { - await disposableDirectory(async (tempDirPath) => { - const filePath = join(tempDirPath, "index.mjs"); + tests.add( + "`coverage-node` CLI with 1 uncovered file, `ALLOW_MISSING_COVERAGE` environment variable falsy.", + async () => { + await disposableDirectory(async (tempDirPath) => { + const filePath = join(tempDirPath, "index.mjs"); - await fs.promises.writeFile(filePath, "() => {};"); + await fs.promises.writeFile(filePath, "() => {};"); - const { stdout, stderr, status, error } = spawnSync( - "node", - [COVERAGE_NODE_CLI_PATH, filePath], - { - env: { - ...process.env, - FORCE_COLOR: "1", - }, - } - ); + const { stdout, stderr, status, error } = spawnSync( + "node", + [COVERAGE_NODE_CLI_PATH, filePath], + { + env: { + ...process.env, + FORCE_COLOR: "1", + }, + } + ); - if (error) throw error; + if (error) throw error; - await snapshot( - coverageSupported - ? stdout.toString() - : stdout + await snapshot( + coverageSupported + ? stdout.toString() + : stdout + .toString() + .replace( + process.version, + SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION + ), + new URL( + `./test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-${ + coverageSupported ? "supported" : "unsupported" + }-stdout.ans`, + import.meta.url + ) + ); + + if (coverageSupported) + await snapshot( + stderr .toString() - .replace( - process.version, - SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION - ), - new URL( - `./test/snapshots/coverage-node/1-uncovered-file-coverage-${ - coverageSupported ? "supported" : "unsupported" - }-stdout.ans`, - import.meta.url - ) - ); + .replace(relative("", filePath), SNAPSHOT_REPLACEMENT_FILE_PATH), + new URL( + "./test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stderr.ans", + import.meta.url + ) + ); + else strictEqual(stderr.toString(), ""); + + strictEqual(status, coverageSupported ? 1 : 0); + }); + } + ); + + tests.add( + "`coverage-node` CLI with 1 uncovered file, `ALLOW_MISSING_COVERAGE` environment variable truthy.", + async () => { + await disposableDirectory(async (tempDirPath) => { + const filePath = join(tempDirPath, "index.mjs"); + + await fs.promises.writeFile(filePath, "() => {};"); + + const { stdout, stderr, status, error } = spawnSync( + "node", + [COVERAGE_NODE_CLI_PATH, filePath], + { + env: { + ...process.env, + FORCE_COLOR: "1", + ALLOW_MISSING_COVERAGE: "1", + }, + } + ); + + if (error) throw error; - if (coverageSupported) await snapshot( - stderr - .toString() - .replace(relative("", filePath), SNAPSHOT_REPLACEMENT_FILE_PATH), + coverageSupported + ? stdout.toString() + : stdout + .toString() + .replace( + process.version, + SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION + ), new URL( - "./test/snapshots/coverage-node/1-uncovered-file-coverage-supported-stderr.ans", + `./test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-${ + coverageSupported ? "supported" : "unsupported" + }-stdout.ans`, import.meta.url ) ); - else strictEqual(stderr.toString(), ""); - strictEqual(status, coverageSupported ? 1 : 0); - }); - }); + if (coverageSupported) + await snapshot( + stderr + .toString() + .replace(relative("", filePath), SNAPSHOT_REPLACEMENT_FILE_PATH), + new URL( + "./test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stderr.ans", + import.meta.url + ) + ); + else strictEqual(stderr.toString(), ""); + + strictEqual(status, 0); + }); + } + ); tests.add( "`coverage-node` CLI with 2 covered, ignored and uncovered files.", diff --git a/readme.md b/readme.md index af6e573..6c286c0 100644 --- a/readme.md +++ b/readme.md @@ -57,13 +57,7 @@ if (false) console.log("Never runs."); ### Command `coverage-node` -Substitutes the normal `node` command; any [`node` CLI options](https://nodejs.org/api/cli.html#cli_options) can be used to run a test script. If the script doesn’t error a code coverage analysis is reported to the console, and if coverage is incomplete the exit code is `1`. - -If environment TOLERATE_INCOMPLETE_COVERAGE is set, coverage-node show result to console, but return last run code from test process: -```sh -TOLERATE_INCOMPLETE_COVERAGE=true npx coverage-node test.mjs -echo $? # return 0, if test is successful -``` +Substitutes the normal `node` command; any [`node` CLI options](https://nodejs.org/api/cli.html#cli_options) can be used to run a test script. If the script doesn’t error a code coverage analysis is reported to the console, and if coverage is incomplete and there isn’t a truthy `ALLOW_MISSING_COVERAGE` environment variable the process exits with code `1`. #### Examples @@ -83,6 +77,16 @@ A [`package.json` script](https://docs.npmjs.com/cli/v8/configuring-npm/package- } ``` +A [`package.json` script](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#scripts) that allows missing coverage: + +```json +{ + "scripts": { + "test": "ALLOW_MISSING_COVERAGE=1 coverage-node test.mjs" + } +} +``` + ## Exports These ECMAScript modules are published to [npm](https://npmjs.com) and exported via the [`package.json`](./package.json) `exports` field: diff --git a/test/snapshots/coverage-node/1-uncovered-file-coverage-supported-stderr.ans b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stderr.ans similarity index 100% rename from test/snapshots/coverage-node/1-uncovered-file-coverage-supported-stderr.ans rename to test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stderr.ans diff --git a/test/snapshots/coverage-node/1-uncovered-file-coverage-supported-stdout.ans b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stdout.ans similarity index 100% rename from test/snapshots/coverage-node/1-uncovered-file-coverage-supported-stdout.ans rename to test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-supported-stdout.ans diff --git a/test/snapshots/coverage-node/1-uncovered-file-coverage-unsupported-stdout.ans b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-unsupported-stdout.ans similarity index 100% rename from test/snapshots/coverage-node/1-uncovered-file-coverage-unsupported-stdout.ans rename to test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-falsy-coverage-unsupported-stdout.ans diff --git a/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stderr.ans b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stderr.ans new file mode 100644 index 0000000..877bc9d --- /dev/null +++ b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stderr.ans @@ -0,0 +1,4 @@ + +1 file missing coverage: + + :1:1 → 1:8 diff --git a/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stdout.ans b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stdout.ans new file mode 100644 index 0000000..9740782 --- /dev/null +++ b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-supported-stdout.ans @@ -0,0 +1,3 @@ + +0/1 files covered. + diff --git a/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-unsupported-stdout.ans b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-unsupported-stdout.ans new file mode 100644 index 0000000..ede1113 --- /dev/null +++ b/test/snapshots/coverage-node/1-uncovered-file-ALLOW_MISSING_COVERAGE-truthy-coverage-unsupported-stdout.ans @@ -0,0 +1,3 @@ + +Skipped code coverage as Node.js is , v13.3.0+ is supported. +