I am having some issues setting up Jest to test a rather complex node project.
I have succesfully managed to setup a testsuite, running Integration tests, using Jest. However, as soon as I add another test file which needs to run the same setup script in beforeAll things start breaking, and whichever test gets to run last, isn't able to connect to the (test) database.
In my first testfile I import a file dbSetup with a single exported method setupDatabase() that:
- Connects to our neo4j database (locally, it's a test database running in docker)
- Adds fixtures (seed) to the database, so testing can begin
- Closes the connection when its done.
All of the above is awaited so the test suite only runs once the fixtures have populated the database, which is working as intended.
However, as soon as another file gets added, which attempts to run the same file, its breaking with an error Illegal Host (specific to Neo4j) which essentially means it cant connect to the database.
I suspect its a race condition.
Fundamentally its a poor design as this type of thing shouldn't be run in a beforeAll. So I tried moving it to the globalSetup and globalTeardown properties of Jest. However, these global setups do not understand Typescript path aliasing, even if the rest of Jest does (after setting up moduleNameMapper).
So now I'm stuck:
- How on earth do I move forward with the global setup/teardown properties? These seem to be the right place to ensure my setup runs once every time I perform the tests
- How do I make Jest understand Typescript path aliases?
- If Jest isn't meant to understand path aliases, how do I proceed from here? Even if I compile things to Javascript first (which ideally I don't want to, as we're not transpiling anything in our development environment), the compiled output still contains path aliases.
Any help or insight is greatly appreciated 🙏