Skip to content

Commit

Permalink
abort after all cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rluvaton committed Jul 19, 2023
1 parent 58da4d2 commit f5f36b6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
17 changes: 6 additions & 11 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/test-runner/aborts/wait-for-abort-helper.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f5f36b6

Please sign in to comment.