React-native的单元测试框架-Jest

首先一个简单例子,自己先试了一下~

安装

使用命令npm install jest-cli --save-dev来安装Jest命令行:

localhost:TesterHome wuxian$ npm install jest-cli --save-dev
[email protected] node_modules/jest-cli
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected],

单元测试文件夹

Jest默认执行__tests__(左右各两个下划线)文件下的单测case,所以在你的根目录下创建该目录:

React-native的单元测试框架-Jest_第1张图片


编写case

目标文件为sum.js:

// sum.js
function sum(value1, value2) {
  return value1 + value2;
}
module.exports = sum;

__tests__目录下编写单元测试类sum-test.js:

// __tests__/sum-test.js
jest.dontMock('../sum');

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

执行单测

命令为npm test:

localhost:TesterHome wuxian$ npm test

> [email protected] test /Users/wuxian/Documents/sourcecode/self/react-native/TesterHome
> jest

Using Jest CLI v0.7.1
 PASS  __tests__/sum-test.js (0.018s)
1 test passed (1 total in 1 test suite, run time 0.555s)



接下来进行接口测试:

import 'react-native';
import React from 'react';
import Index from '../app/service/userService.js';

// // Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

describe('signIn',function() {
  it('登录的测试',function(){
    var Request = require('request');
    var myInit = {
                  method:'POST',
                  headers:{
                          'Accept':'application/json',
                          'Content-Type':'application/json',
                  },
                  body:JSON.stringify({
                      mobile: 157××××2323,
                      codE: 1234,
                      password: 11242343
                  }) };

    var myRequest = new Request('http://192.168.1.110:3000/user/generate', myInit);
 
  });
});

终端npm test

测试结果会将body数据发送至服务器,Request详细自行查阅Jest、Fetch文档吧~

你可能感兴趣的:(React,Native)