简体   繁体   English

开玩笑地将 ReferenceError 抛出到默认导出

[英]Jest throwing ReferenceError to default exports

Jest is unable to find functions which are export default but IS able to find export const . Jest 无法找到export default函数,但可以找到export const I can go through and redefine how all of my functions are exported/imported, but I feel this is likely just a config issue but have been unable to find any solution on the docs or github issues to solve it.我可以重新定义我所有函数的导出/导入方式,但我觉得这可能只是一个配置问题,但一直无法在文档或 github 问题上找到任何解决方案来解决它。

Does anyone know of some jest config which can be used to resolve this?有谁知道一些可用于解决这个问题的笑话配置?

GOOD file:文件:

export const MyFunction = () => {..

spec:规格:

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

BAD file:文件:

export default MyFunction = () => {..

spec:规格:

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

My jest.config.js :我的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,
  },
};

export default MyFunction = () => {.. 将此更改为 export default const MyFunction = () => {..

Ok guys stupid one here, but the solution is to change the default export to: MyFunction.js :好吧,这里的人很愚蠢,但解决方案是将默认导出更改为: MyFunction.js

export default () => {..

Ie drop the name in the export default function declaration.即在导出默认函数声明中删除名称。 Hope this helps someone having this issue.希望这可以帮助遇到此问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM