mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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: https://github.com/nodejs/node/pull/54353 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
7ac3a50345
commit
3f694f7c99
1 file changed
+40
-19
@@ -311,23 +311,43 @@ class TestContext {
|
||||
}
|
||||
|
||||
before(fn, options) {
|
||||
this.#test
|
||||
.createHook('before', fn, { __proto__: null, ...options, hookType: 'before', loc: getCallerLocation() });
|
||||
this.#test.createHook('before', fn, {
|
||||
__proto__: null,
|
||||
...options,
|
||||
parent: this.#test,
|
||||
hookType: 'before',
|
||||
loc: getCallerLocation(),
|
||||
});
|
||||
}
|
||||
|
||||
after(fn, options) {
|
||||
this.#test
|
||||
.createHook('after', fn, { __proto__: null, ...options, hookType: 'after', loc: getCallerLocation() });
|
||||
this.#test.createHook('after', fn, {
|
||||
__proto__: null,
|
||||
...options,
|
||||
parent: this.#test,
|
||||
hookType: 'after',
|
||||
loc: getCallerLocation(),
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(fn, options) {
|
||||
this.#test
|
||||
.createHook('beforeEach', fn, { __proto__: null, ...options, hookType: 'beforeEach', loc: getCallerLocation() });
|
||||
this.#test.createHook('beforeEach', fn, {
|
||||
__proto__: null,
|
||||
...options,
|
||||
parent: this.#test,
|
||||
hookType: 'beforeEach',
|
||||
loc: getCallerLocation(),
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(fn, options) {
|
||||
this.#test
|
||||
.createHook('afterEach', fn, { __proto__: null, ...options, hookType: 'afterEach', loc: getCallerLocation() });
|
||||
this.#test.createHook('afterEach', fn, {
|
||||
__proto__: null,
|
||||
...options,
|
||||
parent: this.#test,
|
||||
hookType: 'afterEach',
|
||||
loc: getCallerLocation(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1089,14 +1109,17 @@ class Test extends AsyncResource {
|
||||
class TestHook extends Test {
|
||||
#args;
|
||||
constructor(fn, options) {
|
||||
if (options === null || typeof options !== 'object') {
|
||||
options = kEmptyObject;
|
||||
}
|
||||
const { loc, timeout, signal } = options;
|
||||
super({ __proto__: null, fn, loc, timeout, signal });
|
||||
|
||||
this.parentTest = options.parent ?? null;
|
||||
this.hookType = options.hookType;
|
||||
const { hookType, loc, parent, timeout, signal } = options;
|
||||
super({
|
||||
__proto__: null,
|
||||
fn,
|
||||
loc,
|
||||
timeout,
|
||||
signal,
|
||||
harness: parent.root.harness,
|
||||
});
|
||||
this.parentTest = parent;
|
||||
this.hookType = hookType;
|
||||
}
|
||||
run(args) {
|
||||
if (this.error && !this.outerSignal?.aborted) {
|
||||
@@ -1120,9 +1143,7 @@ class TestHook extends Test {
|
||||
const { error, loc, parentTest: parent } = this;
|
||||
|
||||
// Report failures in the root test's after() hook.
|
||||
if (error && parent !== null &&
|
||||
parent === parent.root && this.hookType === 'after') {
|
||||
|
||||
if (error && parent === parent.root && this.hookType === 'after') {
|
||||
if (isTestFailureError(error)) {
|
||||
error.failureType = kHookFailure;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user