common mistake labelnode node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.As the title says. If I call this.skip() in the before hook, the after hook is still called.
Is this a correct behavior?
describe('test', function () {
before(function() {
this.skip();
});
it('it', function* () {
// foobar
});
after(function() {
// Will be called
});
});
It can be solved by calling this.skip(); in the after hook as well. But since the DOC says:
Mocha provides the hooks before(), after(), beforeEach(), and afterEach(). These should be used to set up preconditions and clean up after your tests.
It feels like an odd behavior that skipping the test in before still results in after being called.
Not test setup was done so why should the cleanup still be executed?
Kind regards,
Edvin
P.S
I'm quite new to github so please excuse any rookie mistakes that I've made.
I hope I have provided all the necessary information in a good manner.
Thank you for any and all help.
If applicable, please specify:
mocha --version and node node_modules/.bin/mocha --version: 3.5.0node --version: v6.10.3Hi @EdvinOlofsson! That's a good question. I think the crux of it is here:
No test setup was done so why should the cleanup still be executed?
As a matter of fact, this.skip can be called anywhere in the hook, so test setup may have been done and Mocha doesn't really know whether it was or not. And even without this.skip, it's possible for setup processes to complete partially before throwing an error, which presents a similar problem for Mocha. (Hypothetically, if setup were encouraged to split up into more steps by some system wherein cleanup steps are associated with specific setup steps, neither of these would be an issue; but I'm not sure whether that can be done in Mocha's style of UI.)
Humm I see. That makes a lot of sense. Thanks for clearing that up for me!
I have recently ran into this issue as well and initially I started to wrap the after hooks in "exception swallowers", which works, but I find it a bit ugly. So I thought I'd try @EdvinOlofsson's suggestion to also call this.skip() in after(), but that apparently no longer works:
1) Service
"after all" hook in "Service":
Error: `this.skip` forbidden
Is this expected? If yes, what is the recommended way how to skip after hooks then? I can open a new issue for that, if this is out of scope here.
Most helpful comment
Hi @EdvinOlofsson! That's a good question. I think the crux of it is here:
As a matter of fact,
this.skipcan be called anywhere in the hook, so test setup may have been done and Mocha doesn't really know whether it was or not. And even withoutthis.skip, it's possible for setup processes to complete partially before throwing an error, which presents a similar problem for Mocha. (Hypothetically, if setup were encouraged to split up into more steps by some system wherein cleanup steps are associated with specific setup steps, neither of these would be an issue; but I'm not sure whether that can be done in Mocha's style of UI.)