Skip to content

Commit f5f36b6

Browse files
committed
abort after all cleanup
1 parent 58da4d2 commit f5f36b6

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

lib/internal/test_runner/test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -586,21 +586,10 @@ class Test extends AsyncResource {
586586
return;
587587
}
588588

589-
// Do not run for hooks and root test as hooks instance are shared between tests suite so aborting them will
590-
// abort cause them to not run for further tests.
591-
if (this.parent !== null) {
592-
this.#abortController.abort();
593-
}
594-
595589
await afterEach();
596590
await after();
597591
this.pass();
598592
} catch (err) {
599-
// Do not run for hooks and root test as hooks instance are shared between tests suite so aborting them will
600-
// abort cause them to not run for further tests.
601-
if (this.parent !== null) {
602-
this.#abortController.abort();
603-
}
604593
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
605594
try { await after(); } catch { /* Ignore error. */ }
606595
if (isTestFailureError(err)) {
@@ -612,6 +601,12 @@ class Test extends AsyncResource {
612601
} else {
613602
this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure));
614603
}
604+
} finally {
605+
// Do not run for hooks and root test as hooks instance are shared between tests suite so aborting them will
606+
// abort cause them to not run for further tests.
607+
if (this.parent !== null) {
608+
this.#abortController.abort();
609+
}
615610
}
616611

617612
// Clean up the test. Then, try to report the results and execute any

test/fixtures/test-runner/aborts/failed-test-still-call-abort.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const {test, afterEach} = require('node:test');
22
const assert = require('node:assert');
3+
const { waitForAbort } = require('./wait-for-abort-helper');
34

45
let testCount = 0;
56
let signal;
67

78
afterEach(() => {
8-
testCount++;
9-
assert.equal(signal.aborted, true);
9+
assert.equal(signal.aborted, false);
1010

11-
console.log(`abort called for test ${testCount}`)
11+
waitForAbort({ testNumber: ++testCount, signal });
1212
});
1313

1414
test("sync", (t) => {

test/fixtures/test-runner/aborts/successful-test-still-call-abort.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const {test, afterEach} = require('node:test');
22
const assert = require('node:assert');
3+
const {waitForAbort} = require("./wait-for-abort-helper");
34

45
let testCount = 0;
56
let signal;
67

78
afterEach(() => {
8-
testCount++;
9-
assert.equal(signal.aborted, true);
9+
assert.equal(signal.aborted, false);
1010

11-
console.log(`abort called for test ${testCount}`)
11+
waitForAbort({ testNumber: ++testCount, signal });
1212
});
1313

1414
test("sync", (t) => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
waitForAbort: function ({ testNumber, signal }) {
3+
let retries = 0;
4+
5+
const interval = setInterval(() => {
6+
retries++;
7+
if(signal.aborted) {
8+
console.log(`abort called for test ${testNumber}`);
9+
clearInterval(interval);
10+
return;
11+
}
12+
13+
if(retries > 100) {
14+
clearInterval(interval);
15+
throw new Error(`abort was not called for test ${testNumber}`);
16+
}
17+
}, 10);
18+
}
19+
}

0 commit comments

Comments
 (0)