Jest Installation
Install the jest command line by using the command NPM installation jest-cli--save-dev:
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 (WORDW rap@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, escodegen@1.7.0, htmlparser2@3.8.3, request@2.65.0)
Unit Test Folder
Jest default execution of the single test case under the __tests__ (left and right two underscore) file, so create the directory under your root directory:
Writing case
The target file is sum.js:
Sum.js
function sum (value1, value2) {
return value1 + value2;
}
Module.exports = sum;
Write the Unit test class Sum-test.js under the __tests__ directory:
__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);
});
perform a single test
Command for 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)