kykyi
kykyi

Reputation: 385

Jest throwing ReferenceError to default exports

Jest is unable to find functions which are export default but IS able to find 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.

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:

/*
 * 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,
  },
};

Upvotes: 2

Views: 1553

Answers (2)

kykyi
kykyi

Reputation: 385

Ok guys stupid one here, but the solution is to change the default export to: MyFunction.js:

export default () => {..

Ie drop the name in the export default function declaration. Hope this helps someone having this issue.

Upvotes: 0

Kavitha K T
Kavitha K T

Reputation: 119

export default MyFunction = () => {.. Change this to export default const MyFunction = () => {..

Upvotes: 1

Related Questions