test_runner: support running tests in process

This commit introduces a new --experimental-test-isolation flag
that, when set to 'none', causes the test runner to execute all
tests in the same process. By default, this is the main test
runner process, but if watch mode is enabled, it spawns a separate
process that runs all of the tests.

The default value of the new flag is 'process', which uses the
existing behavior of running each test file in its own child
process.

It is worth noting that when the isolation mode is 'none', globals
and all other top level logic (such as top level before() and after()
hooks) is shared among all files.

Co-authored-by: Moshe Atlow <moshe@atlow.co.il>
PR-URL: https://github.com/nodejs/node/pull/53927
Fixes: https://github.com/nodejs/node/issues/51548
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
cjihrigandMoshe Atlow authored and RafaelGSS committed 2024-08-30 15:52:06 -03:00
1 parent 858b583c88
commit 97fa075c2e
22 files changed
+745 -205

No files matched your search

@@ -24,3 +24,17 @@ test('concurrency of two', async () => {
const cp = spawnSync(process.execPath, args, { cwd, env });
assert.match(cp.stderr.toString(), /concurrency: 2,/);
});
test('isolation=none uses a concurrency of one', async () => {
const args = ['--test', '--experimental-test-isolation=none'];
const cp = spawnSync(process.execPath, args, { cwd, env });
assert.match(cp.stderr.toString(), /concurrency: 1,/);
});
test('isolation=none overrides --test-concurrency', async () => {
const args = [
'--test', '--experimental-test-isolation=none', '--test-concurrency=2',
];
const cp = spawnSync(process.execPath, args, { cwd, env });
assert.match(cp.stderr.toString(), /concurrency: 1,/);
});
+8
View File
@@ -18,3 +18,11 @@ test('timeout of 10ms', async () => {
const cp = spawnSync(process.execPath, args, { cwd, env });
assert.match(cp.stderr.toString(), /timeout: 10,/);
});
test('isolation=none uses the --test-timeout flag', async () => {
const args = [
'--test', '--experimental-test-isolation=none', '--test-timeout=10',
];
const cp = spawnSync(process.execPath, args, { cwd, env });
assert.match(cp.stderr.toString(), /timeout: 10,/);
});
+145 -126
View File
@@ -7,92 +7,162 @@ const { join } = require('path');
const fixtures = require('../common/fixtures');
const testFixtures = fixtures.path('test-runner');
{
// File not found.
const args = ['--test', 'a-random-file-that-does-not-exist.js'];
const child = spawnSync(process.execPath, args);
for (const isolation of ['none', 'process']) {
{
// File not found.
const args = [
'--test',
`--experimental-test-isolation=${isolation}`,
'a-random-file-that-does-not-exist.js',
];
const child = spawnSync(process.execPath, args);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stdout.toString(), '');
assert.match(child.stderr.toString(), /^Could not find/);
}
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stdout.toString(), '');
assert.match(child.stderr.toString(), /^Could not find/);
}
{
// Default behavior. node_modules is ignored. Files that don't match the
// pattern are ignored except in test/ directories.
const args = ['--test'];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') });
{
// Default behavior. node_modules is ignored. Files that don't match the
// pattern are ignored except in test/ directories.
const args = ['--test', `--experimental-test-isolation=${isolation}`];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') });
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /not ok 2 - this should fail/);
assert.match(stdout, /ok 3 - subdir.+subdir_test\.js/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should be skipped/);
assert.match(stdout, /ok 6 - this should be executed/);
}
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
{
// Same but with a prototype mutation in require scripts.
const args = ['--require', join(testFixtures, 'protoMutation.js'), '--test'];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') });
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /not ok 2 - this should fail/);
assert.match(stdout, /ok 3 - subdir.+subdir_test\.js/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should be skipped/);
assert.match(stdout, /ok 6 - this should be executed/);
}
const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /not ok 2 - this should fail/);
assert.match(stdout, /ok 3 - subdir.+subdir_test\.js/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should be skipped/);
assert.match(stdout, /ok 6 - this should be executed/);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
}
{
// Same but with a prototype mutation in require scripts.
const args = [
'--require', join(testFixtures, 'protoMutation.js'),
'--test',
`--experimental-test-isolation=${isolation}`,
];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'default-behavior') });
{
// User specified files that don't match the pattern are still run.
const args = ['--test', join(testFixtures, 'index.js')];
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /not ok 2 - this should fail/);
assert.match(stdout, /ok 3 - subdir.+subdir_test\.js/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should be skipped/);
assert.match(stdout, /ok 6 - this should be executed/);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
}
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /not ok 1 - .+index\.js/);
}
{
// User specified files that don't match the pattern are still run.
const args = [
'--test',
`--experimental-test-isolation=${isolation}`,
join(testFixtures, 'index.js'),
];
const child = spawnSync(process.execPath, args, { cwd: testFixtures });
{
// Searches node_modules if specified.
const args = ['--test', join(testFixtures, 'default-behavior/node_modules/*.js')];
const child = spawnSync(process.execPath, args);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /not ok 1 - .+index\.js/);
}
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /not ok 1 - .+test-nm\.js/);
}
{
// Searches node_modules if specified.
const args = [
'--test',
`--experimental-test-isolation=${isolation}`,
join(testFixtures, 'default-behavior/node_modules/*.js'),
];
const child = spawnSync(process.execPath, args);
{
// The current directory is used by default.
const args = ['--test'];
const options = { cwd: join(testFixtures, 'default-behavior') };
const child = spawnSync(process.execPath, args, options);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /not ok 1 - .+test-nm\.js/);
}
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /not ok 2 - this should fail/);
assert.match(stdout, /ok 3 - subdir.+subdir_test\.js/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should be skipped/);
assert.match(stdout, /ok 6 - this should be executed/);
{
// The current directory is used by default.
const args = ['--test', `--experimental-test-isolation=${isolation}`];
const options = { cwd: join(testFixtures, 'default-behavior') };
const child = spawnSync(process.execPath, args, options);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /not ok 2 - this should fail/);
assert.match(stdout, /ok 3 - subdir.+subdir_test\.js/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should be skipped/);
assert.match(stdout, /ok 6 - this should be executed/);
}
{
// Test combined stream outputs
const args = [
'--test',
`--experimental-test-isolation=${isolation}`,
'test/fixtures/test-runner/default-behavior/index.test.js',
'test/fixtures/test-runner/nested.js',
'test/fixtures/test-runner/invalid-tap.js',
];
const child = spawnSync(process.execPath, args);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /# Subtest: this should pass/);
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, / {2}---/);
assert.match(stdout, / {2}duration_ms: .*/);
assert.match(stdout, / {2}\.\.\./);
assert.match(stdout, /# Subtest: .+invalid-tap\.js/);
assert.match(stdout, /invalid tap output/);
assert.match(stdout, /ok 2 - .+invalid-tap\.js/);
assert.match(stdout, /# Subtest: level 0a/);
assert.match(stdout, / {4}# Subtest: level 1a/);
assert.match(stdout, / {4}ok 1 - level 1a/);
assert.match(stdout, / {4}# Subtest: level 1b/);
assert.match(stdout, / {4}not ok 2 - level 1b/);
assert.match(stdout, / {6}code: 'ERR_TEST_FAILURE'/);
assert.match(stdout, / {6}stack: |-'/);
assert.match(stdout, / {8}TestContext\.<anonymous> .*/);
assert.match(stdout, / {4}# Subtest: level 1c/);
assert.match(stdout, / {4}ok 3 - level 1c # SKIP aaa/);
assert.match(stdout, / {4}# Subtest: level 1d/);
assert.match(stdout, / {4}ok 4 - level 1d/);
assert.match(stdout, /not ok 3 - level 0a/);
assert.match(stdout, / {2}error: '1 subtest failed'/);
assert.match(stdout, /# Subtest: level 0b/);
assert.match(stdout, /not ok 4 - level 0b/);
assert.match(stdout, / {2}error: 'level 0b error'/);
assert.match(stdout, /# tests 8/);
assert.match(stdout, /# suites 0/);
assert.match(stdout, /# pass 4/);
assert.match(stdout, /# fail 3/);
assert.match(stdout, /# cancelled 0/);
assert.match(stdout, /# skipped 1/);
assert.match(stdout, /# todo 0/);
}
}
{
@@ -115,57 +185,6 @@ const testFixtures = fixtures.path('test-runner');
}
}
{
// Test combined stream outputs
const args = [
'--test',
'test/fixtures/test-runner/default-behavior/index.test.js',
'test/fixtures/test-runner/nested.js',
'test/fixtures/test-runner/invalid-tap.js',
];
const child = spawnSync(process.execPath, args);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();
assert.match(stdout, /# Subtest: this should pass/);
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, / {2}---/);
assert.match(stdout, / {2}duration_ms: .*/);
assert.match(stdout, / {2}\.\.\./);
assert.match(stdout, /# Subtest: .+invalid-tap\.js/);
assert.match(stdout, /# invalid tap output/);
assert.match(stdout, /ok 2 - .+invalid-tap\.js/);
assert.match(stdout, /# Subtest: level 0a/);
assert.match(stdout, / {4}# Subtest: level 1a/);
assert.match(stdout, / {4}ok 1 - level 1a/);
assert.match(stdout, / {4}# Subtest: level 1b/);
assert.match(stdout, / {4}not ok 2 - level 1b/);
assert.match(stdout, / {6}code: 'ERR_TEST_FAILURE'/);
assert.match(stdout, / {6}stack: |-'/);
assert.match(stdout, / {8}TestContext\.<anonymous> .*/);
assert.match(stdout, / {4}# Subtest: level 1c/);
assert.match(stdout, / {4}ok 3 - level 1c # SKIP aaa/);
assert.match(stdout, / {4}# Subtest: level 1d/);
assert.match(stdout, / {4}ok 4 - level 1d/);
assert.match(stdout, /not ok 3 - level 0a/);
assert.match(stdout, / {2}error: '1 subtest failed'/);
assert.match(stdout, /# Subtest: level 0b/);
assert.match(stdout, /not ok 4 - level 0b/);
assert.match(stdout, / {2}error: 'level 0b error'/);
assert.match(stdout, /# tests 8/);
assert.match(stdout, /# suites 0/);
assert.match(stdout, /# pass 4/);
assert.match(stdout, /# fail 3/);
assert.match(stdout, /# cancelled 0/);
assert.match(stdout, /# skipped 1/);
assert.match(stdout, /# todo 0/);
}
{
// Test user logging in tests.
const args = [
+38
View File
@@ -187,6 +187,44 @@ test('coverage is combined for multiple processes', skipIfNoInspector, () => {
assert.strictEqual(result.status, 0);
});
test('coverage works with isolation=none', skipIfNoInspector, () => {
// There is a bug in coverage calculation. The branch % in the common.js
// fixture is different depending on the test isolation mode. The 'none' mode
// is closer to what c8 reports here, so the bug is likely in the code that
// merges reports from different processes.
let report = [
'# start of coverage report',
'# -------------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# -------------------------------------------------------------------',
'# common.js | 89.86 | 68.42 | 100.00 | 8 13-14 18 34-35 53',
'# first.test.js | 83.33 | 100.00 | 50.00 | 5-6',
'# second.test.js | 100.00 | 100.00 | 100.00 | ',
'# third.test.js | 100.00 | 100.00 | 100.00 | ',
'# -------------------------------------------------------------------',
'# all files | 92.11 | 76.00 | 88.89 |',
'# -------------------------------------------------------------------',
'# end of coverage report',
].join('\n');
if (common.isWindows) {
report = report.replaceAll('/', '\\');
}
const fixture = fixtures.path('v8-coverage', 'combined_coverage');
const args = [
'--test', '--experimental-test-coverage', '--test-reporter', 'tap', '--experimental-test-isolation=none',
];
const result = spawnSync(process.execPath, args, {
env: { ...process.env, NODE_TEST_TMPDIR: tmpdir.path },
cwd: fixture,
});
assert.strictEqual(result.stderr.toString(), '');
assert(result.stdout.toString().includes(report));
assert.strictEqual(result.status, 0);
});
test('coverage reports on lines, functions, and branches', skipIfNoInspector, async (t) => {
const fixture = fixtures.path('test-runner', 'coverage.js');
const child = spawnSync(process.execPath,
@@ -48,3 +48,21 @@ const { spawnSync } = require('child_process');
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
}
{
const child = spawnSync(process.execPath, [
'--test',
'--experimental-test-isolation=none',
fixtures.path('test-runner', 'async-error-in-test-hook.mjs'),
]);
const stdout = child.stdout.toString();
assert.match(stdout, /^# Error: Test hook "before" at .+async-error-in-test-hook\.mjs:3:1 generated asynchronous activity after the test ended/m);
assert.match(stdout, /^# Error: Test hook "beforeEach" at .+async-error-in-test-hook\.mjs:9:1 generated asynchronous activity after the test ended/m);
assert.match(stdout, /^# Error: Test hook "after" at .+async-error-in-test-hook\.mjs:15:1 generated asynchronous activity after the test ended/m);
assert.match(stdout, /^# Error: Test hook "afterEach" at .+async-error-in-test-hook\.mjs:21:1 generated asynchronous activity after the test ended/m);
assert.match(stdout, /^# pass 1$/m);
assert.match(stdout, /^# fail 0$/m);
assert.match(stdout, /^# cancelled 0$/m);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
}
@@ -4,12 +4,21 @@ const { match, doesNotMatch, strictEqual } = require('node:assert');
const { spawnSync } = require('node:child_process');
const fixtures = require('../common/fixtures');
const fixture = fixtures.path('test-runner/throws_sync_and_async.js');
const r = spawnSync(process.execPath, ['--test', '--test-force-exit', fixture]);
strictEqual(r.status, 1);
strictEqual(r.signal, null);
strictEqual(r.stderr.toString(), '');
for (const isolation of ['none', 'process']) {
const args = [
'--test',
'--test-force-exit',
`--experimental-test-isolation=${isolation}`,
fixture,
];
const r = spawnSync(process.execPath, args);
const stdout = r.stdout.toString();
match(stdout, /error: 'fails'/);
doesNotMatch(stdout, /this should not have a chance to be thrown/);
strictEqual(r.status, 1);
strictEqual(r.signal, null);
strictEqual(r.stderr.toString(), '');
const stdout = r.stdout.toString();
match(stdout, /error: 'fails'/);
doesNotMatch(stdout, /this should not have a chance to be thrown/);
}
@@ -0,0 +1,69 @@
'use strict';
require('../common');
const fixtures = require('../common/fixtures');
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const { test } = require('node:test');
const fixture1 = fixtures.path('test-runner', 'no-isolation', 'one.test.js');
const fixture2 = fixtures.path('test-runner', 'no-isolation', 'two.test.js');
test('works with --test-only', () => {
const args = [
'--test',
'--experimental-test-isolation=none',
'--test-only',
fixture1,
fixture2,
];
const child = spawnSync(process.execPath, args);
const stdout = child.stdout.toString();
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.match(stdout, /# tests 2/);
assert.match(stdout, /# suites 2/);
assert.match(stdout, /# pass 2/);
assert.match(stdout, /ok 1 - suite one/);
assert.match(stdout, /ok 1 - suite one - test/);
assert.match(stdout, /ok 2 - suite two/);
assert.match(stdout, /ok 1 - suite two - test/);
});
test('works with --test-name-pattern', () => {
const args = [
'--test',
'--experimental-test-isolation=none',
'--test-name-pattern=/test one/',
fixture1,
fixture2,
];
const child = spawnSync(process.execPath, args);
const stdout = child.stdout.toString();
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.match(stdout, /# tests 1/);
assert.match(stdout, /# suites 0/);
assert.match(stdout, /# pass 1/);
assert.match(stdout, /ok 1 - test one/);
});
test('works with --test-skip-pattern', () => {
const args = [
'--test',
'--experimental-test-isolation=none',
'--test-skip-pattern=/one/',
fixture1,
fixture2,
];
const child = spawnSync(process.execPath, args);
const stdout = child.stdout.toString();
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.match(stdout, /# tests 1/);
assert.match(stdout, /# suites 1/);
assert.match(stdout, /# pass 1/);
assert.match(stdout, /ok 1 - suite two - test/);
});
@@ -0,0 +1,47 @@
import { allowGlobals, mustCall, mustNotCall } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { deepStrictEqual } from 'node:assert';
import { run } from 'node:test';
const stream = run({
files: [
fixtures.path('test-runner', 'no-isolation', 'one.test.js'),
fixtures.path('test-runner', 'no-isolation', 'two.test.js'),
],
isolation: 'none',
});
stream.on('test:fail', mustNotCall());
stream.on('test:pass', mustCall(5));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
allowGlobals(globalThis.GLOBAL_ORDER);
deepStrictEqual(globalThis.GLOBAL_ORDER, [
'before one: <root>',
'suite one',
'before two: <root>',
'suite two',
'beforeEach one: suite one - test',
'beforeEach two: suite one - test',
'suite one - test',
'afterEach one: suite one - test',
'afterEach two: suite one - test',
'beforeEach one: test one',
'beforeEach two: test one',
'test one',
'afterEach one: test one',
'afterEach two: test one',
'before suite two: suite two',
'beforeEach one: suite two - test',
'beforeEach two: suite two - test',
'suite two - test',
'afterEach one: suite two - test',
'afterEach two: suite two - test',
'after one: <root>',
'after two: <root>',
]);
@@ -339,3 +339,75 @@ test('t.assert.snapshot()', async (t) => {
t.assert.match(child.stdout, /fail 0/);
});
});
test('snapshots from multiple files (isolation=none)', async (t) => {
tmpdir.refresh();
const fixture = fixtures.path('test-runner', 'snapshots', 'unit.js');
const fixture2 = fixtures.path('test-runner', 'snapshots', 'unit-2.js');
await t.test('fails prior to snapshot generation', async (t) => {
const args = [
'--test',
'--experimental-test-isolation=none',
'--experimental-test-snapshots',
fixture,
fixture2,
];
const child = await common.spawnPromisified(
process.execPath,
args,
{ cwd: tmpdir.path },
);
t.assert.strictEqual(child.code, 1);
t.assert.strictEqual(child.signal, null);
t.assert.match(child.stdout, /# tests 6/);
t.assert.match(child.stdout, /# pass 0/);
t.assert.match(child.stdout, /# fail 6/);
t.assert.match(child.stdout, /Missing snapshots/);
});
await t.test('passes when regenerating snapshots', async (t) => {
const args = [
'--test',
'--experimental-test-isolation=none',
'--experimental-test-snapshots',
'--test-update-snapshots',
fixture,
fixture2,
];
const child = await common.spawnPromisified(
process.execPath,
args,
{ cwd: tmpdir.path },
);
t.assert.strictEqual(child.code, 0);
t.assert.strictEqual(child.signal, null);
t.assert.match(child.stdout, /tests 6/);
t.assert.match(child.stdout, /pass 6/);
t.assert.match(child.stdout, /fail 0/);
});
await t.test('passes when snapshots exist', async (t) => {
const args = [
'--test',
'--experimental-test-isolation=none',
'--experimental-test-snapshots',
fixture,
fixture2,
];
const child = await common.spawnPromisified(
process.execPath,
args,
{ cwd: tmpdir.path },
);
t.assert.strictEqual(child.code, 0);
t.assert.strictEqual(child.signal, null);
t.assert.match(child.stdout, /tests 6/);
t.assert.match(child.stdout, /pass 6/);
t.assert.match(child.stdout, /fail 0/);
});
});