diff --git a/test/fixtures/test-runner/coverage-default-exclusion/file-test.js b/test/fixtures/test-runner/coverage-default-exclusion/file-test.js new file mode 100644 index 00000000000000..ff1e8a2be02fcf --- /dev/null +++ b/test/fixtures/test-runner/coverage-default-exclusion/file-test.js @@ -0,0 +1,7 @@ +const test = require('node:test'); +const assert = require('node:assert'); +const { foo } = require('./logic-file'); + +test('foo returns 1', () => { + assert.strictEqual(foo(), 1); +}); diff --git a/test/fixtures/test-runner/coverage-default-exclusion/file.test.mjs b/test/fixtures/test-runner/coverage-default-exclusion/file.test.mjs new file mode 100644 index 00000000000000..5b25d4c85d6b6e --- /dev/null +++ b/test/fixtures/test-runner/coverage-default-exclusion/file.test.mjs @@ -0,0 +1,7 @@ +import test from 'node:test'; +import assert from 'node:assert'; +import { foo } from './logic-file.js'; + +test('foo returns 1', () => { + assert.strictEqual(foo(), 1); +}); diff --git a/test/fixtures/test-runner/coverage-default-exclusion/file.test.ts b/test/fixtures/test-runner/coverage-default-exclusion/file.test.ts new file mode 100644 index 00000000000000..5b25d4c85d6b6e --- /dev/null +++ b/test/fixtures/test-runner/coverage-default-exclusion/file.test.ts @@ -0,0 +1,7 @@ +import test from 'node:test'; +import assert from 'node:assert'; +import { foo } from './logic-file.js'; + +test('foo returns 1', () => { + assert.strictEqual(foo(), 1); +}); diff --git a/test/fixtures/test-runner/coverage-default-exclusion/logic-file.js b/test/fixtures/test-runner/coverage-default-exclusion/logic-file.js new file mode 100644 index 00000000000000..cc2ca8284abe7d --- /dev/null +++ b/test/fixtures/test-runner/coverage-default-exclusion/logic-file.js @@ -0,0 +1,9 @@ +function foo() { + return 1; +} + +function bar() { + return 'bar'; +} + +module.exports = { foo, bar }; diff --git a/test/fixtures/test-runner/coverage-default-exclusion/test.cjs b/test/fixtures/test-runner/coverage-default-exclusion/test.cjs new file mode 100644 index 00000000000000..641bad44ed4f33 --- /dev/null +++ b/test/fixtures/test-runner/coverage-default-exclusion/test.cjs @@ -0,0 +1,7 @@ +const test = require('node:test'); +const assert = require('node:assert'); +const { foo } = require('./logic-file.js'); + +test('foo returns 1', () => { + assert.strictEqual(foo(), 1); +}); diff --git a/test/fixtures/test-runner/coverage-default-exclusion/test/not-matching-test-name.js b/test/fixtures/test-runner/coverage-default-exclusion/test/not-matching-test-name.js new file mode 100644 index 00000000000000..8d01a2b260657f --- /dev/null +++ b/test/fixtures/test-runner/coverage-default-exclusion/test/not-matching-test-name.js @@ -0,0 +1,7 @@ +const test = require('node:test'); +const assert = require('node:assert'); +const { foo } = require('../logic-file.js'); + +test('foo returns 1', () => { + assert.strictEqual(foo(), 1); +}); diff --git a/test/parallel/test-runner-coverage-default-exclusion.mjs b/test/parallel/test-runner-coverage-default-exclusion.mjs new file mode 100644 index 00000000000000..a7c0f416ad4b67 --- /dev/null +++ b/test/parallel/test-runner-coverage-default-exclusion.mjs @@ -0,0 +1,113 @@ +import '../common/index.mjs'; +import { before, describe, it } from 'node:test'; +import assert from 'node:assert'; +import { spawnSync } from 'node:child_process'; +import { cp } from 'node:fs/promises'; +import tmpdir from '../common/tmpdir.js'; +import fixtures from '../common/fixtures.js'; + +tmpdir.refresh(); + +async function setupFixtures() { + const fixtureDir = fixtures.path('test-runner', 'coverage-default-exclusion'); + await cp(fixtureDir, tmpdir.path, { recursive: true }); +} + +describe('test runner coverage default exclusion', () => { + before(async () => { + await setupFixtures(); + }); + + it('should override default exclusion setting --test-coverage-exclude', async () => { + const report = [ + '# start of coverage report', + '# ---------------------------------------------------------------------------', + '# file | line % | branch % | funcs % | uncovered lines', + '# ---------------------------------------------------------------------------', + '# file-test.js | 100.00 | 100.00 | 100.00 | ', + '# file.test.mjs | 100.00 | 100.00 | 100.00 | ', + '# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7', + '# test.cjs | 100.00 | 100.00 | 100.00 | ', + '# test | | | | ', + '# not-matching-test-name.js | 100.00 | 100.00 | 100.00 | ', + '# ---------------------------------------------------------------------------', + '# all files | 91.89 | 100.00 | 83.33 | ', + '# ---------------------------------------------------------------------------', + '# end of coverage report', + ].join('\n'); + + + const args = [ + '--test', + '--experimental-test-coverage', + '--test-coverage-exclude=!test/**', + '--test-reporter=tap', + ]; + const result = spawnSync(process.execPath, args, { + env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path }, + cwd: tmpdir.path + }); + + assert.strictEqual(result.stderr.toString(), ''); + assert(result.stdout.toString().includes(report)); + assert.strictEqual(result.status, 0); + }); + + it('should exclude test files from coverage by default', async () => { + const report = [ + '# start of coverage report', + '# --------------------------------------------------------------', + '# file | line % | branch % | funcs % | uncovered lines', + '# --------------------------------------------------------------', + '# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7', + '# --------------------------------------------------------------', + '# all files | 66.67 | 100.00 | 50.00 | ', + '# --------------------------------------------------------------', + '# end of coverage report', + ].join('\n'); + + const args = [ + '--test', + '--experimental-test-coverage', + '--test-reporter=tap', + ]; + const result = spawnSync(process.execPath, args, { + env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path }, + cwd: tmpdir.path + }); + + assert.strictEqual(result.stderr.toString(), ''); + assert(result.stdout.toString().includes(report)); + assert.strictEqual(result.status, 0); + }); + + it('should exclude ts test files when using --experimental-strip-types', async () => { + const report = [ + '# start of coverage report', + '# --------------------------------------------------------------', + '# file | line % | branch % | funcs % | uncovered lines', + '# --------------------------------------------------------------', + '# logic-file.js | 66.67 | 100.00 | 50.00 | 5-7', + '# --------------------------------------------------------------', + '# all files | 66.67 | 100.00 | 50.00 | ', + '# --------------------------------------------------------------', + '# end of coverage report', + ].join('\n'); + + const args = [ + '--test', + '--experimental-test-coverage', + '--experimental-strip-types', + '--disable-warning=ExperimentalWarning', + '--test-reporter=tap', + ]; + const result = spawnSync(process.execPath, args, { + env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path }, + cwd: tmpdir.path + }); + + assert.strictEqual(result.stderr.toString(), ''); + assert(result.stdout.toString().includes(report)); + assert.strictEqual(result.status, 0); + }); +});