es-jest icon indicating copy to clipboard operation
es-jest copied to clipboard

jest.mock doesn't work with es-jest

Open atlanteh opened this issue 2 years ago • 1 comments

As the title says, jest.mock doesn't work with es-jest. Adding above files in this repo and running the tests fail. This totally blocks us. Can you please help?

// file myClass.ts
export default class MyClass {
    doSomething() {
        return 'foo'
    }
}
// file test/myClass.test.ts
import MyClass from '../myClass';
jest.mock('../myClass');

const MockedMyClass = jest.mocked(MyClass, true);

// fails here on TypeError: MockedMyClass.mockImplementation is not a function
MockedMyClass.mockImplementation(() => ({
  doSomething: () => 'bar',
}))

test('MyClass', () => {
  const myClass = new MyClass();
  expect(myClass.doSomething()).toEqual('bar')
})


atlanteh avatar Feb 23 '23 18:02 atlanteh

This is a limitation of esbuild, I personally recommend you switch to @swc/jest to use jest.mock.

ambar avatar Feb 24 '23 05:02 ambar