|
| 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 | + |
| 18 | +function createFixture(name, source) { |
| 19 | + const file = tmpdir.resolve(`${name}.js`); |
| 20 | + writeFileSync(file, ` |
| 21 | + const { test, it, suite, describe } = require('node:test'); |
| 22 | + ${source} |
| 23 | + `); |
| 24 | + return file; |
| 25 | +} |
| 26 | + |
| 27 | +function assertForbidden({ code, signal, stdout, stderr }) { |
| 28 | + assert.strictEqual(signal, null); |
| 29 | + assert.strictEqual(code, 1, stdout + stderr); |
| 30 | + assert.match(stdout + stderr, /(?:only|runOnly).*not allowed/); |
| 31 | + assert.doesNotMatch(stdout, /BODY_EXECUTED/); |
| 32 | +} |
| 33 | + |
| 34 | +const cases = { |
| 35 | + 'test-only': "test.only('focused test', () => { console.log('BODY_EXECUTED'); });", |
| 36 | + 'it-only': "it.only('focused test', () => { console.log('BODY_EXECUTED'); });", |
| 37 | + 'suite-only': "suite.only('focused suite', () => { console.log('BODY_EXECUTED'); });", |
| 38 | + 'describe-only': "describe.only('focused suite', () => { console.log('BODY_EXECUTED'); });", |
| 39 | + 'only-option': "test('focused test', { only: true }, () => { console.log('BODY_EXECUTED'); });", |
| 40 | + 'suite-option': "suite('focused suite', { only: true }, () => { console.log('BODY_EXECUTED'); });", |
| 41 | + 'subtest-option': "test('parent', async (t) => { await t.test('child', { only: true }, () => {}); });", |
| 42 | + 'run-only': "test('parent', (t) => { t.runOnly(true); console.log('BODY_EXECUTED'); });", |
| 43 | + 'skipped-only': "test('focused test', { only: true, skip: true }, () => {});", |
| 44 | + 'todo-only': "test('focused test', { only: true, todo: true }, () => {});", |
| 45 | + 'expected-failure-only': "test('focused test', { only: true, expectFailure: true }, () => {});", |
| 46 | + 'todo-parent': "test.todo('parent', () => { test.only('child', () => {}); });", |
| 47 | + 'expected-failure-parent': "test('parent', { expectFailure: true }, (t) => { t.runOnly(true); });", |
| 48 | +}; |
| 49 | +const caseFiles = Object.entries(cases).map(([name, source]) => [name, createFixture(name, source)]); |
| 50 | + |
| 51 | +for (const isolation of ['process', 'none']) { |
| 52 | + test(`run() rejects only with isolation=${isolation}`, async () => { |
| 53 | + const runner = tmpdir.resolve(`runner-${isolation}.js`); |
| 54 | + writeFileSync(runner, ` |
| 55 | + const { run } = require('node:test'); |
| 56 | + const { tap } = require('node:test/reporters'); |
| 57 | + run({ |
| 58 | + files: [${JSON.stringify(fixture)}], |
| 59 | + isolation: ${JSON.stringify(isolation)}, |
| 60 | + forbidOnly: true, |
| 61 | + }).on('test:fail', () => { process.exitCode = 1; }).compose(tap).pipe(process.stdout); |
| 62 | + `); |
| 63 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [runner], { env }); |
| 64 | + assertForbidden({ code, signal, stdout, stderr }); |
| 65 | + }); |
| 66 | + |
| 67 | + for (const [name, file] of caseFiles) { |
| 68 | + test(`CLI rejects ${name} with isolation=${isolation}`, async () => { |
| 69 | + assertForbidden(await common.spawnPromisified(process.execPath, [ |
| 70 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-forbid-only', file, |
| 71 | + ], { env })); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + for (const args of [['--test-only'], ['--test-name-pattern=ordinary'], ['--test-skip-pattern=focused']]) { |
| 76 | + test(`filter ${args[0]} cannot hide only with isolation=${isolation}`, async () => { |
| 77 | + assertForbidden(await common.spawnPromisified(process.execPath, [ |
| 78 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-forbid-only', ...args, fixture, |
| 79 | + ], { env })); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + test(`NODE_OPTIONS enables forbidOnly with isolation=${isolation}`, async () => { |
| 84 | + assertForbidden(await common.spawnPromisified(process.execPath, [ |
| 85 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', fixture, |
| 86 | + ], { env: { ...env, NODE_OPTIONS: '--test-forbid-only' } })); |
| 87 | + }); |
| 88 | + |
| 89 | + test(`filtered suites cannot hide only with isolation=${isolation}`, async () => { |
| 90 | + const file = createFixture(`filtered-${isolation}`, ` |
| 91 | + suite('filtered', () => { it.only('child', () => {}); }); |
| 92 | + test('ordinary', () => {}); |
| 93 | + `); |
| 94 | + assertForbidden(await common.spawnPromisified(process.execPath, [ |
| 95 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', |
| 96 | + '--test-forbid-only', '--test-name-pattern=ordinary', file, |
| 97 | + ], { env })); |
| 98 | + }); |
| 99 | + |
| 100 | + test(`run() can override the CLI with forbidOnly=false and isolation=${isolation}`, async () => { |
| 101 | + const runner = createFixture(`override-${isolation}`, ` |
| 102 | + const { run } = require('node:test'); |
| 103 | + const { tap } = require('node:test/reporters'); |
| 104 | + run({ files: [${JSON.stringify(fixture)}], isolation: ${JSON.stringify(isolation)}, forbidOnly: false }) |
| 105 | + .on('test:fail', () => { process.exitCode = 1; }).compose(tap).pipe(process.stdout); |
| 106 | + `); |
| 107 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [ |
| 108 | + runner, |
| 109 | + ], { env: { ...env, NODE_OPTIONS: '--test-forbid-only' } }); |
| 110 | + assert.strictEqual(signal, null); |
| 111 | + assert.strictEqual(code, 0, stdout + stderr); |
| 112 | + assert.match(stdout, /ok 1 - focused test/); |
| 113 | + }); |
| 114 | + |
| 115 | + test(`ordinary tests still run with isolation=${isolation}`, async () => { |
| 116 | + const file = createFixture(`ordinary-${isolation}`, ` |
| 117 | + test('ordinary', () => {}); |
| 118 | + test('explicit false', { only: false }, () => {}); |
| 119 | + test('runOnly false', (t) => { t.runOnly(false); }); |
| 120 | + `); |
| 121 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [ |
| 122 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-forbid-only', file, |
| 123 | + ], { env }); |
| 124 | + assert.strictEqual(signal, null); |
| 125 | + assert.strictEqual(code, 0, stdout + stderr); |
| 126 | + // Explicit only:false is filtered out in the existing none-isolation mode. |
| 127 | + assert.match(stdout, isolation === 'none' ? /# pass 2/ : /# pass 3/); |
| 128 | + }); |
| 129 | + |
| 130 | + for (const flags of [[], ['--test-forbid-only', '--no-test-forbid-only']]) { |
| 131 | + test(`only still filters when disabled (${flags}) with isolation=${isolation}`, async () => { |
| 132 | + const { code, signal, stdout, stderr } = await common.spawnPromisified(process.execPath, [ |
| 133 | + '--test', `--test-isolation=${isolation}`, '--test-reporter=tap', '--test-only', ...flags, fixture, |
| 134 | + ], { env }); |
| 135 | + assert.strictEqual(signal, null); |
| 136 | + assert.strictEqual(code, 0, stdout + stderr); |
| 137 | + assert.match(stdout, /# pass 1/); |
| 138 | + assert.doesNotMatch(stdout, /ordinary test/); |
| 139 | + }); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +test('run() validates forbidOnly', () => { |
| 144 | + for (const forbidOnly of [0, 1, '', 'true', {}, []]) { |
| 145 | + assert.throws(() => run({ forbidOnly }), { |
| 146 | + code: 'ERR_INVALID_ARG_TYPE', |
| 147 | + }); |
| 148 | + } |
| 149 | +}); |
0 commit comments