Skip to content

Commit 71e2dcf

Browse files
committed
test_runner: use run() options with isolation="none"
When using run() programatically with isolation="none", testNamePatterns, testSkipPattersn, and only were ignored. This combination of options only worked when set via CLI flags, because parseCommandLine() is still used to seed globalOptions.
1 parent 2e5731e commit 71e2dcf

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

lib/internal/test_runner/runner.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,19 @@ function run(options = kEmptyObject) {
784784
cwd,
785785
globalSetupPath,
786786
};
787+
788+
if (isolation === 'none') {
789+
if (testNamePatterns != null) {
790+
globalOptions.testNamePatterns = testNamePatterns;
791+
}
792+
if (testSkipPatterns != null) {
793+
globalOptions.testSkipPatterns = testSkipPatterns;
794+
}
795+
if (only != null) {
796+
globalOptions.only = only;
797+
}
798+
}
799+
787800
const root = createTestTree(rootTestOptions, globalOptions);
788801
let testFiles = files ?? createTestFileList(globPatterns, cwd);
789802
const { isTestRunner } = globalOptions;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { run } from 'node:test';
2+
import { tap } from 'node:test/reporters';
3+
import { parseArgs } from 'node:util';
4+
5+
const {
6+
values,
7+
} = parseArgs({
8+
args: process.argv.slice(2),
9+
options: {
10+
file: { type: 'string' },
11+
only: { type: 'boolean' },
12+
'name-pattern': { type: 'string' },
13+
'skip-pattern': { type: 'string' },
14+
},
15+
});
16+
17+
const opts = {
18+
isolation: 'none',
19+
files: [values.file],
20+
};
21+
22+
if (values.only) {
23+
opts.only = true;
24+
}
25+
if (values['name-pattern']) {
26+
opts.testNamePatterns = [new RegExp(values['name-pattern'])];
27+
}
28+
if (values['skip-pattern']) {
29+
opts.testSkipPatterns = [new RegExp(values['skip-pattern'])];
30+
}
31+
32+
run(opts).compose(tap).pipe(process.stdout);

test/parallel/test-runner-run.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,48 @@ describe('forceExit', () => {
689689
});
690690
});
691691

692+
describe('with isolation="none"', () => {
693+
const isolationNoneFixture = fixtures.path('test-runner', 'test-runner-isolation-none.mjs');
694+
695+
it('should pass only to children', { timeout: 10000 }, async () => {
696+
const child = await common.spawnPromisified(process.execPath, [
697+
isolationNoneFixture,
698+
'--file', join(testFixtures, 'test_only.js'),
699+
'--only',
700+
]);
701+
702+
assert.strictEqual(child.stderr, '');
703+
assert.strictEqual(child.code, 0);
704+
assert.match(child.stdout, /ok 1 - this should be executed/);
705+
assert.match(child.stdout, /# tests 1/);
706+
});
707+
708+
it('should skip tests not matching testNamePatterns - RegExp', { timeout: 10000 }, async () => {
709+
const child = await common.spawnPromisified(process.execPath, [
710+
isolationNoneFixture,
711+
'--file', join(testFixtures, 'default-behavior/test/skip_by_name.cjs'),
712+
'--name-pattern', 'executed',
713+
]);
714+
715+
assert.strictEqual(child.stderr, '');
716+
assert.strictEqual(child.code, 0);
717+
assert.match(child.stdout, /ok 1 - this should be executed/);
718+
assert.match(child.stdout, /# tests 1/);
719+
});
720+
721+
it('should skip tests matching testSkipPatterns - RegExp', { timeout: 10000 }, async () => {
722+
const child = await common.spawnPromisified(process.execPath, [
723+
isolationNoneFixture,
724+
'--file', join(testFixtures, 'default-behavior/test/skip_by_name.cjs'),
725+
'--skip-pattern', 'skipped',
726+
]);
727+
728+
assert.strictEqual(child.stderr, '');
729+
assert.strictEqual(child.code, 0);
730+
assert.match(child.stdout, /ok 1 - this should be executed/);
731+
assert.match(child.stdout, /# tests 1/);
732+
});
733+
});
692734

693735
// exitHandler doesn't run until after the tests / after hooks finish.
694736
process.on('exit', () => {

0 commit comments

Comments
 (0)