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

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

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

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

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.