Migrating unit tests to use Jest 27

With promises that not all future posts are going to cover upgrading every single dependency in the project, I wanted to also get documented some of the banana skins that left me stumbling around when updating the Jest testing framework to the latest version.

Working on the assumption you know what Jest even is, and why I'd even want to have unit tests, the biggest change in the major release 27.0.0 was the default test runner flipping to jest-circus.

A ripple effect of this change was stricter management by the framework of how callbacks were handled in each test block. To date Jest had been fairly forgiving if you executed a done callback multiple times, or indeed called it and returned a promise.

Earlier Jest versions just swallowed these unintentional mistakes silently, and carried on it's merry way running the tests without failing the test

From this latest release however, Jest was getting stricter meaning that now

The first two items here didn't really impact the project-beardy code-base; however the last one did.

There's unit tests we run ensure that the database collections, and their associated service layer api methods behave as expected. To do this, each test spins up a copy of the database with some simulated data already populated, and once the tests are complete - tears this instance down before moving on to the next set of tests.

So what does this look like; and what changes were needed to get the tests behaving with version 27.0.0.

describe("TESTING: PlayerService methods", () =>{
…
clubid = "2fc81fe7-18b0-4a82-8bd4-f660f0da499a";
playerid = "b23adbc3-8961-400d-b0e6-6c96e3af5769";

beforeAll(async (done) => {
app = new App();
await app.init();
service = new PlayerService(app.store.db);
clubs = new ClubService(app.store.db);
done();
})

afterAll(async (done) => {
await app.teardown();
done();
})
})

Essentially whats going on here is, before our PlayerService methods test set start running we create a new version of the App, initiate the database inside it via the init() method. Then once this has resolved create instances of the required service classes - passing them a reference to the database.

Once thats all complete the done callback is executed and the tests can start.

The problem being however if we try to run tests configured this way in Jest 27 it will barff…

Test functions cannot both take a 'done' callback and
return something. Either use a 'done' callback, or return
a promise.
Returned value: Promise {}

18 | let wait;
19 |
> 20 | beforeAll(async (done) => {
| ^
21 |
22 | clubid = "d62a9d4c-3541-4a20…";
23 | playerid = "11f45943-4348-4a93…";

at tests/unit/services/player-service.test.ts:20:3
at Object.<anonymous> (…/player-service.test.ts:11:1)

No major surprises here, its doing exactly what the docs said it would. However I spent way more time than I should have getting these tests to even run let alone pass.

Frustratingly the fix is super simple; but I initially disappeared down a rabbit hole of trying to fore a resolve promise from the beforeAll and afterAll methods. None of which gave any joy.

The solution - as if often the case - was about what we take away and not what we add.

describe("TESTING: PlayerService methods", () => {

…

beforeAll(async () => {
app = new App();
await app.init();
service = new PlayerService(app.store.db);
clubs = new ClubService(app.store.db);
})

afterAll(async () => {
await app.teardown();
})

})

Nuking the done callback references completely gives us what we need. What I'd stupidly missed initially, was by their very nature, the async method is returning a promise already.

Not the most complex of issues, but I'm sure I wont be the only one who gets snagged on this, so hopefully this short post will save others the time I wasted.

Back to Home