Skip to content
1 change: 1 addition & 0 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class FileTest extends Test {
column: 1,
file: resolve(this.name),
};
this.timeout = null;
}

#skipReporting() {
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,14 @@ class Test extends AsyncResource {
throw new ERR_INVALID_ARG_TYPE('options.concurrency', ['boolean', 'number'], concurrency);
}

// Take timeout from cli
const cliTimeout = this.config.timeout;
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
if (cliTimeout != null && cliTimeout !== Infinity) {
validateNumber(cliTimeout, 'options.timeout', 0, TIMEOUT_MAX);
this.timeout = cliTimeout;
}

// Take timeout from options
if (timeout != null && timeout !== Infinity) {
Comment thread
cjihrig marked this conversation as resolved.
Comment thread
jakecastelli marked this conversation as resolved.
validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX);
this.timeout = timeout;
Expand Down Expand Up @@ -1394,6 +1402,7 @@ class Suite extends Test {
reportedType = 'suite';
constructor(options) {
super(options);
this.timeout = null;

if (this.config.testNamePatterns !== null &&
this.config.testSkipPatterns !== null &&
Expand Down
4 changes: 1 addition & 3 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function parseCommandLine() {
const sourceMaps = getOptionValue('--enable-source-maps');
const updateSnapshots = getOptionValue('--test-update-snapshots');
const watch = getOptionValue('--watch');
const timeout = getOptionValue('--test-timeout') || Infinity;
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
let concurrency;
Expand All @@ -211,7 +212,6 @@ function parseCommandLine() {
let shard;
let testNamePatterns = mapPatternFlagToRegExArray('--test-name-pattern');
let testSkipPatterns = mapPatternFlagToRegExArray('--test-skip-pattern');
let timeout;

if (isChildProcessV8) {
kBuiltinReporters.set('v8-serializer', 'internal/test_runner/reporter/v8-serializer');
Expand Down Expand Up @@ -242,7 +242,6 @@ function parseCommandLine() {

if (isTestRunner) {
isolation = getOptionValue('--test-isolation');
timeout = getOptionValue('--test-timeout') || Infinity;

if (isolation === 'none') {
concurrency = 1;
Expand Down Expand Up @@ -271,7 +270,6 @@ function parseCommandLine() {
};
}
} else {
timeout = Infinity;
concurrency = 1;
const testNamePatternFlag = getOptionValue('--test-name-pattern');
only = getOptionValue('--test-only');
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/test-runner/output/test-timeout-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --test-timeout=20
'use strict';
const { describe, test, afterEach } = require('node:test');
const { setTimeout } = require('node:timers/promises');

let timeout;

describe('--test-timeout is set to 20ms', () => {
Comment thread
jakecastelli marked this conversation as resolved.
test('should timeout after 20ms', async () => {
timeout = await setTimeout(2000);
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
});
test('should timeout after 5ms', { timeout: 5 }, async () => {
timeout = await setTimeout(2000);
});
test('should not timeout', { timeout: 5000 }, async () => {
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
await setTimeout(200);
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
});
test('should pass', async () => {});

afterEach(() => {
if (timeout) {
Comment thread
jakecastelli marked this conversation as resolved.
Outdated
timeout.unref();
}
});
});
55 changes: 55 additions & 0 deletions test/fixtures/test-runner/output/test-timeout-flag.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
TAP version 13
# Subtest: --test-timeout is set to 20ms
# Subtest: should timeout after 20ms
not ok 1 - should timeout after 20ms
---
duration_ms: *
type: 'test'
location: '/test/fixtures/test-runner/output/test-timeout-flag.js:(LINE):3'
failureType: 'testTimeoutFailure'
error: 'test timed out after 20ms'
code: 'ERR_TEST_FAILURE'
stack: |-
async Promise.all (index 0)
...
# Subtest: should timeout after 5ms
not ok 2 - should timeout after 5ms
---
duration_ms: *
type: 'test'
location: '/test/fixtures/test-runner/output/test-timeout-flag.js:(LINE):3'
failureType: 'testTimeoutFailure'
error: 'test timed out after 5ms'
code: 'ERR_TEST_FAILURE'
...
# Subtest: should not timeout
ok 3 - should not timeout
---
duration_ms: *
type: 'test'
...
# Subtest: should pass
ok 4 - should pass
---
duration_ms: *
type: 'test'
...
1..4
not ok 1 - --test-timeout is set to 20ms
---
duration_ms: *
type: 'suite'
location: '/test/fixtures/test-runner/output/test-timeout-flag.js:(LINE):1'
failureType: 'subtestsFailed'
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
1..1
# tests 4
# suites 1
# pass 2
# fail 0
# cancelled 2
# skipped 0
# todo 0
Comment thread
pmarchini marked this conversation as resolved.
# duration_ms *
9 changes: 9 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ const tests = [
name: 'test-runner/output/timeout_in_before_each_should_not_affect_further_tests.js',
flags: ['--test-reporter=tap'],
},
{
name: 'test-runner/output/test-timeout-flag.js',
flags: ['--test-reporter=tap'],
},
// --test-timeout should work with or without --test flag
{
name: 'test-runner/output/test-timeout-flag.js',
flags: ['--test-reporter=tap', '--test'],
Comment thread
pmarchini marked this conversation as resolved.
},
{
name: 'test-runner/output/hooks-with-no-global-test.js',
flags: ['--test-reporter=tap'],
Expand Down
Loading