Problem
I was not able the find the RIGHT solution and looks like no one has been. When the tests are run in sequence (or ran alone), and the tests fail, Jest can show a readable error message.
The problem happens when you run Jest in parallel mode (which is the default mode). That's why adding the -w=2
helped, it's because it decreased the "parallelity" to only 2.
Probably Jest has a bug that it can't detect failing tests when they're run in parallel mode. So the solution is to always use a config that forces sequence.
Solution
You have basically these options:
npx jest --maxWorkers=1 # run tests serially
npx jest --runInBand # equivalent of the above
npx jest --detectOpenHandles # equivalent of the above, plus some additional checks
Performance
It'll slow down the test speed a little bit, but in most cases, the difference won't be big. In my case, the difference was in the order of seconds, look:
Running with --detectOpenHandles
:
Test Suites: 26 passed, 26 total
Tests: 145 passed, 145 total
Snapshots: 0 total
Time: 24.093 s
Running without --detectOpenHandles
:
Test Suites: 26 passed, 26 total
Tests: 145 passed, 145 total
Snapshots: 0 total
Time: 23.602 s
Also, I found this article in which the guy also states he hadn't noticed a huge performance issue, even with 600+ tests.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…