假设你有一个简单的摩卡测试:
describe("Suite", function(){
it("test",function(doneCallback){
// here be tests
});
});
Run Code Online (Sandbox Code Playgroud)
在此测试中,我可以通过添加函数this.timeout(VALUE);内的任何位置来更改超时describe.
但是,除了该timeout值之外,还有许多其他Mocha选项可以从命令行或生成mocha.opts在test文件夹(./test/mocha.opts)中的文件中独占声明.
我想要的是在运行时更改其中一些选项(例如,reporter),而不是在命令行/ mocha.opts文件中.
根据我对可能性的研究,我发现有一篇文章解释了如何以编程方式使用mocha,这将允许在运行时更改这些选项,但是您需要自己创建Mocha实例,而在普通测试中则不会t可以直接访问Mocha实例.
那么,有没有办法Mocha从现有测试中获取实例并reporter在测试期间更改某些选项,例如在运行时?
我想有一个选项,不需要以Mocha任何方式修改源代码(我想我可以篡改Mocha实例来实现一种直接在Mocha构造函数中获取实例的方法).
这里我附加了我的代码,我传递完成回调并使用supertest请求.因为我在request.end块中的testcase中使用assert/expect,为什么我需要担心超时?我在这里犯的是什么错误.
it('should get battle results ', function(done) {
request(url)
.post('/compare?vf_id='+vf_id)
.set('access_token',access_token)
.send(battleInstance)
.end(function(err, res){ // why need timeout
if (err) return done(err);
console.log(JSON.stringify(res.body));
expect(res.body.status).to.deep.equal('SUCCESS');
done();
});
});
Run Code Online (Sandbox Code Playgroud)
响应后的测试用例结果:错误:超出2000ms的超时.确保在此测试中调用done()回调.
如果我使用mocha命令运行我的测试用例,那么它显示此错误,而如果我正在运行测试, mocha --timeout 15000 则testcase正确传递.但我想避免超时,我该怎么做?
我想用 测试异步代码Mocha。
我按照本教程testing-promises-with-mocha进行操作。最后,它说最好的方法是 async/await。
以下是我的代码,我打算将 setTimeout 设置得比 Mocha 默认值长。
describe('features', () => {
it('assertion success', async() => {
const resolvingPromise = new Promise( resolve => {
setTimeout(() => {
resolve('promise resolved')
}, 3000)
})
const result = await resolvingPromise
expect(result).to.equal('promise resolved')
})
})
Run Code Online (Sandbox Code Playgroud)
摩卡给我错误如下:
Error: Timeout of 2000ms exceeded. For async tests and hooks,
ensure "done()" is called; if returning a Promise, ensure it resolves...
Run Code Online (Sandbox Code Playgroud)
如何解决该错误?简单设置mocha --timeout 10000更长?
谢谢你的时间!
Mocha: 5.2.0
Chai: 4.2.0
Run Code Online (Sandbox Code Playgroud) mocha.js ×3
async-await ×1
automation ×1
javascript ×1
node.js ×1
supertest ×1
unit-testing ×1