diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 06adf1b6840f61..6fea31b684f07b 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -586,21 +586,10 @@ class Test extends AsyncResource { return; } - // Do not run for hooks and root test as hooks instance are shared between tests suite so aborting them will - // abort cause them to not run for further tests. - if (this.parent !== null) { - this.#abortController.abort(); - } - await afterEach(); await after(); this.pass(); } catch (err) { - // Do not run for hooks and root test as hooks instance are shared between tests suite so aborting them will - // abort cause them to not run for further tests. - if (this.parent !== null) { - this.#abortController.abort(); - } try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ } try { await after(); } catch { /* Ignore error. */ } if (isTestFailureError(err)) { @@ -612,6 +601,12 @@ class Test extends AsyncResource { } else { this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure)); } + } finally { + // Do not run for hooks and root test as hooks instance are shared between tests suite so aborting them will + // abort cause them to not run for further tests. + if (this.parent !== null) { + this.#abortController.abort(); + } } // Clean up the test. Then, try to report the results and execute any diff --git a/test/fixtures/test-runner/aborts/failed-test-still-call-abort.js b/test/fixtures/test-runner/aborts/failed-test-still-call-abort.js index dc2bd645b319aa..496d2331422c00 100644 --- a/test/fixtures/test-runner/aborts/failed-test-still-call-abort.js +++ b/test/fixtures/test-runner/aborts/failed-test-still-call-abort.js @@ -1,14 +1,14 @@ const {test, afterEach} = require('node:test'); const assert = require('node:assert'); +const { waitForAbort } = require('./wait-for-abort-helper'); let testCount = 0; let signal; afterEach(() => { - testCount++; - assert.equal(signal.aborted, true); + assert.equal(signal.aborted, false); - console.log(`abort called for test ${testCount}`) + waitForAbort({ testNumber: ++testCount, signal }); }); test("sync", (t) => { diff --git a/test/fixtures/test-runner/aborts/successful-test-still-call-abort.js b/test/fixtures/test-runner/aborts/successful-test-still-call-abort.js index fabafd919d9ac9..4f3879c624de44 100644 --- a/test/fixtures/test-runner/aborts/successful-test-still-call-abort.js +++ b/test/fixtures/test-runner/aborts/successful-test-still-call-abort.js @@ -1,14 +1,14 @@ const {test, afterEach} = require('node:test'); const assert = require('node:assert'); +const {waitForAbort} = require("./wait-for-abort-helper"); let testCount = 0; let signal; afterEach(() => { - testCount++; - assert.equal(signal.aborted, true); + assert.equal(signal.aborted, false); - console.log(`abort called for test ${testCount}`) + waitForAbort({ testNumber: ++testCount, signal }); }); test("sync", (t) => { diff --git a/test/fixtures/test-runner/aborts/wait-for-abort-helper.js b/test/fixtures/test-runner/aborts/wait-for-abort-helper.js new file mode 100644 index 00000000000000..f513063066a1ff --- /dev/null +++ b/test/fixtures/test-runner/aborts/wait-for-abort-helper.js @@ -0,0 +1,19 @@ +module.exports = { + waitForAbort: function ({ testNumber, signal }) { + let retries = 0; + + const interval = setInterval(() => { + retries++; + if(signal.aborted) { + console.log(`abort called for test ${testNumber}`); + clearInterval(interval); + return; + } + + if(retries > 100) { + clearInterval(interval); + throw new Error(`abort was not called for test ${testNumber}`); + } + }, 10); + } +} \ No newline at end of file