Closed meis closed 6 years ago
Doesn't this (https://mochajs.org/#hooks) do everything you need?
Hi @meis!
Thank you very much for your PR, it's definitely a feature I've been wanting to implement.
I just got back from a long vacation, so very sorry for taking so long to get to this. I will evaluate your PR as soon as I can.
@binarymist Unfortunately beforeEach
and afterEach
are executed around every it
, regardless of nesting. At least that was the case last time I checked. Not sure if it has changed for Mocha 4
Thanks @iensu, welcome back :)
And yes, I don't think there's a way to do this in Mocha. There's probably no reason to in the interfaces they ship because there's no named levels of indentation.
@meis Thanks! I have had a look at your PR and it works as expected. Great work!
I understand the symmetry reason for implementing beforeEachFeature
and afterEachFeature
. However, I can't come up with a good use-case for the hooks since they would be applied globally to all features in the test suite. I'm worried that it might be a bit confusing. I would like to hear your opinion here :)
Also before merging it would be awesome if you could add a description of the functions in the README.
I understand the symmetry reason for implementing beforeEachFeature and afterEachFeature. However, I can't come up with a good use-case for the hooks since they would be applied globally to all features in the test suite. I'm worried that it might be a bit confusing. I would like to hear your opinion here :)
The only thing that comes to mind (and how I intend to use this) is as a common setup step. We do initialize a database on each scenario, but I can see that done on each feature too. If you think that might be confusing, we could leave them out. They should be trivial to implement anyway.
Well, let's leave the functions in. As you say, it might be that someone has the need for them and the name should be clear enough to understand the consequences of using it on the top scope.
I'm going to merge this now. Again thanks a lot for your PR!
Awesome @iensu, thanks to you!
Do you have any plans to release this to npm?
I'm going to try to get a new release out beginning of next week.
@meis version 3.1.0 is out now!
This commit adds four functions to context: beforeEachFeature, beforeEachScenario, afterEachFeature and afterEachScenario. They set hooks that would only be executed on nested Features and Scenarios respectively.
This is convenient to setup all the Features and Scenarios without requiring a before or after for each of them.
First of all, thanks in advance for your time reviewing this.
I'm working in a project which adopted
mocha-cakes-2
recently and last week we've missed a feature. We were setting a bunch ofScenarios
inside a feature, and we wanted to provide a clean state (in this case, a clean database) for eachScenario
, but share that state between all the tests inside theScenario
.We ended up adding the same
before
hook on eachScenario
. I've found that solution not quite satisfactory so I tried to implement abeforeEachScenario
hook that would solve our problem in a rather elegant way.For the same price, I've also implemented
beforeEachFeature
,afterEachFeature
andafterEachScenario
. I'm not sure about the order in which the hooks should executed, so maybe it's worth having an extra look at that.I'm not that familiar with the mocha ecosystem, so maybe there's a better way to implement this. In any case, this would be a nice feature for us and I hope you find that it has a place in this module.