Error: Uncaught [TypeError:default is not a constructor] jest mock

826 Views Asked by At

I have a service class FooService.js which I'm using in my component

export default class FooService {
  saveInfo = (params) => {
    //method implementation here.
  };
}

I'm using this in my component Foo.tsx in saveData method as below:

const saveData = (message: string) => {
    const fooService = new FooService();
    fooService.saveInfo(params);
  };

I'm trying to mock my FooService as below in Foo.test.tsx

jest.mock('src/js/services/FooService', () => {
  return {
    _esModule: true,
    default: jest.fn().mockImplementation(() => {
      return {
        saveInfo: jest.fn(),
      };
    }),
  };

When I run my test case from Foo.test.tsx file in debug mode, I'm able to get inside saveData method. But, when I hit const fooService = new FooService(); line, it throws the following error: TypeError: FooService_1.default is not a constructor

Please let me know what is that I'm missing in jest.mock. Thanks in advance!

1

There are 1 best solutions below

0
On

Replacing _esModules with __esModules solved the issue.