Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fix timeout in *Each hook failing further tests #48925

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ class SuiteContext {
}

class Test extends AsyncResource {
#abortController;
#outerSignal;
abortController;
outerSignal;
#reportedSubtest;

constructor(options) {
Expand Down Expand Up @@ -263,16 +263,16 @@ class Test extends AsyncResource {
fn = noop;
}

this.#abortController = new AbortController();
this.#outerSignal = signal;
this.signal = this.#abortController.signal;
this.abortController = new AbortController();
this.outerSignal = signal;
this.signal = this.abortController.signal;

validateAbortSignal(signal, 'options.signal');
if (signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
}

this.#outerSignal?.addEventListener(
this.outerSignal?.addEventListener(
'abort',
this.#abortHandler,
{ __proto__: null, [kResistStopPropagation]: true },
Expand Down Expand Up @@ -412,7 +412,7 @@ class Test extends AsyncResource {
}

#abortHandler = () => {
const error = this.#outerSignal?.reason || new AbortError('The test was aborted');
const error = this.outerSignal?.reason || new AbortError('The test was aborted');
error.failureType = kAborted;
this.#cancel(error);
};
Expand All @@ -430,7 +430,7 @@ class Test extends AsyncResource {
);
this.startTime = this.startTime || this.endTime; // If a test was canceled before it was started, e.g inside a hook
this.cancelled = true;
this.#abortController.abort();
this.abortController.abort();
}

createHook(name, fn, options) {
Expand Down Expand Up @@ -498,7 +498,7 @@ class Test extends AsyncResource {
if (this.signal.aborted) {
return true;
}
if (this.#outerSignal?.aborted) {
if (this.outerSignal?.aborted) {
this.#abortHandler();
return true;
}
Expand Down Expand Up @@ -606,7 +606,7 @@ class Test extends AsyncResource {
// Do not abort hooks and the root test as hooks instance are shared between tests suite so aborting them will
// cause them to not run for further tests.
if (this.parent !== null) {
this.#abortController.abort();
this.abortController.abort();
}
}

Expand Down Expand Up @@ -646,7 +646,7 @@ class Test extends AsyncResource {
this.fail(new ERR_TEST_FAILURE(msg, kSubtestsFailed));
}

this.#outerSignal?.removeEventListener('abort', this.#abortHandler);
this.outerSignal?.removeEventListener('abort', this.#abortHandler);
this.mock?.reset();

if (this.parent !== null) {
Expand Down Expand Up @@ -762,6 +762,14 @@ class TestHook extends Test {
super({ __proto__: null, fn, timeout, signal });
}
run(args) {
if (this.error && !this.outerSignal?.aborted) {
this.passed = false;
this.error = null;
this.abortController.abort();
this.abortController = new AbortController();
this.signal = this.abortController.signal;
}

this.#args = args;
return super.run();
}
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/test-runner/output/abort_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Flags: --no-warnings
'use strict';
const { before, beforeEach, describe, it, after, afterEach } = require('node:test');

describe('1 before describe', () => {
const ac = new AbortController();
before(() => {
console.log('before');
ac.abort()
}, {signal: ac.signal});

it('test 1', () => {
console.log('1.1');
});
it('test 2', () => {
console.log('1.2');
});
});

describe('2 after describe', () => {
const ac = new AbortController();
after(() => {
console.log('after');
ac.abort()
}, {signal: ac.signal});

it('test 1', () => {
console.log('2.1');
});
it('test 2', () => {
console.log('2.2');
});
});

describe('3 beforeEach describe', () => {
const ac = new AbortController();
beforeEach(() => {
console.log('beforeEach');
ac.abort()
}, {signal: ac.signal});

it('test 1', () => {
console.log('3.1');
});
it('test 2', () => {
console.log('3.2');
});
});

describe('4 afterEach describe', () => {
const ac = new AbortController();
afterEach(() => {
console.log('afterEach');
ac.abort()
}, {signal: ac.signal});

it('test 1', () => {
console.log('4.1');
});
it('test 2', () => {
console.log('4.2');
});
});
188 changes: 188 additions & 0 deletions test/fixtures/test-runner/output/abort_hooks.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
before
2.1
2.2
after
beforeEach
4.1
afterEach
4.2
TAP version 13
# Subtest: 1 before describe
# Subtest: test 1
not ok 1 - test 1
---
duration_ms: ZERO
failureType: 'cancelledByParent'
error: 'test did not finish before its parent and was cancelled'
code: 'ERR_TEST_FAILURE'
...
# Subtest: test 2
not ok 2 - test 2
---
duration_ms: ZERO
failureType: 'cancelledByParent'
error: 'test did not finish before its parent and was cancelled'
code: 'ERR_TEST_FAILURE'
...
1..2
not ok 1 - 1 before describe
---
duration_ms: *
type: 'suite'
failureType: 'hookFailed'
error: 'This operation was aborted'
code: 20
name: 'AbortError'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2 after describe
# Subtest: test 1
ok 1 - test 1
---
duration_ms: *
...
# Subtest: test 2
ok 2 - test 2
---
duration_ms: *
...
1..2
not ok 2 - 2 after describe
---
duration_ms: *
type: 'suite'
failureType: 'hookFailed'
error: 'This operation was aborted'
code: 20
name: 'AbortError'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 3 beforeEach describe
# Subtest: test 1
not ok 1 - test 1
---
duration_ms: *
failureType: 'hookFailed'
error: 'This operation was aborted'
code: 20
name: 'AbortError'
stack: |-
*
*
*
*
*
*
*
*
*
async Promise.all (index 0)
...
# Subtest: test 2
not ok 2 - test 2
---
duration_ms: *
failureType: 'hookFailed'
error: 'This operation was aborted'
code: 20
name: 'AbortError'
stack: |-
*
*
*
*
*
*
*
*
*
async Promise.all (index 0)
...
1..2
not ok 3 - 3 beforeEach describe
---
duration_ms: *
type: 'suite'
failureType: 'subtestsFailed'
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: 4 afterEach describe
# Subtest: test 1
not ok 1 - test 1
---
duration_ms: *
failureType: 'hookFailed'
error: 'This operation was aborted'
code: 20
name: 'AbortError'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: test 2
not ok 2 - test 2
---
duration_ms: *
failureType: 'hookFailed'
error: 'This operation was aborted'
code: 20
name: 'AbortError'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
1..2
not ok 4 - 4 afterEach describe
---
duration_ms: *
type: 'suite'
failureType: 'subtestsFailed'
error: '2 subtests failed'
code: 'ERR_TEST_FAILURE'
...
1..4
# tests 8
# suites 4
# pass 2
# fail 4
# cancelled 2
# skipped 0
# todo 0
# duration_ms *
4 changes: 0 additions & 4 deletions test/fixtures/test-runner/output/hooks.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ not ok 3 - after throws
*
*
*
async Promise.all (index 0)
*
*
...
1..2
Expand Down Expand Up @@ -183,7 +181,6 @@ not ok 4 - beforeEach throws
*
*
*
async Promise.all (index 0)
*
...
1..2
Expand Down Expand Up @@ -265,7 +262,6 @@ not ok 6 - afterEach when test fails
*
*
*
async Promise.all (index 0)
*
...
1..2
Expand Down
Loading