Search code examples
typescriptjestjsmockingts-jest

Why does upgrading jest to 29.5.0 break my mocks?


I'm upgrading jest in my project form 27.4.7 to 29.5.0 and suddenly my mocks for modules don't work. I've tried reading the documentation and it doesn't seem like anything changed, but it simply doesn't work. Maybe I missed something. All of the answers I can find here tell me to do the same thing I've always done, so I'm somewhat at a loss.

Here's an example:

mockTest.spec.ts

import { isTestPartnerId } from './utility';

jest.mock('./utility', () => ({
  __esModule: true,
  isTestPartnerId: jest.fn(async (base, context, id) => Promise.resolve(id === 'testPartnerId'))
}));

describe('test mock', () => {

  it('should print true', async () => {
    console.log(await isTestPartnerId(null, null, 'testPartnerId'));
  });
});

It seems like this should print 'true' to the console, but instead I get 'undefined'. Debugging tests shows that isTestPartnerId is, in fact, undefined.

I am using typescript with ts-jest 29.0.5 (the latest).

Any idea what I'm doing wrong here?


Solution

  • I found the answer. The clue was this: https://stackoverflow.com/a/70270857/662970

    In my case, we had "restoreMocks": true in the jest config in package.json. Removing that line has removed the issue.