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.

Jest setup not valid when importing ES6 modules

See original GitHub issue

Bug

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

  1. Install the library
  2. Use in a component and create a jest test for it.
  3. Use the device info library, importing a function as an ES6 module. …

Describe what you expected to happen:

  1. Test to not error for NativeModules error

Reproducible sample code

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
jefflewiscommented, Jan 3, 2020

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!

0reactions
mikehardycommented, Jan 3, 2020

PRs to the README happily accepted 😅 🙏

Read more comments on GitHub >

github_iconTop 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 >

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