Skip to content

Pass test's args to beforeEach/afterEach hooks #2223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,11 @@ class Runner extends Emittery {
return result;
}

async runHooks(tasks, contextRef, titleSuffix) {
async runHooks(tasks, contextRef, testArgs = [], titleSuffix) {
const hooks = tasks.map(task => new Runnable({
contextRef,
failWithoutAssertions: false,
fn: task.args.length === 0 ?
task.implementation :
t => task.implementation.apply(null, [t].concat(task.args)),
fn: t => task.implementation.call(null, t, ...task.args, ...testArgs),
compareTestSnapshot: this.boundCompareTestSnapshot,
updateSnapshots: this.updateSnapshots,
metadata: task.metadata,
Expand Down Expand Up @@ -303,9 +301,14 @@ class Runner extends Emittery {

async runTest(task, contextRef) {
let hooksAndTestOk = false;

const hookSuffix = ` for ${task.title}`;
if (await this.runHooks(this.tasks.beforeEach, contextRef, hookSuffix)) {
let testArgs = task.args || [];
// Is it a macro? Then skip its args
if (task.implementation && task.implementation.title) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Macros are not required to implement .title. They're not really distinguishable from regular test implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried to nail down the very last argument of a test ({initDB: true} in the example) and only pass it to the hook, since stuff like '3 * 3', 9 won't be useful. Also document it and explain users that the last argument is always passed to the beforeEach/afterEach hooks.

test('providedTitle', macro1, '3 * 3', 9, {initDB: true});

But due to inability to really detect when something is a macro I'm puzzled for now.
And with my implementation args are and array, need to think about @Gwenio response.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried to nail down the very last argument of a test ({initDB: true} in the example) and only pass it to the hook, since stuff like '3 * 3', 9 won't be useful. Also document it and explain users that the last argument is always passed to the beforeEach/afterEach hooks.

test('providedTitle', macro1, '3 * 3', 9, {initDB: true});

But due to inability to really detect when something is a macro I'm puzzled for now.
And with my implementation args are and array, need to think about @Gwenio response.

If both hook args and macro args can both be of variable length then finding where to split them is not possible.
This means to support macros with rest or optional parameters that the hook arg count needs to be fixed.
Having them be fixed not only excludes a rest parameter, but also means the hook arg(s) cannot be optional.
And requiring a hook arg would be an interface breaking change.

To avoid this the hook arg would need to come before the test implementation(s).
Then it could be extracted when getting the optional base title and the implementation(s).
It would also need the type of the arg limited such that it is distinct from the implementation(s) (and the title, if it can come before that too).

The relevant part of the runner for extracting title and implementation(s) is line 60 to 65 of runner.js.
The hook arg should then be added to the task record when it is created on line 129.

testArgs = testArgs.slice(2);
}

if (await this.runHooks(this.tasks.beforeEach, contextRef, testArgs, hookSuffix)) {
// Only run the test if all `beforeEach` hooks passed.
const test = new Runnable({
contextRef,
Expand All @@ -328,7 +331,7 @@ class Runner extends Emittery {
knownFailing: result.metadata.failing,
logs: result.logs
});
hooksAndTestOk = await this.runHooks(this.tasks.afterEach, contextRef, hookSuffix);
hooksAndTestOk = await this.runHooks(this.tasks.afterEach, contextRef, testArgs, hookSuffix);
} else {
this.emit('stateChange', {
type: 'test-failed',
Expand All @@ -342,7 +345,7 @@ class Runner extends Emittery {
}
}

const alwaysOk = await this.runHooks(this.tasks.afterEachAlways, contextRef, hookSuffix);
const alwaysOk = await this.runHooks(this.tasks.afterEachAlways, contextRef, testArgs, hookSuffix);
return hooksAndTestOk && alwaysOk;
}

Expand Down
60 changes: 60 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,66 @@ test('macros: Additional args will be spread as additional args on implementatio
});
});

test('Test\'s additional args will be spread and passed down to beforeEach, afterEach implementation functions', t => {
t.plan(5);

return promiseEnd(new Runner(), runner => {
runner.on('stateChange', evt => {
if (evt.type === 'test-passed') {
t.pass();
}
});

runner.chain.beforeEach((a, ...rest) => {
t.deepEqual(rest, ['foo', 'bar']);
a.pass();
});
runner.chain.afterEach((a, ...rest) => {
t.deepEqual(rest, ['foo', 'bar']);
a.pass();
});
runner.chain.afterEach.always((a, ...rest) => {
t.deepEqual(rest, ['foo', 'bar']);
a.pass();
});

runner.chain('test1', (a, ...rest) => {
t.deepEqual(rest, ['foo', 'bar']);
a.pass();
}, 'foo', 'bar');
});
});

test('Test\'s additional args will be concatenated with args of beforeEach, afterEach args', t => {
t.plan(5);

return promiseEnd(new Runner(), runner => {
runner.on('stateChange', evt => {
if (evt.type === 'test-passed') {
t.pass();
}
});

runner.chain.beforeEach((a, ...rest) => {
t.deepEqual(rest, ['baz', 'foo', 'bar']);
a.pass();
}, 'baz');
runner.chain.afterEach((a, ...rest) => {
t.deepEqual(rest, ['baz', 'foo', 'bar']);
a.pass();
}, 'baz');
runner.chain.afterEach.always((a, ...rest) => {
t.deepEqual(rest, ['baz', 'foo', 'bar']);
a.pass();
}, 'baz');

runner.chain('test1', (a, ...rest) => {
t.deepEqual(rest, ['foo', 'bar']);
a.pass();
}, 'foo', 'bar');
});
});

test('macros: Customize test names attaching a `title` function', t => {
t.plan(6);

Expand Down