Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

jestjs - Yarn test hangs when one more tests file is added

I'm running a project that has several test suites (Jest), all passing locally. I have a step in my CircleCI job that simply runs yarn test.

The problem is that the entire build hangs during the yarn test step. The tests all pass, but the step just hangs, and times out after 10 min.

To make thing more interesting, if I remove one test suite file from my tests folder, the step will pass. I can remove any tests file, it will still pass. That leads me to believe there is nothing wrong with my tests. Also, if I remove one file, and add a new file with just one dummy test, the step will hang again.

enter image description here

What am I overlooking?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...