问题描述
我最近弹出了 Create-React-App
并重新配置了很多东西!但是,我仍然无法进行测试...我收到以下错误:
TypeError: (0,_storeGenerator.storeGenerator) is not a function
这发生在 src/store/__mocks__/store.js
内,我正在调用 storeGeneratore,如下所示;
import storeGenerator from '../storeGenerator';
import TEST_STATE from '../../__mockData__/testState';
import { ROOT_ACTION_TYPES } from './reducers';
const { store,saga } = storeGenerator(TEST_STATE); <-- this line fails
const resetState = () => {
store.dispatch({ type: ROOT_ACTION_TYPES.setState,payload: TEST_STATE });
};
export { saga,resetState };
export default store;
这是我的 jestConfig:
module.exports = {
setupFilesAfterEnv: [
"<rootDir>/src/setupTests.js"
],coverageReporters: [
"html","text","cobertura"
],snapshotSerializers: [
"enzyme-to-json/serializer"
],transformIgnorePatterns: [
"node_modules/?!(ezyvet-react-ui)","node_modules/?!(ezyvet-fe-helpers)","node_modules/?!(ezyvet-change-management-tools)"
],moduleNameMapper: {
"test-utils": "<rootDir>/src/test-utils",},collectCoverageFrom: [
"src/**/*.{ts,tsx,js,jsx}","!**/__*__/**/*","!src/**/index.{ts,js}","!src/**/types/*","!src/**/types.ts","!src/react-app-env.d.ts","!src/serviceWorker.ts","!src/setupProxy.js","!src/setupTests.js","!src/test-utils/*","!src/wellnessplan/**/*","!src/main/components/widgetpanel/autoprefix.js"
],transform: {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js","^.+\\.css$": "<rootDir>/config/jest/csstransform.js","^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},}
我错过了什么?
解决方法
根据 Jest 的说法:
无论是否启用自动模拟,映射到别名的模块默认都不会被模拟。
这意味着映射模块 test-utils
正在尝试导入未模拟的模块,因此导入错误。
如果你想为你的测试创建一个别名,比如我一直在尝试做的“test-utils”,你应该改用 moduleDirectories
。
ModuleDirectories
是要从所需模块的位置向上递归搜索的目录名称数组。默认值为 node_modules,实际上这就是 Jest 将第三方库导入测试的方式。
以下示例将为名为“utils”的文件夹创建别名:
moduleDirectories: ["node_modules","utils",__dirname],
以下是更改后我的配置的样子:
module.exports = {
setupFilesAfterEnv: [
"<rootDir>/src/setupTests.js"
],coverageReporters: [
"html","text","cobertura"
],snapshotSerializers: [
"enzyme-to-json/serializer"
],transformIgnorePatterns: [
"node_modules/?!(ezyvet-react-ui)","node_modules/?!(ezyvet-fe-helpers)","node_modules/?!(ezyvet-change-management-tools)"
],moduleDirectories: ["node_modules",collectCoverageFrom: [
"src/**/*.{ts,tsx,js,jsx}","!**/__*__/**/*","!src/**/index.{ts,js}","!src/**/types/*","!src/**/types.ts","!src/react-app-env.d.ts","!src/serviceWorker.ts","!src/setupProxy.js","!src/setupTests.js","!src/test-utils/*","!src/wellnessplan/**/*","!src/main/components/widgetpanel/autoprefix.js"
],transform: {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js","^.+\\.css$": "<rootDir>/config/jest/cssTransform.js","^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},}