Skip to content

Commit 4e2bdf6

Browse files
committed
test: add default coverage default exclusions test
1 parent 4f5885e commit 4e2bdf6

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
const { foo } = require('./logic-file');
4+
5+
test('foo returns 1', () => {
6+
assert.strictEqual(foo(), 1);
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test from 'node:test';
2+
import assert from 'node:assert';
3+
import { foo } from './logic-file.js';
4+
5+
test('foo returns 1', () => {
6+
assert.strictEqual(foo(), 1);
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test from 'node:test';
2+
import assert from 'node:assert';
3+
import { foo } from './logic-file.js';
4+
5+
test('foo returns 1', () => {
6+
assert.strictEqual(foo(), 1);
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function foo() {
2+
return 1;
3+
}
4+
5+
function bar() {
6+
return 'bar';
7+
}
8+
9+
module.exports = { foo, bar };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
const { foo } = require('./logic-file.js');
4+
5+
test('foo returns 1', () => {
6+
assert.strictEqual(foo(), 1);
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
const { foo } = require('../logic-file.js');
4+
5+
test('foo returns 1', () => {
6+
assert.strictEqual(foo(), 1);
7+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import '../common/index.mjs';
2+
import { before, describe, it } from 'node:test';
3+
import assert from 'node:assert';
4+
import { spawnSync } from 'node:child_process';
5+
import { cp } from 'node:fs/promises';
6+
import tmpdir from '../common/tmpdir.js';
7+
import fixtures from '../common/fixtures.js';
8+
9+
tmpdir.refresh();
10+
11+
async function setupFixtures() {
12+
const fixtureDir = fixtures.path('test-runner', 'coverage-default-exclusion');
13+
await cp(fixtureDir, tmpdir.path, { recursive: true });
14+
}
15+
16+
describe('test runner coverage default exclusion', () => {
17+
before(async () => {
18+
await setupFixtures();
19+
});
20+
21+
it('should override default exclusion setting --test-coverage-exclude', async () => {
22+
const report = [
23+
'# start of coverage report',
24+
'# ---------------------------------------------------------------------------',
25+
'# file | line % | branch % | funcs % | uncovered lines',
26+
'# ---------------------------------------------------------------------------',
27+
'# file-test.js | 100.00 | 100.00 | 100.00 | ',
28+
'# file.test.mjs | 100.00 | 100.00 | 100.00 | ',
29+
'# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7',
30+
'# test.cjs | 100.00 | 100.00 | 100.00 | ',
31+
'# test | | | | ',
32+
'# not-matching-test-name.js | 100.00 | 100.00 | 100.00 | ',
33+
'# ---------------------------------------------------------------------------',
34+
'# all files | 91.89 | 100.00 | 83.33 | ',
35+
'# ---------------------------------------------------------------------------',
36+
'# end of coverage report',
37+
].join('\n');
38+
39+
40+
const args = [
41+
'--test',
42+
'--experimental-test-coverage',
43+
'--test-coverage-exclude=!test/**',
44+
'--test-reporter=tap',
45+
];
46+
const result = spawnSync(process.execPath, args, {
47+
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
48+
cwd: tmpdir.path
49+
});
50+
51+
assert.strictEqual(result.stderr.toString(), '');
52+
assert(result.stdout.toString().includes(report));
53+
assert.strictEqual(result.status, 0);
54+
});
55+
56+
it('should exclude test files from coverage by default', async () => {
57+
const report = [
58+
'# start of coverage report',
59+
'# --------------------------------------------------------------',
60+
'# file | line % | branch % | funcs % | uncovered lines',
61+
'# --------------------------------------------------------------',
62+
'# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7',
63+
'# --------------------------------------------------------------',
64+
'# all files | 66.67 | 100.00 | 50.00 | ',
65+
'# --------------------------------------------------------------',
66+
'# end of coverage report',
67+
].join('\n');
68+
69+
const args = [
70+
'--test',
71+
'--experimental-test-coverage',
72+
'--test-reporter=tap',
73+
];
74+
const result = spawnSync(process.execPath, args, {
75+
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
76+
cwd: tmpdir.path
77+
});
78+
79+
assert.strictEqual(result.stderr.toString(), '');
80+
assert(result.stdout.toString().includes(report));
81+
assert.strictEqual(result.status, 0);
82+
});
83+
84+
it('should exclude ts test files when using --experimental-strip-types', async () => {
85+
const report = [
86+
'# start of coverage report',
87+
'# --------------------------------------------------------------',
88+
'# file | line % | branch % | funcs % | uncovered lines',
89+
'# --------------------------------------------------------------',
90+
'# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7',
91+
'# --------------------------------------------------------------',
92+
'# all files | 66.67 | 100.00 | 50.00 | ',
93+
'# --------------------------------------------------------------',
94+
'# end of coverage report',
95+
].join('\n');
96+
97+
const args = [
98+
'--test',
99+
'--experimental-test-coverage',
100+
'--experimental-strip-types',
101+
'--disable-warning=ExperimentalWarning',
102+
'--test-reporter=tap',
103+
];
104+
const result = spawnSync(process.execPath, args, {
105+
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
106+
cwd: tmpdir.path
107+
});
108+
109+
assert.strictEqual(result.stderr.toString(), '');
110+
assert(result.stdout.toString().includes(report));
111+
assert.strictEqual(result.status, 0);
112+
});
113+
});

0 commit comments

Comments
 (0)