I am playing around with Node.js and Jest for testing and running into a problem when I have multiple test files.
In each of my test, I have beforeAll() which starts my server and I have afterAll() which closes the server. When I had one test file, it worked fine. But when I have two test files, the server state conflicts. Sometimes while server is already running from first file, second file tries to run and causes Error: listen EADDRINUSE type of error, sometimes first file closes server in the middle of second file running and it all depends on timing that sometimes it works and sometimes it runs into issues.
Is there way to get my server started once before any of the test files run, then close the server after all of the test files are completed?
Or is it that I need to structure my testing differently? Perhaps I need to put all tests which requires my server into single test file? I kind of doubt I have to do it that way, but who knows..
Any help or suggestion would be appreciated. And I prefer not using --runInBand flag though I am not certain this would help.