I’m running my integration tests in the “13- Integration Testing” unit. Sometimes, when I run those tests, I get this error:
listen EADDRINUSE: address already in use :::3000
I’ve compared my solution to the code in the “after” directory, and I don’t see what I’m doing wrong. Can someone tell me how I can figure out what’s causing this problem?
OK, I’ve gotten a little further ahead in the lessons, and I see that Mosh avoids this error by awaiting each server.close(). But why would that work? It’s my understanding that while Jest runs the tests within each file sequentially, it runs the different test files in parallel. It seems like we’d still get two different test files trying to start the Node server at the same time.
ok guys here is the solution. GO TO PACKAGE.JSON, under scripts, test, you probably already have some flags like --verbose --watchAll, NOWWWWWW ADDDD another flag called --maxWorkers=1
I’m having the same issues, i don’t know why, but unless i use --runInBand or --maxWorkers=1. Even using --detectOpenHandles didn’t work because my tests ran perfecty when using that flag. So i’m getting really confused on why this problem happens to me but not to mosh (Even before fixing his ‘await server.close()’ he didn-t have any issues.
This worked for me. I renamed the integration test files with “xxxx” instead of “test” eg auth.xxxx.js instead of auth.test.js so I could check each file independently. If I ran tests in genres.test.js by itself (other two files renamed) all tests passed. If I ran auth.test.js with other two files renamed then auth.test.js ran fine. But if I ran tests with both genres.test.js AND auth.test.js then it would throw an error related to the port being in use. Thats crazy! One set of tests shouldnt affect another!! If I run all three then I get two errors, both relating to Port. So there definitely is something going on with running multiple tests. Port is being opened in one test and then another test file tries to open same port. Anyway, I added --maxWorkers=1 and all three ran with no errors. Thanks for the solution.