TypeError: _API.default is not a constructor with Jest tests
TypeError: _API.default is not a constructor with Jest tests
我有一个 API class,我想在 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": [
"./jestSetupFile.js"
],
"testEnvironment": "jsdom",
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|react-router-native/.*|@invertase/react-native-apple-authentication/.*)"
]
},
我的 strong 猜测这是由于我的 babel、webpack 或 package.json.
的配置所致
可能是什么原因造成的?
请注意,我想明确一点,这在我的主应用程序中根本不会发生,仅 在 Jest 测试中
如果我将其更改为命名的 export/import,我会得到:
TypeError: _API.API is not a constructor
极度令人困惑的行为。
您需要像这样导出它:
export default class API
你可以试试:
utils/API.js
export default class API {
...
}
test.js
import API from "utils/API";
const api = new API();
正在尝试在您的 tsconfig.json
中添加 "esModuleInterop": true,
。默认情况下 esModuleInterop
设置为 false 或未设置。 B 将 esModuleInterop 设置为 true 会更改编译器的行为并修复一些 ES6 语法错误。
请参阅文档 here.
这最终是由于我从中导出 class 的文件中有额外的代码。
import { store } from "root/App";
if (typeof store !== "undefined") {
let storeState = store.getState();
let profile = storeState.profile;
}
在顶部,在我的 class 之外,我一直在处理一些功能。
这导致 class 默认导出失败,但 仅 在 Jest 中,而不是在我的实际应用程序中。
正如其他人所提到的,看到一个最小的可重现示例会很有帮助。
但是,还有一个可能的原因。你在嘲笑你的测试文件中的 API class 吗?如果 class 被错误地模拟为“对象”而不是函数,有时会发生此问题。无法使用“new”运算符实例化对象。
例如,假设我们有一个 class 文件 utils/API
,如下所示:
class API {
someMethod() {
// Does stuff
}
}
export default API;
以下是模拟此 class 的“不正确”方法,如果在创建模拟后实例化 class,将抛出 TypeError... is not a constructor
错误。
import API from 'utils/API';
jest.mock('utils/API', () => {
// Returns an object
return {
someMethod: () => {}
};
})
// This will throw the error
const api = new API();
下面将把 class 模拟为一个函数,并接受 new
运算符,不会抛出错误。
import API from 'utils/API';
jest.mock('utils/API', () => {
// Returns a function
return jest.fn().mockImplementation(() => ({
someMethod: () => {}
}));
})
// This will not throw an error anymore
const api = new API();
我有一个 API class,我想在 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": [
"./jestSetupFile.js"
],
"testEnvironment": "jsdom",
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|react-router-native/.*|@invertase/react-native-apple-authentication/.*)"
]
},
我的 strong 猜测这是由于我的 babel、webpack 或 package.json.
的配置所致可能是什么原因造成的?
请注意,我想明确一点,这在我的主应用程序中根本不会发生,仅 在 Jest 测试中
如果我将其更改为命名的 export/import,我会得到:
TypeError: _API.API is not a constructor
极度令人困惑的行为。
您需要像这样导出它:
export default class API
你可以试试:
utils/API.js
export default class API {
...
}
test.js
import API from "utils/API";
const api = new API();
正在尝试在您的 tsconfig.json
中添加 "esModuleInterop": true,
。默认情况下 esModuleInterop
设置为 false 或未设置。 B 将 esModuleInterop 设置为 true 会更改编译器的行为并修复一些 ES6 语法错误。
请参阅文档 here.
这最终是由于我从中导出 class 的文件中有额外的代码。
import { store } from "root/App";
if (typeof store !== "undefined") {
let storeState = store.getState();
let profile = storeState.profile;
}
在顶部,在我的 class 之外,我一直在处理一些功能。
这导致 class 默认导出失败,但 仅 在 Jest 中,而不是在我的实际应用程序中。
正如其他人所提到的,看到一个最小的可重现示例会很有帮助。
但是,还有一个可能的原因。你在嘲笑你的测试文件中的 API class 吗?如果 class 被错误地模拟为“对象”而不是函数,有时会发生此问题。无法使用“new”运算符实例化对象。
例如,假设我们有一个 class 文件 utils/API
,如下所示:
class API {
someMethod() {
// Does stuff
}
}
export default API;
以下是模拟此 class 的“不正确”方法,如果在创建模拟后实例化 class,将抛出 TypeError... is not a constructor
错误。
import API from 'utils/API';
jest.mock('utils/API', () => {
// Returns an object
return {
someMethod: () => {}
};
})
// This will throw the error
const api = new API();
下面将把 class 模拟为一个函数,并接受 new
运算符,不会抛出错误。
import API from 'utils/API';
jest.mock('utils/API', () => {
// Returns a function
return jest.fn().mockImplementation(() => ({
someMethod: () => {}
}));
})
// This will not throw an error anymore
const api = new API();