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.

Importing Realm breaks React Native jest tests

See original GitHub issue

Importing Realm into a React Native project breaks existing jest tests. This is probably partially related to #262, but the error in question is a decidedly opaque syntax error, vs. anything going on at runtime. I.e. I can’t even run tests that mock Realm out.

Repro steps

  1. git clone https://github.com/jwhitley/ReactNativeTesting
  2. cd ReactNativeTesting
  3. npm install
  4. npm test
  5. Observe that the (trivial) tests pass without Realm.
  6. git checkout test-with-realm
  7. npm install (to install realm)
  8. npm test

Actual results

If the realm npm package is anywhere in the import chain in the tests, the following syntax error occurs:

$ npm test

> ReactNativeTesting@0.0.1 test /Users/whitley/src/ReactNativeTesting
> jest

Using Jest CLI v0.9.0, jasmine2, babel-jest
 FAIL  __tests__/sum-test.js (0.017s)
● sum › it adds 1 + 2 to equal 3
  - SyntaxError: Unexpected token {
        at eval (native)
        at Object.<anonymous> (node_modules/realm/lib/index.js:26:22)
        at Object.<anonymous> (sum.js:1:131)
        at Object.eval (__tests__/sum-test.js:5:9)
1 test failed, 0 tests passed (1 total in 1 test suite, run time 0.803s)
npm ERR! Test failed.  See above for more details.

Expected results

Tests pass as before importing Realm. While the ability to run full integration tests under node would be the endgame, I’d be overjoyed to just have unit tests with Realm mocked out actually run.

Notes

The test-with-realm branch has just two changes:

  1. It adds the realm npm package.
  2. It adds import Realm from 'realm' to the trivial sum.js module being tested.

This was motivated by adding Realm to a new RN project our company has started building, and watching the tests blow up with the above error.

The sample repo uses the about-to-be standard setup for jest testing with React Native. See discussion at facebook/react-native#6315. Of particular note is babel-plugin-react-native, which extracts the RN babel configuration into a first-class plugin, usable by other workflows than the RN packager.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
appdencommented, Mar 9, 2016

Thanks @cpojer! I confirmed that upgrading the reduction provided by @jwhitley resulted in it successfully passing the test. 👍

0reactions
cpojercommented, Mar 9, 2016

Published jest 0.9.1 and babel-jest 9.0.2 which should resolve this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest & RN & Realm : Error: Cannot find module ' ...
I'm using jest for unit testing in a react native app, which usually works fine. Trying to use realm breaks the tests and...
Read more >
Testing - React Native SDK — Realm
When testing the Realm React Native SDK, you must close realms with Realm.close() after you're done with them to prevent memory leaks.
Read more >
testing-library/react-native
Simple and complete React Native testing utilities that encourage good testing practices.. Latest version: 11.5.0, last published: a month ...
Read more >
Testing React Native Apps
Jest searches for all test files and executes them from a single command. And here is what our execution and results will appear:...
Read more >
[Solved]-Testing Realm react-native with Jest-React Native
[Solved]-Testing Realm react-native with Jest-React Native ... Linking realm ios dependency rnpm-link info iOS module realm has been successfully linked.
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