Skip to content
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

fix: make sure that errors from before hook are reported #196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,27 @@ function format(name, err, suite = '') {
return str + '\n';
}

function addNewlineToErrors(errors) {
if (errors.length) errors += '\n';
}

async function runner(ctx, name) {
let { only, tests, before, after, bEach, aEach, state } = ctx;
let hook, test, arr = only.length ? only : tests;
let num=0, errors='', total=arr.length;

try {
if (name) write(SUITE(kleur.black(` ${name} `)) + ' ');
for (hook of before) await hook(state);
for (hook of before) {
try {
await hook(state);
} catch (err) {
addNewlineToErrors(errors)
errors += format(`${name ? name + ' ' : '' }before`, err, name)
write(FAIL)
throw err;
}
}

for (test of arr) {
state.__test__ = test.name;
Expand All @@ -79,15 +92,25 @@ async function runner(ctx, name) {
write(PASS);
num++;
} catch (err) {
for (hook of aEach) await hook(state);
if (errors.length) errors += '\n';
for (hook of aEach) try {
await hook(state);
} catch (aEachErr) {
addNewlineToErrors(errors)
errors += format(`${test.name} afterEach`, aEachErr, name);
}
addNewlineToErrors(errors)
errors += format(test.name, err, name);
write(FAIL);
}
}
} finally {
state.__test__ = '';
for (hook of after) await hook(state);
for (hook of after) try {
await hook(state);
} catch (afterErr) {
addNewlineToErrors(errors)
errors += format(`${ name ? name + ' ' : ''} after`, err, name);
}
let msg = ` (${num} / ${total})\n`;
let skipped = (only.length ? tests.length : 0) + ctx.skips;
write(errors.length ? kleur.red(msg) : kleur.green(msg));
Expand Down