Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow missing coverage via an environment variable #2

Merged
merged 4 commits into from
Feb 17, 2022
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
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.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
12 changes: 11 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
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.