Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

javascript - How to test React Native Module?

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?

question from:https://stackoverflow.com/questions/56158398/how-to-test-react-native-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...