Jest setup not valid when importing ES6 modules
See original GitHub issueBug
When importing functions with ES6 modules and running jest tests, NativeModules.RNDeviceInfo
is not defined.
Environment info
React native info output:
info Fetching system and libraries information...
System:
OS: macOS 10.15.1
CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Memory: 1.27 GB / 16.00 GB
Shell: 2.7.1 - /usr/local/bin/fish
Binaries:
Node: 10.14.0 - ~/.config/fnm/bin/node
Yarn: 1.17.3 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
IDEs:
Xcode: 11.3/11C29 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.6 => 16.8.6
react-native: 0.60.4 => 0.60.4
npmGlobalPackages:
create-react-native-app: 2.0.2
react-native-git-upgrade: 0.2.7
Library version: 5.4.0
Steps To Reproduce
- Install the library
- Use in a component and create a jest test for it.
- Use the device info library, importing a function as an ES6 module. …
Describe what you expected to happen:
- Test to not error for NativeModules error
Reproducible sample code
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
JEST and ES6 import - root folder based imports does not ...
In Jest tests: when I trying to import something with root-based path like "components/Link" - it does not work. Only relative paths are...
Read more >Configuring Jest
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have...
Read more >Anyone gotten unit tests working with ES6 modules?
Does anyone have a working test setup and "module": "es6", ... you are trying to import a file which Jest cannot parse, e.g....
Read more >TSConfig Option: esModuleInterop - TypeScript
the ES6 modules spec states that a namespace import ( import * as x ) can only be an object, ... That's not...
Read more >syntaxerror cannot use import statement outside a module ...
Jest doesn't support ES6 module and hence throwing this error when you directly run the test with Jest. if you want to run...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
Yea, I couldn’t figure out why it didn’t work. Maybe a simple DX approach would be to add a “Testing with Jest” section to the readme that has the mock ready for copy/pasting? I grabbed the export from that commit and dropped it into my project and was ready to go.
It would still require some additional maintenance, but would maybe encourage some helpful PRs to update the readme instead of Issues that complain about it failing the test?
Either way, this issue should stay closed. Thanks for taking a look!
PRs to the README happily accepted 😅 🙏