Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing Realm breaks React Native jest tests #313

Closed
jwhitley opened this issue Mar 6, 2016 · 6 comments
Closed

Importing Realm breaks React Native jest tests #313

jwhitley opened this issue Mar 6, 2016 · 6 comments

Comments

@jwhitley
Copy link

jwhitley commented Mar 6, 2016

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.

@appden
Copy link
Contributor

appden commented Mar 7, 2016

@jwhitley I just spoke with the maintainer of Jest (friend of mine) and he's working on a fix for this issue right now. It will expand on the changes here to include all react-native plugins.

@jwhitley
Copy link
Author

jwhitley commented Mar 8, 2016

@appden thanks, I'll keep an eye out for those changes. (and yeah, @cpojer's been killing it on the jest/react/react-native updates.)

@cpojer
Copy link

cpojer commented Mar 8, 2016

Working on it: jestjs/jest#779

@cpojer
Copy link

cpojer commented Mar 8, 2016

Sorry that this is happening. I wasn't aware of some of the setup steps in react-native. I don't think babel-jest ever worked for react-native before and I haven't been able to update the docs for jest + react-native integration yet.

@cpojer
Copy link

cpojer commented Mar 9, 2016

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

@appden
Copy link
Contributor

appden commented Mar 9, 2016

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

@appden appden closed this as completed Mar 9, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@cpojer @jwhitley @appden and others