Skip to content

Commit 3f694f7

Browse files
cjihrignodejs-github-bot
authored andcommitted
test_runner: refactor hook creation
This commit makes hook creation more consistent by always passing in a reference to the test that owns the hook. It also removes some unnecessary validation on internal API. PR-URL: #54353 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 7ac3a50 commit 3f694f7

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

lib/internal/test_runner/test.js

+40-19
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,43 @@ class TestContext {
311311
}
312312

313313
before(fn, options) {
314-
this.#test
315-
.createHook('before', fn, { __proto__: null, ...options, hookType: 'before', loc: getCallerLocation() });
314+
this.#test.createHook('before', fn, {
315+
__proto__: null,
316+
...options,
317+
parent: this.#test,
318+
hookType: 'before',
319+
loc: getCallerLocation(),
320+
});
316321
}
317322

318323
after(fn, options) {
319-
this.#test
320-
.createHook('after', fn, { __proto__: null, ...options, hookType: 'after', loc: getCallerLocation() });
324+
this.#test.createHook('after', fn, {
325+
__proto__: null,
326+
...options,
327+
parent: this.#test,
328+
hookType: 'after',
329+
loc: getCallerLocation(),
330+
});
321331
}
322332

323333
beforeEach(fn, options) {
324-
this.#test
325-
.createHook('beforeEach', fn, { __proto__: null, ...options, hookType: 'beforeEach', loc: getCallerLocation() });
334+
this.#test.createHook('beforeEach', fn, {
335+
__proto__: null,
336+
...options,
337+
parent: this.#test,
338+
hookType: 'beforeEach',
339+
loc: getCallerLocation(),
340+
});
326341
}
327342

328343
afterEach(fn, options) {
329-
this.#test
330-
.createHook('afterEach', fn, { __proto__: null, ...options, hookType: 'afterEach', loc: getCallerLocation() });
344+
this.#test.createHook('afterEach', fn, {
345+
__proto__: null,
346+
...options,
347+
parent: this.#test,
348+
hookType: 'afterEach',
349+
loc: getCallerLocation(),
350+
});
331351
}
332352
}
333353

@@ -1089,14 +1109,17 @@ class Test extends AsyncResource {
10891109
class TestHook extends Test {
10901110
#args;
10911111
constructor(fn, options) {
1092-
if (options === null || typeof options !== 'object') {
1093-
options = kEmptyObject;
1094-
}
1095-
const { loc, timeout, signal } = options;
1096-
super({ __proto__: null, fn, loc, timeout, signal });
1097-
1098-
this.parentTest = options.parent ?? null;
1099-
this.hookType = options.hookType;
1112+
const { hookType, loc, parent, timeout, signal } = options;
1113+
super({
1114+
__proto__: null,
1115+
fn,
1116+
loc,
1117+
timeout,
1118+
signal,
1119+
harness: parent.root.harness,
1120+
});
1121+
this.parentTest = parent;
1122+
this.hookType = hookType;
11001123
}
11011124
run(args) {
11021125
if (this.error && !this.outerSignal?.aborted) {
@@ -1120,9 +1143,7 @@ class TestHook extends Test {
11201143
const { error, loc, parentTest: parent } = this;
11211144

11221145
// Report failures in the root test's after() hook.
1123-
if (error && parent !== null &&
1124-
parent === parent.root && this.hookType === 'after') {
1125-
1146+
if (error && parent === parent.root && this.hookType === 'after') {
11261147
if (isTestFailureError(error)) {
11271148
error.failureType = kHookFailure;
11281149
}

0 commit comments

Comments
 (0)