Mocha 是一个功能强大的 JavaScript 测试框架,可以运行在和浏览器环境中
安装 Mocha:
1 | $ npm install --save-dev mocha |
使用 node 自带的断言模块 assert1
2
3
4
5
6
7const assert = require('assert');
describe('加法运算测试', function() {
it('1 + 1 等于 2', function() {
assert.equal(1 + 1, 2);
});
});
Chai:
使用功能更强大 Chai 断言库,支持 expect、assert 和 should 三种断言风格
安装 Chai:
1 | $ npm install --save-dev chai |
1 | const expect = require('chai').expect; |
异步测试:
调用 done 回调函数1
2
3
4
5
6
7
8
9
10const expect = require('chai').expect;
describe('异步测试', function() {
it('测试 setTimeout', function(done) {
setTimeout(() => {
expect(1 + 1).to.be.equal(2);
done();
}, 1000);
});
});
钩子:
1 | const expect = require('chai').expect; |