This story was moved to: https://jonathancardoso.com/en/blog/parallel-testing-a-graphql-server-with-jest/

This is a follow-up to the awesome post written by Sibelius Seraphini:

Previously here at Entria, we were running our tests using the --runInBand flag, which makes Jest work serially, running one test after the other.

This was needed, because we were creating a physical temporary database to be used by all tests, and as so, we could not let two tests run at the same time, because it would create data inconsistencies between different tests. So the idea was basically to connect to the database before each test, populate the database and run the necessary tests, and after that drop the database, so it could be recreated again by the next test. Repeat this for the number of tests we had.

We solved this thanks to the awesome library mongodb-memory-server by @nodkz

To use it, we created our own jest test environment, which is just a single file:

And configured jest to use it, by setting the testEnvironment setting:

//...

"jest": { "testEnvironment": "<rootDir>/test/environment/mongodb",

" resetModules": true, //...

Note the resetModules setting there, it’s also important if you use Mongoose, otherwise it will try to reuse the same connection from the previous test that was ran. This happens because we are setting our models using the default connection from mongoose, which is stored directly on the exported mongoose object.

And that is it, you can see it in action on the boilerplate we created here at Entria, here is the PR adding it: https://github.com/entria/graphql-dataloader-boilerplate/pull/80

before

after

On one of our biggest projects:

before