本文主要目的在於橫評業界主流的幾款前端框架,順帶說下相關的一些內容。javascript
一般應用會有 單元測試(Unit tests) 和 功能測試(Functional tests),複雜大型應用可能會有整合測試(Integration tests)。
其中:html
單元測試應該:簡單,快速執行,清晰的錯誤報告。
測試框架基本上都作了同一件事兒:前端
組合使用工具很常見,即便已選框架也能實現相似的功能java
總結一下,Mocha 用的人最多,社區最成熟,靈活,可配置性強易拓展,Jest 開箱即用,裏邊啥都有提供全面的方案,Tape 最精簡,提供最基礎的東西最底層的API。node
參考react
選擇測試框架並非非黑即白的事兒,就像你並不能證實PHP不是最好的語言。
我的傾向 Jest,緣由:容易上手,開箱即用,功能全面。git
下面是在 stackshare 最流行的三個測試框架以下,但應考慮到 Jest 比較年輕,參與投票的時間較短的因素。
下面是三個框架在過去一年裏 google 的搜索熱度,但應該考慮到 Jest 比較年輕,你們嘗試新東西,解決新問題,可能會帶來較大搜索量。
下面是用戶使用狀況的調查,能夠看出, Jest 忠誠度較高,使用後棄用的機率較低,Mocha 和 Jasmine 知名度最高。數據統計於 2017 年。
參考github
要測試的代碼瀏覽器
'use strict' var Math = { add(a, b) { return a + b; } } module.exports = Math;
const test = require('ava'); const math = require('../Math'); const firstOperand = 2; const secondOperand = 3; test("Math add function", t => { const result = math.add(firstOperand, secondOperand); t.is(result, firstOperand + secondOperand); });
var math = require('../Math'); describe("Math", function() { var firstOperand; var secondOperand; beforeEach(function() { firstOperand = 2; secondOperand = 3; }); it("should add two numbers", function() { var result = math.add(firstOperand, secondOperand); expect(result).toEqual(firstOperand + secondOperand); }); });
jest.unmock('../Math'); // unmock to use the actual implementation of Math var math = require('../Math'); describe("Math", function() { var firstOperand; var secondOperand; beforeEach(function() { firstOperand = 2; secondOperand = 3; }); it("should add two numbers", function() { var result = math.add(firstOperand, secondOperand); expect(result).toEqual(firstOperand + secondOperand); }); });
var assert = require('assert'); // nodejs 內建斷言 var math = require('../Math'); describe("Math", function() { var firstOperand; var secondOperand; beforeEach(function() { firstOperand = 2; secondOperand = 3; }); it("should add two numbers", function() { var result = math.add(firstOperand, secondOperand); assert.equal(result, firstOperand + secondOperand); }); });
var test = require('tape'); var math = require('../Math'); var firstOperand = 2; var secondOperand = 3; test("Math add function", function(t) { var result = math.add(firstOperand, secondOperand); t.equal(result, firstOperand + secondOperand); t.end(); });