【问题标题】:Jest throwing ReferenceError to default exports开玩笑将 ReferenceError 扔到默认导出
【发布时间】:2021-09-25 09:54:35
【问题描述】:

Jest 无法找到 export default 的函数,但能够找到 export const。我可以重新定义所有函数的导出/导入方式,但我觉得这可能只是一个配置问题,但在文档或 github 问题上找不到任何解决方案来解决它。

有没有人知道一些可以用来解决这个问题的笑话配置?

不错 文件:

export const MyFunction = () => {..

规格:

import { MyFunction } from "src/MyFunction";
=>   ● Pass

不好 文件:

export default MyFunction = () => {..

规格:

import MyFunction from "src/MyFunction";
=>   ● Test suite failed to run
    ReferenceError: MyFunction is not defined

我的jest.config.js

/*
 * For a detailed explanation regarding each configuration property, visit:
 * https://jestjs.io/docs/en/configuration.html
 */

module.exports = {
  // All imported modules in your tests should be mocked automatically
  // automock: false,

  // Automatically restore mock state between every test
  restoreMocks: true,

  // Make calling deprecated APIs throw helpful error messages
  errorOnDeprecated: true,

  // An array of directory names to be searched recursively up from the requiring module's location
  moduleDirectories: ["node_modules", "src", "test/unit"],

  // The test environment that will be used for testing
  testEnvironment: "node",

  // The glob patterns Jest uses to detect test files
  testMatch: ["**/test/**/**/*.spec.(js|jsx|ts|tsx)"],

  transformIgnorePatterns: [
    "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)",
  ],

  transform: {
    "\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
  },

  // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
  testPathIgnorePatterns: ["/node_modules/"],

  reporters: ["default", "jest-junit"],

  collectCoverage: true,
  coverageReporters: ["lcov", "text-summary"],
  coveragePathIgnorePatterns: [
    "/node_modules/",
    "src/img/",
    "src/styles/",
    "test/factories/",
    "test/fixtures/",
  ],

  // Whether to use watchman for file crawling
  watchman: true,

  setupFilesAfterEnv: ["@testing-library/jest-native/extend-expect"],
  preset: "jest-expo",
  globals: {
    __DEV__: true,
    THEME: true,
    SEGMENT_KEY_STORE_INFO: true,
    INITIAL_STATE: true,
    EMPTY_MESSAGE: true,
  },
};

【问题讨论】:

    标签: javascript reactjs react-native jestjs react-testing-library


    【解决方案1】:

    导出默认 MyFunction = () => {.. 将此更改为 导出默认 const MyFunction = () => {..

    【讨论】:

    • 感谢您的回复,但这将呈现SyntaxError,因为导出选项为default const
    【解决方案2】:

    好吧,伙计们在这里愚蠢,但解决方案是将默认导出更改为: MyFunction.js:

    export default () => {..
    

    即在导出默认函数声明中删除名称。希望这对遇到此问题的人有所帮助。

    【讨论】:

    • 或者简单地先用const MyFunction = () =&gt; {声明函数,然后在声明之后然后将其导出为export default MyFunction;
    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 2021-09-13
    • 2021-02-19
    • 1970-01-01
    • 2022-07-12
    • 2020-05-29
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多