前端單元測試

chai
istanbul //統計覆蓋率
mocha
superagent
supertest

// 單元測試文件 test/test.js

const chai = require('chai');
const expect = chai.expect;
// const mocha = require('mocha');
const getNum = require('../index')// 【待測的功能函數】

describe('Test', function() {
  it('should return 20 when the value is 10', function() {
      expect(getNum(10)).to.equal(20)
  })
  it('should return empty when the value is empty', function() {
    expect(getNum()).to.equal('')
  })
  it('should return string when the value is string', function() {
    expect(getNum('sdsd')).to.equal('sdsd')
  })
})
// 項目業務文件(待測的函數)

const getNum = (value) => {
  if(typeof value=='string') {
    return value;
  } else {
    if(!value) {
      return '';
    } else {
      return value * 2;
    }
  }
}
module.exports = getNum;

// package.json
"scripts": {
    "test": "istanbul cover ./node_modules/mocha/bin/_mocha",
    "test-cov": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --timeout=500000 --recursive test/test.js --bail"
  },

運行npm run test-cov
結果如下圖👇
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章