Jest: Mistakenly nested tests neither run nor result in an error

Created on 25 Dec 2017  路  6Comments  路  Source: facebook/jest

Example:
https://repl.it/repls/CavernousElegantIvorybilledwoodpecker

When test cases get long, it's possible to mistakenly write a test case inside of another test case. Sometimes humans get mixed up on parentheses and braces.

When this happens, the internal test() method does not execute, and no errors are shown. Thus it's easy to add a test case that never runs.

I can't think of a reason to nest test cases, so my feeling is the expected behavior would be to raise an error in this situation.

Most helpful comment

Use describe for nesting and test.each for multiple cases

All 6 comments

Can you try this with Jest 22? We changed this to throw! Happy to reopen if it鈥檚 not fixed.

In #4039, for reference.

We should probably have a test for that behavior...

@cpojer It works _exactly_ as I would like under Jest 22! Excellent error messages! Thank you!

Tests cannot be nested. Test adds 2 + 3 to equal 6 cannot run because it is nested within adds 1 + 2 to equal 3.

I came across this issue when trying to write nested data driven tests .. e.g. for each browser.. for each site .. but got error "Tests cannot be nested."
Is disallowing nested tests by design? Can you explain why please. @SimenB

Use describe for nesting and test.each for multiple cases

Thanks a lot @thymikee
Didn't know about describe.each()

Was this page helpful?
0 / 5 - 0 ratings