简体   繁体   English

如何测试 React Native 模块?

[英]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.我开发了一个 React Native 模块(包装了一个 SDK)并且我对使用 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:我有我的 react native 模块,称之为react-native-mymodule ,我可以通过执行以下操作在应用程序中使用它:

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.在我的根目录中,我有一个test/目录,这是我想要保存所有单元测试的地方。

My simple test file in test/sdk.tests.js我在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 :这是一个文件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 .babelrc在我的模块的根级别

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

I have a test/mocha.opts file:我有一个test/mocha.opts文件:

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

I'm invoking mocha with: ./node_modules/mocha/bin/mocha and I get an error:我正在使用以下./node_modules/mocha/bin/mocha调用 mocha: ./node_modules/mocha/bin/mocha mocha/ ./node_modules/mocha/bin/mocha并且出现错误:

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

Can anyone advise me on the best way to test react native modules?任何人都可以就测试本机模块的最佳方法向我提出建议吗?

If you want to test native modules, I suggest the following:如果你想测试原生模块,我建议如下:

1. E2E Tests 1. E2E 测试

Node.js standalone cannot interpret native modules. Node.js Standalone 无法解释本机模块。 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.如果您想在您的应用程序上下文中测试本机模块,您需要使用 appium/webdriverio 创建 e2e 测试,而不是使用 mocha 编写单元测试。

With this, you actually start an emulator with your app installed.有了这个,您实际上启动了一个安装了应用程序的模拟器。

Resources:资源:

2. Unit Tests 2. 单元测试

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. 除此之外,您必须模拟模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM