mocha-cakes-2 icon indicating copy to clipboard operation
mocha-cakes-2 copied to clipboard

Skip remaining tests when a `Given` or `When` fails

Open scottrippey opened this issue 6 years ago • 3 comments

I love the semantics this library adds to my tests.
In particular, I love how this allows me to write "sequential steps", which are clearly dependent on each other. For example:

Given("I have an apple", ...);
When("I eat the apple", ...);
Then("I feel full", ...);
And("I feel healthy", ...);

This gives me very clear "arrange", "act", and "assert" sections (Given, When, Then).

Now imagine that one of my "arrange" or "act" steps fails -- eg. I fail to "eat the apple".
Unfortunately, this results in 3 failed tests.

However, that's not quite what I expect. When an "arrange" or "act" fails, I would expect the "assertions" to be skipped. In other words, if a Given or When fails, all Thens should be skipped. And / But would probably be best categorized based on what preceded them.

Does this make sense? I half-expected this to be the existing behavior, but a quick test reveals that if any Given/When fails, the rest of the assertions are still executed (which often gives confusing results).

scottrippey avatar May 04 '18 17:05 scottrippey

Hi!

Thank you for your issue. I agree that it would be a nice to have feature. However, I believe this might be a bit tricky to achieve. I will look into it when I get some time over. If you have a clear idea of how to implement it feel free to create a PR :)

Running mocha with the --bail flag will stop the test suite as soon as an assertion fails. Not really what you are after perhaps, but anyway. :)

iensu avatar May 04 '18 19:05 iensu

Totally agree

gallofeliz avatar Dec 31 '18 13:12 gallofeliz

I've made this small plug-in to address this issue: https://gist.github.com/Lautenschlager-id/b2852091e72f4ade94c6212aa510f7b9

Sadly it's a bit too high level to be implemented in the source of mocha-cakes-2 I'm afraid.

Lautenschlager-id avatar Jul 20 '22 17:07 Lautenschlager-id