React-native单元测试框架-Jest

前端之家收集整理的这篇文章主要介绍了React-native单元测试框架-Jest前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Jest

安装

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

localhost:TesterHome wuxian$ npm install jest-cli --save-dev
jest-cli@0.7.1 node_modules/jest-cli
├── jasmine-pit@2.0.2
├── object-assign@4.0.1
├── graceful-fs@4.1.2
├── through@2.3.8
├── resolve@1.1.6
├── diff@2.2.0
├── optimist@0.6.1 (wordwrap@0.0.3,minimist@0.0.10)
├── json-stable-stringify@1.0.0 (jsonify@0.0.0)
├── mkdirp@0.5.1 (minimist@0.0.8)
├── which@1.2.0 (is-absolute@0.1.7)
├── worker-farm@1.3.1 (xtend@4.0.1,errno@0.1.4)
├── node-haste@1.2.8 (esprima-fb@4001.1001.0-dev-harmony-fb)
├── lodash.template@3.6.2 (lodash._basetostring@3.0.1,lodash._reinterpolate@3.0.0,lodash._basecopy@3.0.1,lodash._basevalues@3.0.0,lodash.restparam@3.6.1,lodash.templatesettings@3.1.0,lodash.escape@3.0.0,lodash._isiterateecall@3.0.9,lodash.keys@3.1.2)
├── sane@1.3.0 (watch@0.10.0,minimist@1.2.0,exec-sh@0.2.0,walker@1.0.7,minimatch@0.2.14,fb-watchman@1.6.0)
├── cover@0.2.9 (which@1.0.9,underscore@1.2.4,underscore.string@2.0.0,cli-table@0.0.2)
├── istanbul@0.3.22 (abbrev@1.0.7,wordwrap@1.0.0,nopt@3.0.4,async@1.5.0,once@1.3.2,esprima@2.5.0,escodegen@1.7.0,supports-color@3.1.2,fileset@0.2.1,js-yaml@3.4.3,handlebars@4.0.4)
└── jsdom@7.0.2 (acorn-globals@1.0.9,webidl-conversions@2.0.0,xml-name-validator@2.0.1,browser-request@0.3.3,abab@1.0.0,cssom@0.3.0,symbol-tree@3.1.3,nwmatcher@1.3.6,tough-cookie@2.2.0,parse5@1.5.0,acorn@2.5.2,cssstyle@0.2.30,whatwg-url-compat@0.6.5,htmlparser2@3.8.3,request@2.65.0)

单元测试文件

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

编写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

> TesterHome@0.0.1 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)
原文链接:https://www.f2er.com/react/307552.html

猜你在找的React相关文章