简体   繁体   English

错误:未捕获 [TypeError:default is not a constructor] jest mock

[英]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.

相关问题 Jest Mock 默认构造函数 - Jest Mock Default Constructor 未捕获的类型错误:_konva.default.stage 不是构造函数 - Uncaught TypeError: _konva.default.stage is not a constructor 开玩笑错误:未捕获 [TypeError:XXX 不是函数] - Jest Error: Uncaught [TypeError: XXX is not a function] 如何使用 Jest 从 axios-hooks 模拟 useAxios 钩子? (错误:未捕获 [TypeError: undefined is not a function]) - How can I mock useAxios hook from axios-hooks using Jest? (Error: Uncaught [TypeError: undefined is not a function]) Jest:模拟构造函数 - Jest : mock constructor function 角4:错误:未捕获(承诺中):TypeError:X不是构造函数 - Angular 4: Error: Uncaught (in promise): TypeError: X is not a constructor 错误:未捕获类型错误:非法构造函数 JavaScript - error: Uncaught TypeError: Illegal constructor JavaScript 获取未捕获的TypeError:...默认不是构造函数 - 来自Vue组件 - Getting Uncaught TypeError: …default is not a constructor - from Vue component 未捕获的类型错误:_models_Search__WEBPACK_IMPORTED_MODULE_0___default.a 不是构造函数 - Uncaught TypeError: _models_Search__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor 木偶:未被捕获的TypeError:*不是构造函数 - Marionette: Uncaught TypeError: * is not a constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM