(一)Chai($npm install chai)https://www.chaijs.com/ 安裝到devDependencies中,線上不用,開發用node
Chai is a BDD(行爲驅動開發)/TDD(測試驅動開發)assertion library for node & browser.npm
const { add } = require( '../src/math' );//本身寫的加法函數
const{ should, expect, assert } = require( 'chai' );
①should(); add(2,3).should.equal(5); ②expect(add(2,3)).to.be(5);
assert.equal(add(2,3),5);//原生node也有,但這個方法更多
(二)Mocha($npm install mocha)https://mochajs.org/ json
Mocha is a feature-rich JS test framework running on Node.js &in the browser.函數
注入:package.json中加入一條"script":{"test":"mocha"}以後,直接npm test便可以啓動測試測試
const { add,mul } = require( '../src/math' );//本身寫的加法乘法函數
const assert = require( 'assert' );//本身自己不包含任何斷言庫,或者能夠require( 'chai' )
describe('#math',()=>{ describe('add',()=>{ it('should return 5 when 2+3',()=>{ expect(add(2,3),5); });//2+3應該等於5
it('should return 5 when 2+3',()=>{ expect(add(2,-3),-1); }); }); describe('mul',()=>{ it('should return 6 when 2*3',()=>{ expect(mul(2,3),6); });//2*3應該等於6
}); }); //it.only(只執行一個),it.skip(跳過這個不執行)