mocha 是一款测试框架,支持node端与浏览器端的测试框架。
install
// global
npm install -g mocha
// project
npm install --save-dev mocha
安装 mocha v3.0 或者以上版本,需要以下环境:
node.js 4
npm 2.1.4
执行
in Bash
mocha [debug] [options] [files...]
默认执行 test
文件夹下的测试用例,但不会遍历执行,需添加--recursive
(英文意为递归)参数。
in package.json
最方便的还是直接加入 NPM script 执行。
{
"scripts": {
"test": "mocha"
}
}
npm run test
断言库
mocha
允许使用其他的断言库,其支持的断言库如下:
- should.js
- expect.js
- chai
- better-assert
- unexpected
options
--recursive
递归模式
-R
,--repoert<name>
输出格式
– spec
– dot
– nyan
彩虹喵
– tap
– landing
– progress
– JSON
– min
– doc
– markdown
– html
默认为spec
-w
, -- watch
观察模式。
-t
, --time<ms>
测试延时,使用times()
,默认为毫秒,2000ms
也可指定为2s
--compilers
在 Mocha v4.0.0 中被废弃,请使用
-r
具体参考:https://github.com/mochajs/mocha/wiki/compilers-deprecation
-r
, --require <module-name>
ES5 的支持,添加 babel 的编译。
--require babel-register
-u
,--ui
指定界面,默认为"bdd"
mocha.opts
mocha.opts
是 Mocha
的配置文件。
hooks
before([description], [cb])
after([description], [cb])
beforeEach([description], [cb])
afterEach([description], [cb])
全局注册
在describe
之外注册beforeEach,即为全局注册。
BDD
describe()
context()
it()
specify()
before()
after()
beforeEach()
afterEach()
TDD
suite()
test()
suiteSetup()
suiteTeardown()
setup()
teardown()
useage
describe(descprion, cb)
describe
块称为"测试套件"(test suite),表示一组相关的测试。
describe 可以嵌套使用
it()
it
块称为"测试用例"(test case),表示一个单独的测试,是测试的最小单位。
assert()
异步代码测试
需要在 it()
中添加一个回调函数:
describe('asynchronopus code test', function () {
decribe('test ajax)', function () {
it('should be throw a error', function(done) {
fetch('unexit.url')
.then(function() {
// some code
done();
})
.catch(function (error) {
done(error);
});
});
});
});
该代码可能存在问题
In Mocha v3.0.0 and newer, returning a Promise and calling done() will result in an exception, as this is generally a mistake:
promise
在 promise
中,直接使用返回的 promise:
describe('asynchronopus code test', function () {
decribe('test ajax)', function () {
it('should be throw a error', function() {
return fetch('unexit.url').should.eventually.have.length(3)
});
});
});
async/await
async/await 实际上返回的还是一个 promise
,所以区别不大。
describe('asynchronopus code test', function () {
decribe('test ajax)', function () {
it('should be throw a error', async function() {
const res = await fetch('unexit.url');
res.should.eventually.have.length(3);
});
});
});
箭头函数
mocha 的作用域内不建议使用箭头函数,主要原因还是 this 指向的问题。
padding test
不给测试用例传递回调函数,即为padding test(等待被实现的测试用例)
describe('padding test', function () {
it('this is a padding test!');
});
only
skip
Test coverage
istanbul
浏览器端测试
生成测试目录
mocha init <path>
则会在目标目录生成测试目录,编辑index.html
<!DOCTYPE html>
<html>
<head>
<title>Mocha</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="../add.js"></script>
<script src="mocha.js"></script>
<!-- 引入断言库 -->
<script src="http://chaijs.com/chai.js"></script>
<script>mocha.setup('bdd');</script>
<!-- 引入需测试文件 -->
<script src="tests.spec.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
编辑tests.spec.js
var expect = chai.expect;
decsribe('test add fucntion', function () {
it('one add one should be equal two', function() {
expect(add(1, 1)).to.be.equal(2);
});
});