[英]Error: Uncaught [TypeError:default is not a constructor] jest mock
I have a service class FooService.js which I'm using in my component我有一个在我的组件中使用的服务类 FooService.js
export default class FooService {
saveInfo = (params) => {
//method implementation here.
};
}
I'm using this in my component Foo.tsx in saveData method as below:我在我的组件 Foo.tsx 中的 saveData 方法中使用它,如下所示:
const saveData = (message: string) => {
const fooService = new FooService();
fooService.saveInfo(params);
};
I'm trying to mock my FooService as below in Foo.test.tsx我正在尝试在 Foo.test.tsx 中模拟我的 FooService
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.当我在调试模式下从 Foo.test.tsx 文件运行我的测试用例时,我能够进入 saveData 方法。 But, when I hit
const fooService = new FooService();
但是,当我点击
const fooService = new FooService();
line, it throws the following error: TypeError: FooService_1.default is not a constructor行,它会引发以下错误: TypeError: FooService_1.default is not a constructor
Please let me know what is that I'm missing in jest.mock.请让我知道我在 jest.mock 中缺少什么。 Thanks in advance!
提前致谢!
用 __esModules 替换 _esModules 解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.