TypeError: (0 , _storeGenerator.storeGenerator) is not a function

TypeError: (0 , _storeGenerator.storeGenerator) is not a function

我最近弹出 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"
    },
}

我错过了什么?

根据杰斯特的说法:

映射到别名的模块默认是取消模拟的,无论是否启用自动模拟。

这意味着映射模块 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", "utils", __dirname],
    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"
    },
}