#Struggling to setup Integration Testing with Jest on a complex node Project

9 messages · Page 1 of 1 (latest)

somber nimbus
#

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:

  1. Connects to our neo4j database (locally, it's a test database running in docker)
  2. Adds fixtures (seed) to the database, so testing can begin
  3. 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:

  1. 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
  2. How do I make Jest understand Typescript path aliases?
  3. 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 🙏

reef zealot
#

However, these global setups do not understand Typescript path aliasing

#

!:paths-%

next cipherBOT
#
tjjfvi#0
`!t6:paths-are-not-magic`:

The paths and baseUrl compiler options don't cause any remapping of imports paths, they only inform TS of existing mappings, which you'll have to setup with some other tool.

baseUrl is a pretty well-supported option (e.g. using the NODE_PATH environment variable with node or resolve.modules with webpack).
paths can be trickier to setup, (especially with node see this for node), and you may find it to not be worth the effort.

reef zealot
#

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
it is indeed the right place for what you are trying to do
there is nothing special with those methods tho
and it should work as expected
maybe you should elaborate on the problems you are facing

#

How do I make Jest understand Typescript path aliases?
paths aliases are only for the compilers and don't work at runtime
check the embed above for more info
don't use path raliases (recommended), or add a plugin to resolve them based on your tsconfig

#

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.
it's nothing special with Jest
TS paths aliases are for the TS compiler only
Jest just calls Node
Node does not care about TS config and doesn't use it

#

@somber nimbus

somber nimbus
#

Thanks @reef zealot !

there is nothing special with those methods tho
There is a little bit though. One key thing for me, is that they don't understand the moduleNameMapper mappings.

So any imports I set in the globalSetup files, can't be imported with aliases.

All good though. So far I made a Javascript shim that loads ts-node as that can correctly handle paths.

I was also able to properly connect to the database and add the fixtures.

In fact, everything is working except one thing:

Tests are failing, because the test-suite is unable to connect to redis.
I am not sure why.

The test setup is using Jest with ts-node as described above, in a Typescript codebase.
It seems the fact that its a TS codebase convolutes things a bit and its not as straight forward as it could/should be.