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.

[JS: Testing] require('react-native') and jest test fails

See original GitHub issue

I have the following simple Module

/**
 * @providesModule DropBox
 * @flow
 */
'use strict';

var React = require('react-native');
var {
   NativeModules,
    } = React;

var _ = require('underscore')._;

class DropBox {

    constructor() {
        this.nativeModule = NativeModule.DropBox;
    }

    loadFiles() {
        return new Promise((resolve, reject) => {
            this.nativeModule.load((error, names, contents) => {
                if (error) {
                    reject(error);
                } else {
                    var parsedContent = contents.map((c) => {
                        return JSON.parse(c);
                    });
                    resolve(_.zip(names, parsedContent));
                }
            });
        })
    }
}

module.exports = DropBox;

and the following test:

jest.dontMock('../js/model/DropBox');

describe('loadFiles', function() {
 it('adds 1 + 2 to equal 3', function() {
   var DropBox = require('../js/model/DropBox');
   expect(1 + 21).toBe(3);
 });

});

I know that the test is nonsense, but I just want to make sure that my setup works. Running npm test gives:

  • TypeError: /Users/roger/Documents/AwesomeProject/js/model/DropBox.js: /Users/roger/Documents/AwesomeProject/node_modules/react-native/Libraries/react-native/react-native.js: /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/browser/ui/React.js: /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/modern/class/ReactComponent.js: /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/core/ReactUpdateQueue.js: /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/core/ReactUpdates.js: undefined is not a function at /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/core/ReactUpdates.js:26:39 at /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/core/ReactUpdateQueue.js:18:20 at /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/modern/class/ReactComponent.js:14:24 at /Users/roger/Documents/AwesomeProject/node_modules/react-tools/src/browser/ui/React.js:18:22 at /Users/roger/Documents/AwesomeProject/node_modules/react-native/Libraries/react-native/react-native.js:20:47

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
brentvatnecommented, May 30, 2015

I think we need a section in the docs about how to test your React Native components

4reactions
cpojercommented, Jul 27, 2016

Thank you for your patience. We launched Jest 14.0 with experimental react-native support:

Please feel free to create new issues after trying out the new integration if any issues remain.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Test Fail in React Native - Stack Overflow
The error I now see when executing the tests is: > trytest@0.0.1 test /home/xxxxx/MyAwesomeApp > jest FAIL __tests__/myfirst.test.js * Test ...
Read more >
Testing React Native Apps - Jest
The snapshot should be committed along with code changes. When a snapshot test fails, you need to inspect whether it is an intended...
Read more >
Mocking RN modules | React Made Native Easy
i have introduced a native module called 'react-native-gps-state' in my personal project,but after running tests suits i'm getting error of "Invarient Voilation ...
Read more >
Unit Testing in React: Full Guide on Jest and Enzyme Testing
Discover how to start and proceed with the testing of React components with Enzyme and Jest if you do not have enough experience...
Read more >
Testing React Native apps with Jest | by Emily Xiong - Nx Blog
Jest encountered an unexpected tokenJest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard ...
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