React-native單元測試架構-Jest

來源:互聯網
上載者:User

Jest 安裝

使用命令npm install jest-cli --save-dev來安裝Jest命令列:

localhost:TesterHome wuxian$ npm install jest-cli --save-devjest-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, escodegen@1.7.0, htmlparser2@3.8.3, request@2.65.0)
單元測試檔案夾

Jest預設執行__tests__(左右各兩個底線)檔案下的單測case,所以在你的根目錄下建立該目錄:

編寫case

目標檔案為sum.js:

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

在__tests__目錄下編寫單元測試類sum-test.js:

// __tests__/sum-test.jsjest.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> jestUsing 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)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.