本文主要目的在于横评业界主流的几款前端框架,顺带说下相关的一些内容。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(); });