GeezerVison is showing github.com — test_runner: add option to forbid exclusive tests · nodejs/node@e806656
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +17Lines changed: 17 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3214,6 +3214,22 @@ added: v22.8.0 | 3214 | 3214 | Require a minimum percent of covered lines. If code coverage does not reach | 3215 | 3215 | the threshold specified, the process will exit with code `1`. | 3216 | 3216 |
| | 3217 | +### `--test-forbid-only` | | 3218 | + | | 3219 | +<!-- YAML | | 3220 | +added: REPLACEME | | 3221 | +--> | | 3222 | + | | 3223 | +Fails the test run if a test or suite is declared with `only: true`, including | | 3224 | +the `test.only()`, `it.only()`, `suite.only()`, and `describe.only()` shorthands, | | 3225 | +or if `testContext.runOnly(true)` is called. This is useful in CI to prevent | | 3226 | +accidentally committing exclusive tests that exclude the rest of a test suite. | | 3227 | + | | 3228 | +This option applies with either test isolation mode and takes precedence over | | 3229 | +`--test-only`. Name filtering, TODO markers, and expected failures do not suppress | | 3230 | +detected violations. Only declarations evaluated during the run are checked; | | 3231 | +code inside a skipped test callback is not evaluated. | | 3232 | + | 3217 | 3233 | ### `--test-force-exit` | 3218 | 3234 |
| 3219 | 3235 | <!-- YAML | @@ -4322,6 +4338,7 @@ one is included in the list below. | 4322 | 4338 | * `--test-coverage-include-all` | 4323 | 4339 | * `--test-coverage-include` | 4324 | 4340 | * `--test-coverage-lines` | | 4341 | +* `--test-forbid-only` | 4325 | 4342 | * `--test-global-setup` | 4326 | 4343 | * `--test-isolation` | 4327 | 4344 | * `--test-name-pattern` | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/test.md +15Lines changed: 15 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -355,6 +355,14 @@ it.todo('should do the thing', { expectFailure: true }, () => { | 355 | 355 |
| 356 | 356 | ## `only` tests | 357 | 357 |
| | 358 | +To prevent exclusive tests from accidentally excluding tests in CI, use | | 359 | +[`--test-forbid-only`][] or the `forbidOnly` option of [`run()`][]. When enabled, | | 360 | +declaring a test or suite with `only: true` or calling `testContext.runOnly(true)` | | 361 | +fails the run. This includes the `.only()` shorthands and applies with either | | 362 | +isolation mode, even when `--test-only` is also enabled. Name filtering, TODO | | 363 | +markers, and expected failures cannot suppress detected violations. Declarations | | 364 | +inside callbacks that are never executed are not checked. | | 365 | + | 358 | 366 | If Node.js is started with the [`--test-only`][] command-line option, or test | 359 | 367 | isolation is disabled, it is possible to skip all tests except for a selected | 360 | 368 | subset by passing the `only` option to the tests that should run. When a test | @@ -1706,6 +1714,9 @@ added: | 1706 | 1714 | - v18.9.0 | 1707 | 1715 | - v16.19.0 | 1708 | 1716 | changes: | | 1717 | + - version: REPLACEME | | 1718 | + pr-url: https://github.com/nodejs/node/pull/66578 | | 1719 | + description: Added the `forbidOnly` option. | 1709 | 1720 | - version: v26.2.0 | 1710 | 1721 | pr-url: https://github.com/nodejs/node/pull/63221 | 1711 | 1722 | description: Added the `testTagFilters` option. | @@ -1760,6 +1771,9 @@ changes: | 1760 | 1771 | * `forceExit` {boolean} Configures the test runner to exit the process once | 1761 | 1772 | all known tests have finished executing even if the event loop would | 1762 | 1773 | otherwise remain active. **Default:** `false`. | | 1774 | + * `forbidOnly` {boolean} Fail the run when a test or suite is declared with | | 1775 | + `only: true` or `testContext.runOnly(true)` is called. See [`only` tests](#only-tests). | | 1776 | + **Default:** the value of [`--test-forbid-only`][]. | 1763 | 1777 | * `globPatterns` {Array} An array containing the list of glob patterns to | 1764 | 1778 | match test files. This option cannot be used together with `files`. | 1765 | 1779 | **Default:** Same as [running tests from the command line][]. | @@ -5158,6 +5172,7 @@ test.describe('my suite', (suite) => { | 5158 | 5172 | [`--test-concurrency`]: cli.md#--test-concurrency | 5159 | 5173 | [`--test-coverage-exclude`]: cli.md#--test-coverage-exclude | 5160 | 5174 | [`--test-coverage-include`]: cli.md#--test-coverage-include | | 5175 | +[`--test-forbid-only`]: cli.md#--test-forbid-only | 5161 | 5176 | [`--test-name-pattern`]: cli.md#--test-name-pattern | 5162 | 5177 | [`--test-only`]: cli.md#--test-only | 5163 | 5178 | [`--test-reporter-destination`]: cli.md#--test-reporter-destination | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1609,6 +1609,16 @@ filtering as the rest of the report. | 1609 | 1609 | Require a minimum percent of covered lines. If code coverage does not reach | 1610 | 1610 | the threshold specified, the process will exit with code \fB1\fR. | 1611 | 1611 | . | | 1612 | +.It Fl -test-forbid-only | | 1613 | +Fails the test run if a test or suite is declared with \fBonly: true\fR, including | | 1614 | +the \fBtest.only()\fR, \fBit.only()\fR, \fBsuite.only()\fR, and \fBdescribe.only()\fR shorthands, | | 1615 | +or if \fBtestContext.runOnly(true)\fR is called. This is useful in CI to prevent | | 1616 | +accidentally committing exclusive tests that exclude the rest of a test suite. | | 1617 | +This option applies with either test isolation mode and takes precedence over | | 1618 | +\fB--test-only\fR. Name filtering, TODO markers, and expected failures do not suppress | | 1619 | +detected violations. Only declarations evaluated during the run are checked; | | 1620 | +code inside a skipped test callback is not evaluated. | | 1621 | +. | 1612 | 1622 | .It Fl -test-force-exit | 1613 | 1623 | Configures the test runner to exit the process once all known tests have | 1614 | 1624 | finished executing even if the event loop would otherwise remain active. | @@ -2412,6 +2422,8 @@ one is included in the list below. | 2412 | 2422 | .It | 2413 | 2423 | \fB--test-coverage-lines\fR | 2414 | 2424 | .It | | 2425 | +\fB--test-forbid-only\fR | | 2426 | +.It | 2415 | 2427 | \fB--test-global-setup\fR | 2416 | 2428 | .It | 2417 | 2429 | \fB--test-isolation\fR | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -207,6 +207,7 @@ function getRunArgs(path, { forceExit, | 207 | 207 | testSkipPatterns, | 208 | 208 | testTagFilterExpressions, | 209 | 209 | only, | | 210 | + forbidOnly, | 210 | 211 | hasFiles, | 211 | 212 | testFiles, | 212 | 213 | globPatterns, | @@ -253,6 +254,9 @@ function getRunArgs(path, { forceExit, | 253 | 254 | if (only === true) { | 254 | 255 | ArrayPrototypePush(runArgs, '--test-only'); | 255 | 256 | } | | 257 | + if (forbidOnly != null && forbidOnly !== getOptionValue('--test-forbid-only')) { | | 258 | + ArrayPrototypePush(runArgs, forbidOnly ? '--test-forbid-only' : '--no-test-forbid-only'); | | 259 | + } | 256 | 260 | if (timeout != null) { | 257 | 261 | ArrayPrototypePush(runArgs, `--test-timeout=${timeout}`); | 258 | 262 | } | @@ -789,6 +793,7 @@ function run(options = kEmptyObject) { | 789 | 793 | globalSetupPath, | 790 | 794 | only, | 791 | 795 | globPatterns, | | 796 | + forbidOnly, | 792 | 797 | coverage = false, | 793 | 798 | coverageIncludeAll = false, | 794 | 799 | lineCoverage = 0, | @@ -821,6 +826,9 @@ function run(options = kEmptyObject) { | 821 | 826 | if (only != null) { | 822 | 827 | validateBoolean(only, 'options.only'); | 823 | 828 | } | | 829 | + if (forbidOnly != null) { | | 830 | + validateBoolean(forbidOnly, 'options.forbidOnly'); | | 831 | + } | 824 | 832 | if (globPatterns != null) { | 825 | 833 | validateArray(globPatterns, 'options.globPatterns'); | 826 | 834 | } | @@ -1021,6 +1029,10 @@ function run(options = kEmptyObject) { | 1021 | 1029 | testTagFilters, | 1022 | 1030 | }; | 1023 | 1031 |
| | 1032 | + if (forbidOnly != null) { | | 1033 | + globalOptions.forbidOnly = forbidOnly; | | 1034 | + } | | 1035 | + | 1024 | 1036 | if (isolation === 'none') { | 1025 | 1037 | if (testNamePatterns != null) { | 1026 | 1038 | globalOptions.testNamePatterns = testNamePatterns; | @@ -1067,6 +1079,7 @@ function run(options = kEmptyObject) { | 1067 | 1079 | globPatterns, | 1068 | 1080 | only, | 1069 | 1081 | forceExit, | | 1082 | + forbidOnly, | 1070 | 1083 | cwd, | 1071 | 1084 | isolation, | 1072 | 1085 | argv, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -377,6 +377,9 @@ class TestContext { | 377 | 377 | } | 378 | 378 |
| 379 | 379 | runOnly(value) { | | 380 | + if (value && this.#test.config.forbidOnly) { | | 381 | + failForbiddenOnly(this.#test.root, 'runOnly() is not allowed when forbidOnly is enabled'); | | 382 | + } | 380 | 383 | this.#test.runOnlySubtests = !!value; | 381 | 384 | } | 382 | 385 |
| @@ -575,6 +578,14 @@ function parseExpectFailure(expectFailure) { | 575 | 578 | return { __proto__: null, label: undefined, match: expectFailure }; | 576 | 579 | } | 577 | 580 |
| | 581 | +function failForbiddenOnly(root, message) { | | 582 | + // Filtering, TODO, and expected failures must not hide forbidden exclusivity. | | 583 | + root.harness.success = false; | | 584 | + root.diagnostic(message); | | 585 | + process.exitCode = kGenericUserError; | | 586 | + throw new ERR_TEST_FAILURE(message, kTestCodeFailure); | | 587 | +} | | 588 | + | 578 | 589 | class Test extends AsyncResource { | 579 | 590 | reportedType = 'test'; | 580 | 591 | abortController; | @@ -644,6 +655,10 @@ class Test extends AsyncResource { | 644 | 655 | parent.nesting + 1; | 645 | 656 | const { config, isFilteringByName, isFilteringByOnly, isFilteringByTags } = parent.root.harness; | 646 | 657 |
| | 658 | + if (only && config.forbidOnly) { | | 659 | + failForbiddenOnly(parent.root, 'The only option is not allowed when forbidOnly is enabled'); | | 660 | + } | | 661 | + | 647 | 662 | this.root = parent.root; | 648 | 663 | this.harness = null; | 649 | 664 | this.config = config; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/utils.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -270,6 +270,7 @@ function parseCommandLine() { | 270 | 270 | let destinations; | 271 | 271 | let isolation; | 272 | 272 | let only = getOptionValue('--test-only'); | | 273 | + const forbidOnly = getOptionValue('--test-forbid-only'); | 273 | 274 | let reporters; | 274 | 275 | let shard; | 275 | 276 | let testNamePatterns = mapPatternFlagToRegExArray('--test-name-pattern'); | @@ -424,6 +425,7 @@ function parseCommandLine() { | 424 | 425 | functionCoverage, | 425 | 426 | lineCoverage, | 426 | 427 | only, | | 428 | + forbidOnly, | 427 | 429 | reporters, | 428 | 430 | setup, | 429 | 431 | globalSetupPath, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1267,6 +1267,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 1267 | 1267 | kAllowedInEnvvar, | 1268 | 1268 | false, | 1269 | 1269 | OptionNamespaces::kTestRunnerNamespace); | | 1270 | + AddOption("--test-forbid-only", | | 1271 | + "fail tests that use the 'only' option", | | 1272 | + BOOL_FIELD(test_forbid_only), | | 1273 | + kAllowedInEnvvar, | | 1274 | + false, | | 1275 | + OptionNamespaces::kTestRunnerNamespace); | 1270 | 1276 | AddOption("--test-shard", | 1271 | 1277 | "run test at specific shard", | 1272 | 1278 | &EnvironmentOptions::test_shard, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -274,6 +274,7 @@ class EnvironmentOptions : public Options { | 274 | 274 | DEFINE_BOOL_FIELD(test_randomize) = false; | 275 | 275 | DEFINE_BOOL_FIELD(has_test_random_seed) = false; | 276 | 276 | DEFINE_BOOL_FIELD(test_only) = false; | | 277 | + DEFINE_BOOL_FIELD(test_forbid_only) = false; | 277 | 278 | DEFINE_BOOL_FIELD(test_udp_no_try_send) = false; | 278 | 279 | DEFINE_BOOL_FIELD(coverage_include_all) = false; | 279 | 280 | DEFINE_BOOL_FIELD(throw_deprecation) = false; | |
Collapse file Copy file name to clipboard +153Lines changed: 153 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const assert = require('node:assert'); | | 4 | +const { writeFileSync } = require('node:fs'); | | 5 | +const { test, run } = require('node:test'); | | 6 | +const tmpdir = require('../common/tmpdir'); | | 7 | + | | 8 | +tmpdir.refresh(); | | 9 | +const fixture = tmpdir.resolve('only.test.js'); | | 10 | +writeFileSync(fixture, ` | | 11 | + const { test } = require('node:test'); | | 12 | + test.only('focused test', () => {}); | | 13 | + test('ordinary test', () => {}); | | 14 | +`); | | 15 | +const env = { ...process.env, NODE_OPTIONS: '', FORCE_COLOR: '0' }; | | 16 | +delete env.NODE_TEST_CONTEXT; | | 17 | +const onlyIfNodeOptionsSupport = { skip: process.config.variables.node_without_node_options }; | | 18 | + | | 19 | +function createFixture(name, source) { | | 20 | + const file = tmpdir.resolve(`${name}.js`); | | 21 | + writeFileSync(file, ` | | 22 | + const { test, it, suite, describe } = require('node:test'); | | 23 | + ${source} | | 24 | + `); | | 25 | + return file; | | 26 | +} | | 27 | + | | 28 | +function assertForbidden({ code, signal, stdout, stderr }) { | | 29 | + assert.strictEqual(signal, null); | | 30 | + assert.strictEqual(code, 1, stdout + stderr); | | 31 | + assert.match(stdout + stderr, /(?:only|runOnly).*not allowed/); | | 32 | + assert.doesNotMatch(stdout, /BODY_EXECUTED/); | | 33 | +} | | 34 | + | | 35 | +const cases = { | | 36 | + 'test-only': "test.only('focused test', () => { console.log('BODY_EXECUTED'); });", | | 37 | + 'it-only': "it.only('focused test', () => { console.log('BODY_EXECUTED'); });", | | 38 | + 'suite-only': "suite.only('focused suite', () => { console.log('BODY_EXECUTED'); });", | | 39 | + 'describe-only': "describe.only('focused suite', () => { console.log('BODY_EXECUTED'); });", | | 40 | + 'only-option': "test('focused test', { only: true }, () => { console.log('BODY_EXECUTED'); });", | | 41 | + 'suite-option': "suite('focused suite', { only: true }, () => { console.log('BODY_EXECUTED'); });", | | 42 | + 'subtest-option': "test('parent', async (t) => { await t.test('child', { only: true }, () => {}); });", | | 43 | + 'run-only': "test('parent', (t) => { t.runOnly(true); console.log('BODY_EXECUTED'); });", | | 44 | + 'skipped-only': "test('focused test', { only: true, skip: true }, () => {});", | | 45 | + 'todo-only': "test('focused test', { only: true, todo: true }, () => {});", | | 46 | + 'expected-failure-only': "test('focused test', { only: true, expectFailure: true }, () => {});", | | 47 | + 'todo-parent': "test.todo('parent', () => { test.only('child', () => {}); });", | | 48 | + 'expected-failure-parent': "test('parent', { expectFailure: true }, (t) => { t.runOnly(true); });", | | 49 | +}; | | 50 | +const caseFiles = Object.entries(cases).map(([name, source]) => [name, createFixture(name, source)]); | | 51 | + | | 52 | +for (const isolation of ['process', 'none']) { | | 53 | + test(`run() rejects only with isolation=${isolation}`, async () => { | | 54 | + const runner = tmpdir.resolve(`runner-${isolation}.js`); | | 55 | + writeFileSync(runner, ` | | 56 | + const { run } = require('node:test'); | | 57 | + const { tap } = require('node:test/reporters'); | | 58 | + run({ | | 59 | + files: [${JSON.stringify(fixture)}], | | 60 | + isolation: ${JSON.stringify(isolation)}, | | 61 | + forbidOnly: true, | | 62 | + }).on('test:fail', () => { process.exitCode = 1; }).compose(tap).pipe(process.stdout); | | 63 | + `); | | 64 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [runner], { env }); | | 65 | + assertForbidden({ code, signal, stdout, stderr }); | | 66 | + }); | | 67 | + | | 68 | + for (const [name, file] of caseFiles) { | | 69 | + test(`CLI rejects ${name} with isolation=${isolation}`, async () => { | | 70 | + assertForbidden(await common.spawnPromisified(process.execPath, [ | | 71 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-forbid-only', file, | | 72 | + ], { env })); | | 73 | + }); | | 74 | + } | | 75 | + | | 76 | + for (const args of [['--test-only'], ['--test-name-pattern=ordinary'], ['--test-skip-pattern=focused']]) { | | 77 | + test(`filter ${args[0]} cannot hide only with isolation=${isolation}`, async () => { | | 78 | + assertForbidden(await common.spawnPromisified(process.execPath, [ | | 79 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-forbid-only', ...args, fixture, | | 80 | + ], { env })); | | 81 | + }); | | 82 | + } | | 83 | + | | 84 | + test(`NODE_OPTIONS enables forbidOnly with isolation=${isolation}`, onlyIfNodeOptionsSupport, async () => { | | 85 | + assertForbidden(await common.spawnPromisified(process.execPath, [ | | 86 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', fixture, | | 87 | + ], { env: { ...env, NODE_OPTIONS: '--test-forbid-only' } })); | | 88 | + }); | | 89 | + | | 90 | + test(`filtered suites cannot hide only with isolation=${isolation}`, async () => { | | 91 | + const file = createFixture(`filtered-${isolation}`, ` | | 92 | + suite('filtered', () => { it.only('child', () => {}); }); | | 93 | + test('ordinary', () => {}); | | 94 | + `); | | 95 | + assertForbidden(await common.spawnPromisified(process.execPath, [ | | 96 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', | | 97 | + '--test-forbid-only', '--test-name-pattern=ordinary', file, | | 98 | + ], { env })); | | 99 | + }); | | 100 | + | | 101 | + for (const source of ['CLI', 'NODE_OPTIONS']) { | | 102 | + const options = source === 'NODE_OPTIONS' ? onlyIfNodeOptionsSupport : {}; | | 103 | + test(`run() can override ${source} with forbidOnly=false and isolation=${isolation}`, options, async () => { | | 104 | + const runner = createFixture(`override-${source}-${isolation}`, ` | | 105 | + const { run } = require('node:test'); | | 106 | + const { tap } = require('node:test/reporters'); | | 107 | + run({ files: [${JSON.stringify(fixture)}], isolation: ${JSON.stringify(isolation)}, forbidOnly: false }) | | 108 | + .on('test:fail', () => { process.exitCode = 1; }).compose(tap).pipe(process.stdout); | | 109 | + `); | | 110 | + const args = source === 'CLI' ? ['--test-forbid-only', runner] : [runner]; | | 111 | + const childEnv = source === 'NODE_OPTIONS' ? { ...env, NODE_OPTIONS: '--test-forbid-only' } : env; | | 112 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, args, { env: childEnv }); | | 113 | + assert.strictEqual(signal, null); | | 114 | + assert.strictEqual(code, 0, stdout + stderr); | | 115 | + assert.match(stdout, /ok 1 - focused test/); | | 116 | + }); | | 117 | + } | | 118 | + | | 119 | + test(`ordinary tests still run with isolation=${isolation}`, async () => { | | 120 | + const file = createFixture(`ordinary-${isolation}`, ` | | 121 | + test('ordinary', () => {}); | | 122 | + test('explicit false', { only: false }, () => {}); | | 123 | + test('runOnly false', (t) => { t.runOnly(false); }); | | 124 | + `); | | 125 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [ | | 126 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-forbid-only', file, | | 127 | + ], { env }); | | 128 | + assert.strictEqual(signal, null); | | 129 | + assert.strictEqual(code, 0, stdout + stderr); | | 130 | + // Explicit only:false is filtered out in the existing none-isolation mode. | | 131 | + assert.match(stdout, isolation === 'none' ? /# pass 2/ : /# pass 3/); | | 132 | + }); | | 133 | + | | 134 | + for (const flags of [[], ['--test-forbid-only', '--no-test-forbid-only']]) { | | 135 | + test(`only still filters when disabled (${flags}) with isolation=${isolation}`, async () => { | | 136 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [ | | 137 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-only', ...flags, fixture, | | 138 | + ], { env }); | | 139 | + assert.strictEqual(signal, null); | | 140 | + assert.strictEqual(code, 0, stdout + stderr); | | 141 | + assert.match(stdout, /# pass 1/); | | 142 | + assert.doesNotMatch(stdout, /ordinary test/); | | 143 | + }); | | 144 | + } | | 145 | +} | | 146 | + | | 147 | +test('run() validates forbidOnly', () => { | | 148 | + for (const forbidOnly of [0, 1, '', 'true', {}, []]) { | | 149 | + assert.throws(() => run({ forbidOnly }), { | | 150 | + code: 'ERR_INVALID_ARG_TYPE', | | 151 | + }); | | 152 | + } | | 153 | +}); |
|