Dan*_*ohn 644
你去:http://mochajs.org/#test-level
it('accesses the network', function(done){
this.timeout(500);
[Put network code here, with done() in the callback]
})
Run Code Online (Sandbox Code Playgroud)
对于箭头功能使用如下:
it('accesses the network', (done) => {
[Put network code here, with done() in the callback]
}).timeout(500);
Run Code Online (Sandbox Code Playgroud)
小智 131
如果您想使用es6箭头功能,可以.timeout(ms)在it定义的末尾添加:
it('should not timeout', (done) => {
doLongThing().then(() => {
done();
});
}).timeout(5000);
Run Code Online (Sandbox Code Playgroud)
至少这适用于Typescript.
chr*_*lly 70
(自从我今天遇到这个)
使用ES2015胖箭头语法时要小心:
这将失败:
it('accesses the network', done => {
this.timeout(500); // will not work
// *this* binding refers to parent function scope in fat arrow functions!
// i.e. the *this* object of the describe function
done();
});
Run Code Online (Sandbox Code Playgroud)
编辑:为什么失败:
作为@atoth提到的意见,胖箭头函数没有自己的这一具有约束力.因此,它不可能为它的功能结合到这种回调并提供超时功能.
结论:不要对需要增加超时的函数使用箭头函数.
Sum*_*lly 41
如果您在NodeJS中使用,则可以在package.json中设置超时
"test": "mocha --timeout 10000"
Run Code Online (Sandbox Code Playgroud)
然后你可以使用npm运行:
npm test
Run Code Online (Sandbox Code Playgroud)
and*_*rey 20
从命令行:
mocha -t 100000 test.js
Run Code Online (Sandbox Code Playgroud)
对于测试分区Express:
const request = require('supertest');
const server = require('../bin/www');
describe('navegation', () => {
it('login page', function(done) {
this.timeout(4000);
const timeOut = setTimeout(done, 3500);
request(server)
.get('/login')
.expect(200)
.then(res => {
res.text.should.include('Login');
clearTimeout(timeOut);
done();
})
.catch(err => {
console.log(this.test.fullTitle(), err);
clearTimeout(timeOut);
done(err);
});
});
});
Run Code Online (Sandbox Code Playgroud)
在该示例中,测试时间为4000(4s).
注意:在测试时间内调用setTimeout(done, 3500)是次要的,done但是clearTimeout(timeOut)避免使用所有这些时间.
小智 8
这对我有用!找不到任何东西使它与 before() 一起工作
describe("When in a long running test", () => {
it("Should not time out with 2000ms", async () => {
let service = new SomeService();
let result = await service.callToLongRunningProcess();
expect(result).to.be.true;
}).timeout(10000); // Custom Timeout
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144042 次 |
| 最近记录: |