Importing Realm breaks React Native jest tests
See original GitHub issueImporting 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
git clone https://github.com/jwhitley/ReactNativeTesting
cd ReactNativeTesting
npm install
npm test
- Observe that the (trivial) tests pass without Realm.
git checkout test-with-realm
npm install
(to installrealm
)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:
- It adds the
realm
npm package. - It adds
import Realm from 'realm'
to the trivialsum.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:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Thanks @cpojer! I confirmed that upgrading the reduction provided by @jwhitley resulted in it successfully passing the test. 👍
Published jest 0.9.1 and babel-jest 9.0.2 which should resolve this.