Skip to content

Commit 483e1b0

Browse files
committed
Pass test's args to beforeEach/afterEach hooks
Closes avajs#2070
1 parent 49b202f commit 483e1b0

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

lib/runner.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ class Runner extends Emittery {
266266
return result;
267267
}
268268

269-
async runHooks(tasks, contextRef, titleSuffix) {
269+
async runHooks(tasks, contextRef, testArgs = [], titleSuffix) {
270270
const hooks = tasks.map(task => new Runnable({
271271
contextRef,
272272
failWithoutAssertions: false,
273273
fn: task.args.length === 0 ?
274-
task.implementation :
275-
t => task.implementation.apply(null, [t].concat(task.args)),
274+
t => task.implementation.apply(null, [t].concat(testArgs)) :
275+
t => task.implementation.apply(null, [t].concat(task.args).concat(testArgs)),
276276
compareTestSnapshot: this.boundCompareTestSnapshot,
277277
updateSnapshots: this.updateSnapshots,
278278
metadata: task.metadata,
@@ -303,9 +303,8 @@ class Runner extends Emittery {
303303

304304
async runTest(task, contextRef) {
305305
let hooksAndTestOk = false;
306-
307306
const hookSuffix = ` for ${task.title}`;
308-
if (await this.runHooks(this.tasks.beforeEach, contextRef, hookSuffix)) {
307+
if (await this.runHooks(this.tasks.beforeEach, contextRef, task.args, hookSuffix)) {
309308
// Only run the test if all `beforeEach` hooks passed.
310309
const test = new Runnable({
311310
contextRef,
@@ -328,7 +327,7 @@ class Runner extends Emittery {
328327
knownFailing: result.metadata.failing,
329328
logs: result.logs
330329
});
331-
hooksAndTestOk = await this.runHooks(this.tasks.afterEach, contextRef, hookSuffix);
330+
hooksAndTestOk = await this.runHooks(this.tasks.afterEach, contextRef, task.args, hookSuffix);
332331
} else {
333332
this.emit('stateChange', {
334333
type: 'test-failed',
@@ -342,7 +341,7 @@ class Runner extends Emittery {
342341
}
343342
}
344343

345-
const alwaysOk = await this.runHooks(this.tasks.afterEachAlways, contextRef, hookSuffix);
344+
const alwaysOk = await this.runHooks(this.tasks.afterEachAlways, contextRef, task.args, hookSuffix);
346345
return hooksAndTestOk && alwaysOk;
347346
}
348347

test/runner.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,66 @@ test('macros: Additional args will be spread as additional args on implementatio
592592
});
593593
});
594594

595+
test('Test\'s additional args will be spread and passed down to beforeEach, afterEach implementation functions', t => {
596+
t.plan(5);
597+
598+
return promiseEnd(new Runner(), runner => {
599+
runner.on('stateChange', evt => {
600+
if (evt.type === 'test-passed') {
601+
t.pass();
602+
}
603+
});
604+
605+
runner.chain.beforeEach((a, ...rest) => {
606+
t.deepEqual(rest, ['foo', 'bar']);
607+
a.pass();
608+
});
609+
runner.chain.afterEach((a, ...rest) => {
610+
t.deepEqual(rest, ['foo', 'bar']);
611+
a.pass();
612+
});
613+
runner.chain.afterEach.always((a, ...rest) => {
614+
t.deepEqual(rest, ['foo', 'bar']);
615+
a.pass();
616+
});
617+
618+
runner.chain('test1', (a, ...rest) => {
619+
t.deepEqual(rest, ['foo', 'bar']);
620+
a.pass();
621+
}, 'foo', 'bar');
622+
});
623+
});
624+
625+
test('Test\'s additional args will be concatenated with args of beforeEach, afterEach args', t => {
626+
t.plan(5);
627+
628+
return promiseEnd(new Runner(), runner => {
629+
runner.on('stateChange', evt => {
630+
if (evt.type === 'test-passed') {
631+
t.pass();
632+
}
633+
});
634+
635+
runner.chain.beforeEach((a, ...rest) => {
636+
t.deepEqual(rest, ['baz', 'foo', 'bar']);
637+
a.pass();
638+
}, 'baz');
639+
runner.chain.afterEach((a, ...rest) => {
640+
t.deepEqual(rest, ['baz', 'foo', 'bar']);
641+
a.pass();
642+
}, 'baz');
643+
runner.chain.afterEach.always((a, ...rest) => {
644+
t.deepEqual(rest, ['baz', 'foo', 'bar']);
645+
a.pass();
646+
}, 'baz');
647+
648+
runner.chain('test1', (a, ...rest) => {
649+
t.deepEqual(rest, ['foo', 'bar']);
650+
a.pass();
651+
}, 'foo', 'bar');
652+
});
653+
});
654+
595655
test('macros: Customize test names attaching a `title` function', t => {
596656
t.plan(6);
597657

0 commit comments

Comments
 (0)