Skip to content

Commit

Permalink
Rename the env var, add tests, improve docs, and update the changelog.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenseric committed Feb 17, 2022
1 parent 50e1430 commit 709f69e
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 48 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions coverage-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>} Resolves when all work is complete.
*/
async function coverageNode() {
Expand All @@ -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
Expand Down
137 changes: 98 additions & 39 deletions coverage-node.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
18 changes: 11 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

1 file missing coverage:

<file path>:1:1 → 1:8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

0/1 files covered.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Skipped code coverage as Node.js is <process Node.js version>, v13.3.0+ is supported.

0 comments on commit 709f69e

Please sign in to comment.