let assert = require('assert');
describe('Array', () => {
describe('#indexOf', () => {
it('should return -1 when the value is not present', () => {
assert.equal(-1, [1, 2, 3].indexOf(4));
});
});
});
describe('异步代码回调', () => {
it('当参数是整数的时候不返回错误', (done) => {
asyncMehtod(1, (err, result) => {
assert.equal(1, result);
done();
});
});
it('当参数是整数的时候返回错误', (done) => {
asyncMehtod('dsafdsaf', (err, result) => {
assert.notEqual('dsafdfa', result);
assert.equal('不是一个整数', err.message);
done();
});
});
});
function asyncMehtod(id, callback) {
if (!isNaN(parseInt(id))) {
callback(null, id);
} else {
callback(new Error('不是一个整数'));
}
}
describe('promise测试', () => {
it('当参数是整数时, 返回结果等于参数', () => {
return promiseMethod(1).then((result) => {
assert.equal(result, 1);
});
})
it('当参数不是整数时,会抛出异常', () => {
return promiseMethod('范德萨范德萨发').catch((e) => {
assert.equal(e.message, '不是一个整数');
});
})
});
function promiseMethod(id) {
return new Promise((resolve, reject) => {
if (!isNaN(parseInt(id))) {
resolve(id);
} else {
reject(new Error('不是一个整数'));
}
});
}
describe('async/await测试', () => {
it('当参数是整数时,返回结果应该等于参数', async () => {
let res = await asyncAwaitMethod(1);
assert.equal(res, 1);
});
it('当参数不是整数时,会抛出异常', async () => {
try {
let res = await asyncAwaitMethod('dsafdsaf');
} catch (e) {
assert.equal(e.message, '不是一个整数');
}
});
});
async function asyncAwaitMethod(id) {
return promiseMethod(id);
}
describe('箭头函数()=>{}', function () {
it('箭头函数在不需要mocha上下文时不会有任何影响', () => {
assert.equal(1, 1);
});
it('箭头函数在需要mocha上下文时会使得获取mocha上下文失效,如这个测试超时时间为1秒,但是不会生效', async () => {
// 原本含义是定义本次测试超时时间为1秒,但是由于适用了箭头函数,导致this作用于失效
this.timeout(1000);
// 睡眠1.5秒,也不会超时
//await sleep(1500);
assert.ok(true);
});
it('不使用箭头函数,设置超时时间就会起作用', async function() {
// 定义超时1秒
this.timeout(1000);
// 睡眠1.5秒,会超时,放开下面的代码,测试会报超时错误
//await sleep(1500);
assert.ok(true);
});
});
function sleep(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
}
describe('hooks', function () {
before(function () {
console.log('我会在本区块内的所有测试开始前运行');
}) ;
after(function () {
console.log('我会在本区块内的所有测试完成后运行');
});
beforeEach(function () {
console.log('我会在本区块内的每个测试开始前运行');
});
afterEach(function () {
console.log('我会在本区块内的每个测试完成后运行');
});
it('test1', function () {
assert.ok(true);
});
it('test2', function () {
assert.ok(true);
})
});
// describe('等待测试的用例', function () {
// it('这是一个等待测试的用例,没有回调函数');
// })
// 除非你是真的需要它,否则不要提交only()到你的版本控制中
// describe('指定测试用例集合', function () {
// describe.only('这下面的测试用例都会执行', function () {
// it('测试1会执行', function () {
// assert.ok(true);
// });
//
// it('测试2会执行', function () {
// assert.ok(true);
// })
// });
//
// describe('这下面的测试用例不会执行', function () {
// it('测试3不会执行', function () {
// assert.ok(true);
// })
// })
// });
//
// describe('指定测试用例', function () {
// it.only('测试用例1会执行', function () {
// assert.ok(true);
// });
//
// it.only('测试用例2会执行', function () {
// assert.ok(true);
// });
//
// it('测试用例3不会执行', function () {
// assert.ok(true);
// });
// });
//
// // 和only相反,skip用来跳过指定的用例集或者用例
// describe('测试skip', function () {
// describe.skip('这个用例不会执行', function () {
//
// });
//
// describe('这个用例集合会执行', function () {
// it.skip('用例1会跳过,不会执行', function () {
// assert.ok(true);
// });
//
// it('用例1会跳过,不会执行,但是这个用例会执行', function () {
// assert.ok(true);
// })
// })
// })
describe('slow', function () {
this.slow(1000);
it('会显示为slow', async function () {
await sleep(1500);
})
})