首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向默认导出抛出ReferenceError

向默认导出抛出ReferenceError
EN

Stack Overflow用户
提问于 2021-07-16 15:28:56
回答 2查看 775关注 0票数 2

Jest无法找到属于export default的函数,但能够找到export const。我可以查看并重新定义我的所有函数是如何导出/导入的,但是我觉得这很可能只是一个配置问题,但是无法找到任何关于docs或github问题的解决方案。

有人知道一些可用于解决此问题的jest配置吗?

好的文件:

代码语言:javascript
复制
export const MyFunction = () => {..

规格:

代码语言:javascript
复制
import { MyFunction } from "src/MyFunction";
=>   ● Pass

文件:

代码语言:javascript
复制
export default MyFunction = () => {..

规格:

代码语言:javascript
复制
import MyFunction from "src/MyFunction";
=>   ● Test suite failed to run
    ReferenceError: MyFunction is not defined

我的jest.config.js

代码语言:javascript
复制
/*
 * 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,
  },
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-16 15:50:53

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

票数 1
EN

Stack Overflow用户

发布于 2021-07-16 16:39:30

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

代码语言:javascript
复制
export default () => {..

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68411621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档