我正在尝试使用jest编写单元测试用例,并需要模拟以下模式。我得到了TypeError:不是一个构造函数。 Usecase :我的用例如下 MyComponent.js: import serviceRegistry from "external/serviceRegistry";
serviceRegistry.getService("modulename", "servvice").then(
service => {
let myServiceInst
我试图在我的react原生项目中设置Jest,但它在bugsnag-react-native上运行得并不好。 在我当前的测试配置中,我看到了与bugsnag的leaveBreadcrumb函数相关的错误,如下所示: FAIL app/__tests__/NetworkReducer.test.js
● Test suite failed to run
TypeError: Cannot read property 'default' of undefined
at Object.<anonymous> (app/__tests__/
我有一个API类,我试图在一个React应用程序中使用它。
// API file
class API {
...
}
export default API;
// Other file
import API from "utils/API";
const api = new API();
我发现了一个错误:
TypeError: _API.default is not a constructor
但是..。好像我的默认设置好了?
我的Jest设置如下:
"jest": {
"setupFiles": [
"
我是使用UseHistory钩子在反应路由器v5.1.2与类型记录?在运行单元测试时,我遇到了问题。
TypeError:无法读取未定义的属性“历史记录”。
import { mount } from 'enzyme';
import React from 'react';
import {Action} from 'history';
import * as router from 'react-router';
import { QuestionContainer } from './QuestionsContai
我有服务舱。
Service.js
class Service {
}
export default new Service();
我正在尝试为此提供一个模拟实现。如果我使用这样的东西:
jest.mock('./Service', () => { ... my mock stuff });
它工作得很好,但是我不能访问在mock之外声明的任何变量,这有点限制,因为我想重新配置mock返回的内容,等等。
我尝试了一下(灵感来自另一篇StackOverflow文章:)
import service from './Service';
jest.mock(&
我在这里看到过关于嘲笑默认出口的问题,但我不认为这已经被问到了:
在模拟正在测试的模块的依赖项的默认导出时,如果模块使用ES6 import语句导入依赖项,则测试套件无法运行,但是,如果模块使用require().default调用,则声明TypeError: (0 , _dependency.default) is not a function It succeeds。
在我的理解中,import module from location直接翻译成const module = require(location).default,所以我很困惑为什么会发生这种情况。我宁愿保持代码风格的一致性,而
所有模拟尝试都会产生错误:
import useDebounce from 'use-debounce'
jest.mock('use-debounce')
TypeError: (0 , _useDebounce.useDebounce) is not a function or its return value is not iterable
尝试仅针对该模拟也会失败:
jest.mock('use-debounce',() => {
return {
useDebounce: jest.fn(value => [v
当调用模块中的函数时,我很难弄清楚如何用jest测试外部库方法。
鉴于这种打字本课程:
import transport from "app/customApiLibrary";
const { api } = transport;
// myServiceClass.ts
export class MyServiceClass {
getSomeStuff(): Promise<any> {
return api.get("myservice/end/point/");
}
}
let serviceStuff = new My
我有两个不同的库,用于在Jest中创建模拟程序。库具有相同的函数,名为get。对于我当前的实现来说,这是一个问题,因为两个不同的库都使用get,是否可以为模拟函数(jest.fn())使用别名,或者某种不会破坏当前实现的完整性的解决方法?
以下是我目前的实现,如果可能的话,我想保持这种方式:
let get: jest.Mock<{}>
jest.mock('rxjs/ajax', () => {
get = jest.fn()
return { ajax: { get } }
})
let get as cookieGet: jest.Mock&l