Jest breaking when importing component using state class property
See original GitHub issue- Review the documentation: https://facebook.github.io/react-native
- Search for existing issues: https://github.com/facebook/react-native/issues
- Use the latest React Native release: https://github.com/facebook/react-native/releases
Environment
React Native Environment Info:
System:
OS: macOS 10.14.1
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 496.23 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 8.11.2 - /usr/local/bin/node
Yarn: 1.6.0 - ~/.yarn/bin/yarn
npm: 5.6.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
Android SDK:
API Levels: 21, 22, 23, 24, 25, 26, 27, 28
Build Tools: 23.0.1, 24.0.1, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 27.0.3, 28.0.2, 28.0.3
System Images: android-22 | Google APIs Intel x86 Atom, android-24 | Google APIs Intel x86 Atom
IDEs:
Android Studio: 3.2 AI-181.5540.7.32.5056338
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
react: ^16.6.3 => 16.6.3
react-native: ^0.57.5 => 0.57.5
npmGlobalPackages:
create-react-native-app: 1.0.0
react-native-cli: 2.0.1
react-native: 0.57.5
Additionally I`m using these babel packages:
"@babel/cli": "^7.1.5",
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"@babel/runtime": "^7.1.5",
"babel-jest": "^23.6.0",
"babel-plugin-jest-hoist": "^23.2.0",
Description
When I import a component like the example below:
export default class ButtonHold extends React.PureComponent<Props> {
state = {
locked: true,
unlocking: false,
buttonWidth: 0
}
....
}
the test produces the following error:
FAIL src/components/button-hold.test.js
<ButtonHold>
Structure
✕ renders correctly (6ms)
● <ButtonHold> › Structure › renders correctly
TypeError: Cannot read property 'default' of undefined
14 | }
15 |
> 16 | export default class ButtonHold extends React.PureComponent<Props> {
| ^
17 | static defaultProps = {
18 | style: undefined,
19 | description: undefined
at new ButtonHold (src/components/button-hold.js:16:423)
at ReactShallowRenderer.render (node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js:398:26)
at node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:480:35
at withSetStateAllowed (node_modules/enzyme-adapter-utils/build/Utils.js:137:16)
at Object.render (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:479:68)
at new ShallowWrapper (node_modules/enzyme/build/ShallowWrapper.js:204:22)
at shallow (node_modules/enzyme/build/shallow.js:21:10)
at Object.<anonymous> (src/components/button-hold.test.js:11:41)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.242s
This only happens when the state is defined as class property, when I define it inside the constructor, the test runs normally
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Jest tests breaks when calling getState outside a component ...
A bunch of my tests suddenly started breaking when I use store.getState() inside a function in React. If I log the store ,...
Read more >Testing state changes in React functional components
Learn how to test your React functional components and the state changes for components that use hooks with Jest and Enzyme as testing ......
Read more >How to Test React Components: the Complete Guide
Jest is the environment where all your tests are actually executed. This is why you do not need to import expect and describe...
Read more >Configuring Jest
Jest will run .mjs and .js files with nearest package.json 's type field set to module as ECMAScript Modules.
Read more >undefined. jest - You.com | The AI Search Engine You Control
React Jest Testing TypeError: Cannot read properties of undefined (reading ... facebook/react-nativeJest breaking when importing component using state class ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @sasurau4 , it worked! Thanks for the help!
Just for someone that may endup here in this issue, the workaround consists in:
preprocessor.js
file inside react-native jest folder, normally located in this path<rootDir>/node_modules/react-native/jest/preprocessor.js
andinlineRequires: true
toinlineRequires: false
Jest config file:
packege.json