Name your mocked function with word “mock”

Let’s look at the example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const fakeApi = jest.fn().mockImplementation(() => ({
  ok({
    data: []
  })
}));

jest.mock('../some-module.js', () => {
  return jest.fn().mockImplementation(function () {
    this.api = fakeApi;
  });
});

It’s a part of the test where we mock some module. We also use mocked function “fakeApi” in the mock of module. But it will not work because function “fakeApi” will be in Temporal Dead Zone In JavaScript.

Basically, it happens because all module’s mocks in JestJS moves on the top of test’s executing. So, when test is running, jest.mock('../some-module.js'... is above than mockApi.

We can avoid this behavior. We should name function “fakeApi” with word “mock” at the beginning. Something like that:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const mockApi = jest.fn().mockImplementation(() => ({
  ok({
    data: []
  })
}));

jest.mock('../some-module.js', () => {
  return jest.fn().mockImplementation(function () {
    this.api = mockApi;
  });
});