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 f3ef3d3..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.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 e0b8b97..6c286c0 100644 --- a/readme.md +++ b/readme.md @@ -57,7 +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`. +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 @@ -77,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. +