question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'default' of undefined when testing with Jest

See original GitHub issue

Environment

OS: MacOS 10.14.5 Target platform: Jest react-native-vector-icons version: 6.6.0 Jest version: 24.8.0

Description

Describe your issue in detail. Include screenshots if needed. I am facing an issue with react-native-vector-icons running in my tests with Jest.

The error:

 TypeError: Cannot read property 'default' of undefined

      14 |   test("second step register render correctly", () => {
      15 |     // @ts-ignore
    > 16 |     const wrapper = mount<SecondStepRegisterScreenImpl>(<SecondStepRegisterScreenImpl navigation={{}}/>)
         |                     ^
      17 |     wrapper.render()
      18 | 
      19 |     expect(wrapper.find(SafeAreaView)).toExist()

      at new Icon (node_modules/react-native-vector-icons/lib/create-icon-set.js:42:399)
      at constructClassInstance (node_modules/react-dom/cjs/react-dom.development.js:11361:18)
      at updateClassComponent (node_modules/react-dom/cjs/react-dom.development.js:14687:5)
      at beginWork (node_modules/react-dom/cjs/react-dom.development.js:15644:16)

First I am using react-native with ignite-bowser template which use typescript for react-native. ref: infinitered/ignite-bowser

Bellow my Jest configuration: the “jest-preset-ignite” preset: infinitered/jest-preset-ignite

module.exports = {
  preset: "jest-preset-ignite",
  transformIgnorePatterns: [
    "<rootDir>/node_modules/(?!(jest-)?react-native|react-native|react-navigation|@react-navigation|@storybook|@react-native-community|@expo)"
  ],
  setupFilesAfterEnv: [
    "<rootDir>/test/setup-enzyme.ts"
  ],
  moduleNameMapper: {
    "^lodash-es$": "lodash"
  }
};

I have attempt to mock react-native-vector-icons package like that:

jest.mock("react-native-vector-icons")

But with no results.

Did I do something wrong?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:15

github_iconTop GitHub Comments

10reactions
RageOfJusticecommented, Sep 19, 2019

Hello. I have the same issue. Im new in jest but found (temporary?) solution: Add constructor for Icon in create-vector-icon/lib/create-icon-set.js line 41

image

5reactions
mike-lawsoncommented, Mar 1, 2021

These workarounds no longer appear to resolve this issue. I have the following configuration:

os: Ubuntu 20.04.1 LTS nodejs : v12.20.1 react-native: 0.62.2 jest: “^26.0.0”, ts-jest: “^26.5.1” react-native-vector-icons: “^6.6.0”,

Jest config:

 "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "ts-jest"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!react-native)"
    ],
    "setupFiles": [
      "<rootDir>/__mocks__/setup.tsx"
    ]
  },

Adding the constructor option to the Icon definition still presents me with the Cannot read property 'default' of undefined error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test suite failed to run TypeError: Cannot read property ...
Test suite failed to run TypeError: Cannot read property 'default' of undefined. Save this question. Show activity on this post. I'm trying to ......
Read more >
Jest: TypeError: Cannot read property 'default' of undefined
Even though the project builds just fine in both development and production mode. This usually stems from cyclic dependencies.
Read more >
typeerror: cannot read property 'location' of undefined jest
Vue jest: Error in created hook: "TypeError: Cannot read property 'getters' of undefined" ... Jest Cannot read property 'default' of undefined during test....
Read more >
Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... By default, Jest runs all tests and produces all errors into the console ......
Read more >
Test suite failed to run TypeError: Cannot read property ...
[Solved]-Test suite failed to run TypeError: Cannot read property 'default' of undefined-Reactjs · score:8. Accepted answer · score:0. I solved this by simply ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found