I see two ways in which to solve your task. First, do not subscribe to the after
hook, but create your own reporter
or modify the existing reporter
. Please refer to the following article: https://devexpress.github.io/testcafe/documentation/extending-testcafe/reporter-plugin/#implementing-the-reporter
The most interesting method there is reportTestDone
because it accepts errs
as a parameter, so you can add your custom logic there.
The second approach is using sharing variables between hooks and test code
You can write your test in the following manner:
test(`test`, async t => {
await t.click('#non-existing-element');
t.ctx.passed = true;
}).after(async t => {
if (t.ctx.passed)
throw new Error('not passed');
});
Here I am using the shared passed
variable between the test code and hook. If the test fails, the variable will not be set to true, and I'll get an error in the after
hook.