iensu / mocha-cakes-2

A BDD plugin for Mocha testing framework
MIT License
43 stars 10 forks source link

Add before and after hooks only for Features and Scenarios #11

Closed meis closed 6 years ago

meis commented 6 years ago

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 of Scenarios inside a feature, and we wanted to provide a clean state (in this case, a clean database) for each Scenario, but share that state between all the tests inside the Scenario.

We ended up adding the same before hook on each Scenario. I've found that solution not quite satisfactory so I tried to implement a beforeEachScenario hook that would solve our problem in a rather elegant way.

For the same price, I've also implemented beforeEachFeature, afterEachFeature and afterEachScenario. 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.

binarymist commented 6 years ago

Doesn't this (https://mochajs.org/#hooks) do everything you need?

iensu commented 6 years ago

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.

iensu commented 6 years ago

@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

meis commented 6 years ago

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.

iensu commented 6 years ago

@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.

meis commented 6 years ago

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.

iensu commented 6 years ago

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!

meis commented 6 years ago

Awesome @iensu, thanks to you!

Do you have any plans to release this to npm?

iensu commented 6 years ago

I'm going to try to get a new release out beginning of next week.

iensu commented 6 years ago

@meis version 3.1.0 is out now!