42

I developed a React Native module (wrapping an SDK) and I’m interested in creating some unit tests using mocha. I’m not very familiar with mocha, but I can’t exactly figure out how to proceed.

I have my react native module, call it react-native-mymodule which I can use in an app by doing:

npm install react-native-mymodule

react-native link react-native-mymodule

Then I can import my module with:

import MySDK from "react-native-mymodule”;

I’m trying to do a similar thing with unit tests. In my root directory I have a test/ directory which is where I want to hold all my unit tests.

My simple test file in test/sdk.tests.js

import MySDK from "react-native-mymodule”;
var assert = require('assert');


describe(‘MySDK’, function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});

I’ve tried modifying a tutorial I found online on compiling modules, but haven’t had any luck. This is a file test/setup.js:

import fs from 'fs';
import path from 'path';
import register from 'babel-core/register';

const modulesToCompile = [
  'react-native-mymodule’
].map((moduleName) => new RegExp(`${moduleName}`));


const rcPath = path.join(__dirname, '..', '.babelrc');
const source = fs.readFileSync(rcPath).toString();
const config = JSON.parse(source);
config.ignore = function(filename) {
  if (!(/\/node_modules\//).test(filename)) {
    return false;
  } else {
    return false;
  }
}

register(config);

.babelrc in the root level of my module

{
  "presets": ["flow", "react-native"],
    "plugins": [
      ["module-resolver", {
        "root": [ "./js/" ]
      }]
    ]
}

I have a test/mocha.opts file:

--require babel-core/register
--require test/setup.js

I’m invoking mocha with: ./node_modules/mocha/bin/mocha and I get an error:

Error: Cannot find module 'react-native-mymodule'

Can anyone advise me on the best way to test react native modules?

Clip
  • 3,018
  • 8
  • 42
  • 77
  • Not sure I understand? are you looking to export an sdk so you can test it on a phone or have you built the sdk already? Are you developing with or without expo? – Rachel Gallen May 21 '19 at 22:26
  • @RachelGallen I have a "native module" which wraps native SDK's, for example facebook has this: https://github.com/facebook/react-native-fbsdk. Whats the best way to test something like that? – Clip May 21 '19 at 22:32
  • 2
    is there some reason you [don`t want to create an alias](https://www.npmjs.com/package/mocha-react-native#alias--mock) and mock that module? could this issue be caused from the [transforms](https://gist.github.com/jmreidy/4145809229195441d4d4#file-test_support_compiler-js-L27)? also you may want to [check this issue](https://stackoverflow.com/questions/33793504/using-webpack-aliases-in-mocha-tests). – Fabrizio Bertoglio May 22 '19 at 13:22
  • I would try to isolate the problem and set up a new project as [in this guide](https://formidable.com/blog/2016/02/08/unit-testing-react-native-with-mocha-and-enzyme/), try to get it to work and then import your module. Isolating may help you understand what is causing the issue. We don't know if this is caused from `babel` or from issues with the `node_modules` directories in test environment – Fabrizio Bertoglio May 22 '19 at 13:22
  • @FabrizioBertoglio I do not want to mock the module since that is what I intend to test. I believe the problem is that `NativeModules` are not loaded unless a react native app is actually running. – Clip May 22 '19 at 16:08
  • 4
    please forgive my ignorance, but shouldn't the tests for the module be part of the module source and not be somewhere else? As a user/consumer of modules I rely on my dependencies to be unit tested. In the consuming part, i.e. my app, I mock modules because I know the module works (being unit tested and all) – konqi May 28 '19 at 13:12
  • Are you trying to test `react-native` modules in `reactjs` ? – Rahul Singh Aug 18 '19 at 18:05

1 Answers1

5

If you want to test native modules, I suggest the following:

1. E2E Tests

Node.js standalone cannot interpret native modules. If you want to test native modules in the context of your app, you want to create e2e tests using appium/webdriverio instead of writing unit tests with mocha.

With this, you actually start an emulator with your app installed.

Resources:

2. Unit Tests

If you want to write unit tests for your native module, write them in the Language the Native Module is written in

Resources:


Other than that, you have to mock the modules.
Tom M
  • 2,815
  • 2
  • 20
  • 47
  • also you can use enzyme, Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output. its a good choose for E2E Tests too. https://enzymejs.github.io/enzyme/ – Moein Hosseini May 14 '20 at 06:13
  • 1
    UI testing native modules is possible with Appium and Detox, and even Espresso and XCUITest but what I want to know now is that is it possible to start the app and navigate to the native module with either Appium or Detox and then trigger XCUITests or Espresso? Let's say you are porting over an already existing native app (with its E2E tests) to React Native. – sam k Oct 14 '20 at 17:55
  • @samk it should be possible with Appium – Tom M Oct 26 '20 at 14:26