Realm-js: Jest Tests fails

Created on 9 Sep 2016  Â·  9Comments  Â·  Source: realm/realm-js

Hello I'm getting this error when running the test with Jest. When I run my application it works correctly but in the test it fails.

Test suite failed to run

    Missing Realm constructor - please ensure RealmReact framework is included!

      at Object.<anonymous> (node_modules/realm/lib/index.js:37:11)
      at Object.<anonymous> (app/models.js:6:12)
      at Object.<anonymous> (__tests__/models/location.test.js:2:13)

This is my configuration

Package.json

{
  "name": "App",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "jest": {
    "preset": "jest-react-native"
  },
  "dependencies": {
    "react": "15.3.1",
    "react-native": "0.32.1",
    "realm": "^0.14.3"
  },
  "devDependencies": {
    "babel-jest": "^15.0.0",
    "babel-preset-react-native": "^1.9.0",
    "eslint": "^3.4.0",
    "eslint-plugin-react": "^6.2.0",
    "eslint-plugin-react-native": "^2.0.0",
    "jest": "^15.1.1",
    "jest-react-native": "^15.0.0",
    "react-test-renderer": "^15.3.1"
  }
}

app/models.js

import Realm from 'realm';
// More imported models
class Location {
  get readableLocation() {
    return `${this.city} - ${this.country} - ${this.latitude} - ${this.longitude}`;
  }
}

Location.schema = {
  name: 'Location',
  properties: {
    city: 'string',
    country: 'string',
    latitude: 'double',
    longitude: 'double'
  }
};

export default new Realm({
  schema: [Location /* More schemas */]
});


Test

jest.unmock('../../app/models');
import realm from '../../app/models';
let context = describe;
describe('Location', () => {
  context('.readableLocation', () => {

    beforeEach(() => {
      realm.write(() => {
        realm.create('Location',
          {
            city: 'Medellín',
            country: 'Colombia',
            latitude: 123,
            longitude: 1231
          }
        );
      });

    });

    it('returns the full locations with city, country, latitude, longitude', () => {
      let location = realm.objects('Location')[0];
      let expected = `${location.city} - ${location.country} - ${location.latitude} - ${location.longitude}`;
      expect(location.readableLocation).toBe(expected);
    });


  });
});

EDIT
I ran rnpm link realmand added the new RealmReactPackage()to the Main application

T-Bug

Most helpful comment

Hey folks,

We've been super busy with the recent launch of the Realm Mobile Platform and we haven't had the time to investigate these Jest hangs. I'll try to make time to look into the matter as soon as I can.

All 9 comments

@alexsotocx try installing realm from git and follow these steps: https://github.com/realm/realm-js/issues/521#issuecomment-245262476
I had the same issue when using 0.14.3

@sharq1 It works but the terminal never ends the process so the tests nevers ends. But atleast I can test that now :)
screenshot 2016-09-12 10 00 39

Edit:
Only the terminal hangs on realm tests

Hey folks,

We've been super busy with the recent launch of the Realm Mobile Platform and we haven't had the time to investigate these Jest hangs. I'll try to make time to look into the matter as soon as I can.

Is there any progress? Is there a solution without modifying files in node_modules and with an ending terminal process?

Our error logs:

  1. with realm github master
    import { NativeModules } from 'react-native';
    ^^^^^^
    SyntaxError: Unexpected token import

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:284:10)
      at Object.<anonymous> (node_modules/realm/lib/index.js:30:24)
      at Object.<anonymous> (src/common/database/realm.js:1:4227)
  1. with realm v0.14.3
    Could not locate the bindings file. Tried:
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/build/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/build/Debug/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/build/Release/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/out/Debug/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/Debug/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/out/Release/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/Release/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/build/default/realm.node
     → /Users/r-technik-mbpro-02/Documents/_projectDir_/node_modules/realm/compiled/6.3.0/darwin/x64/realm.node

      at bindings (node_modules/bindings/bindings.js:88:9)
      at Object.<anonymous> (node_modules/realm/lib/index.js:35:41)
      at Object.<anonymous> (src/common/database/realm.js:1:4227)
      at Object.<anonymous> (src/RentStationsView/index.js:10:12)

Any update?

Sorry, no updates yet.

hey @jaimecorrea, if you install the latest rc jest should work: npm install --save [email protected]

0.15.1-rc still hangs for me, but adding realm.close fixes that.

https://github.com/realm/realm-js/issues/521#issuecomment-260219370

This is still causing issues for me. Just importing Realm and not using it at all is enough to prevent Jest from ever finishing.

Was this page helpful?
0 / 5 - 0 ratings