Skip to content

Commit db62068

Browse files
committed
test: fix tests
1 parent 4c8e03d commit db62068

2 files changed

Lines changed: 68 additions & 38 deletions

File tree

‎lib/internal/test_runner/runner.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,11 @@ function run(options = kEmptyObject) {
710710

711711
if (isolation === 'none') {
712712
if (testNamePatterns && !globalOptions.testNamePatterns) {
713-
globalOptions.testNamePatterns = testNamePatterns
713+
globalOptions.testNamePatterns = testNamePatterns;
714714
}
715715

716716
if (testSkipPatterns && !globalOptions.testSkipPatterns) {
717-
globalOptions.testSkipPatterns = testSkipPatterns
717+
globalOptions.testSkipPatterns = testSkipPatterns;
718718
}
719719
}
720720

‎test/parallel/test-runner-run.mjs‎

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@ import { describe, it, run } from 'node:test';
55
import { dot, spec, tap } from 'node:test/reporters';
66
import assert from 'node:assert';
77
import util from 'node:util';
8+
import { spawn } from 'node:child_process';
9+
10+
import { writeFileSync, unlinkSync } from 'node:fs';
11+
import tmpdir from '../common/tmpdir.js';
12+
import { randomBytes } from 'node:crypto';
13+
14+
async function runTestInSubprocess(testFile, options = {}) {
15+
tmpdir.refresh();
16+
17+
const tmpFile = tmpdir.resolve(`test-subprocess-${randomBytes(8).toString('hex')}.mjs`);
18+
19+
const testNamePatterns = options.testNamePatterns ? JSON.stringify(options.testNamePatterns) : undefined;
20+
const testSkipPatterns = options.testSkipPatterns ? JSON.stringify(options.testSkipPatterns) : undefined;
21+
const isolation = options.isolation ? JSON.stringify(options.isolation) : undefined;
22+
23+
const code = `
24+
import { run } from 'node:test';
25+
import { tap } from 'node:test/reporters';
26+
import { join } from 'node:path';
27+
const files = [${JSON.stringify(testFile)}];
28+
const opts = {
29+
files,
30+
reporter: 'tap',
31+
${testNamePatterns ? `testNamePatterns: ${testNamePatterns},` : ''}
32+
${testSkipPatterns ? `testSkipPatterns: ${testSkipPatterns},` : ''}
33+
${isolation ? `isolation: ${isolation},` : ''}
34+
};
35+
const result = await run(opts).compose(tap).toArray();
36+
for (const line of result) {
37+
process.stdout.write(typeof line === 'string' ? line : String(line));
38+
}
39+
`;
40+
writeFileSync(tmpFile, code);
41+
return new Promise((resolve, reject) => {
42+
const child = spawn(process.execPath, [tmpFile], { stdio: ['ignore', 'pipe', 'pipe'] });
43+
let stdout = '';
44+
let stderr = '';
45+
child.stdout.on('data', (data) => { stdout += data; });
46+
child.stderr.on('data', (data) => { stderr += data; });
47+
child.on('close', (code) => {
48+
unlinkSync(tmpFile);
49+
if (code !== 0) {
50+
return reject(new Error(`Test failed with exit code ${code}\n${stderr}`));
51+
}
52+
resolve(stdout);
53+
});
54+
});
55+
}
856

957
const testFixtures = fixtures.path('test-runner');
1058

@@ -206,7 +254,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
206254
files: [join(testFixtures, 'default-behavior/test/random.cjs')],
207255
watch: true,
208256
signal: controller.signal,
209-
}).on('data', function ({ type }) {
257+
}).on('data', function({ type }) {
210258
if (type === 'test:watch:drained') {
211259
controller.abort();
212260
}
@@ -650,45 +698,27 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
650698
});
651699
});
652700

653-
describe("with isolation set to 'none'",() => {
701+
describe("with isolation set to 'none'", () => {
654702
it('should skip tests not matching testNamePatterns - string', async () => {
655-
const result = await run({
656-
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
657-
testNamePatterns: ['executed'],
658-
isolation: 'none',
659-
})
660-
.compose(tap)
661-
.toArray();
662-
assert.strictEqual(result[2], 'ok 1 - this should be executed\n');
663-
assert.strictEqual(result[4], '1..1\n');
664-
assert.strictEqual(result[5], '# tests 1\n');
665-
});
666-
667-
it('should skip tests not matching testNamePatterns - RegExp', async () => {
668-
const result = await run({
669-
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
670-
testNamePatterns: [/executed/],
671-
isolation: 'none',
672-
})
673-
.compose(tap)
674-
.toArray();
675-
assert.strictEqual(result[2], 'ok 1 - this should be executed\n');
676-
assert.strictEqual(result[4], '1..1\n');
677-
assert.strictEqual(result[5], '# tests 1\n');
703+
const result = await runTestInSubprocess(
704+
join(testFixtures, 'default-behavior/test/skip_by_name.cjs'),
705+
{ isolation: 'none', testNamePatterns: ['executed'] }
706+
);
707+
708+
assert.match(result, /ok 1 - this should be executed/);
709+
assert.match(result, /1\.\.1/);
710+
assert.match(result, /# tests 1/);
678711
});
679712

680713
it('should skip tests matching testSkipPatterns', async () => {
681-
const result = await run({
682-
files: [join(testFixtures, 'default-behavior/test/skip_by_name.cjs')],
683-
testSkipPatterns: ['skipped'],
684-
isolation: 'none',
685-
})
686-
.compose(tap)
687-
.toArray();
688-
689-
assert.strictEqual(result[2], 'ok 1 - this should be executed\n');
690-
assert.strictEqual(result[4], '1..1\n');
691-
assert.strictEqual(result[5], '# tests 1\n');
714+
const result = await runTestInSubprocess(
715+
join(testFixtures, 'default-behavior/test/skip_by_name.cjs'),
716+
{ isolation: 'none', testSkipPatterns: ['skipped'] }
717+
);
718+
719+
assert.match(result, /ok 1 - this should be executed/);
720+
assert.match(result, /1\.\.1/);
721+
assert.match(result, /# tests 1/);
692722
});
693723
});
694724

0 commit comments

Comments
 (0)