Promise 的 Mocha 单元测试抛出错误
Mocha unit test for Promise throws error
我正在尝试对 Promise 进行单元测试。这是代码:
it('it should return some 10 user data with ok status code when called with url ', (done) => {
return user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10)
})
.catch((err)=>{
console.log('me in error')
assert.fail('err')
})
})
以上代码在 运行 时抛出以下错误:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\ajay\jay-workspace\UniTestModule\test\user.test.js)
您应该在使用异步测试时调用 done() 回调(有关详细信息,请查看 https://mochajs.org/#asynchronous-code)。
it('it should return some 10 user data with ok status code when called with url ', (done) => {
user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10);
done();
})
.catch((err)=>{
console.log('me in error')
assert.fail('err');
done(err);
})
done
未被调用,这导致测试超时。
Mocha 原生支持 promises。 done
不应该在有承诺时使用;相反,应返回承诺:
it('it should return some 10 user data with ok status code when called with url ', () => {
return user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10)
});
})
拒绝的承诺将无法通过测试,也不需要assert.fail
。
你应该使用 --timeout
hook 并像 ./node_module/.bin/mocha test/ --timeout=5000
一样增加毫秒数
或者您可以在测试用例正文中添加 this.timeout(5000)
it('it should return some 10 user data with ok status code when called with url ', (done) => {
user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10);
this.timeout(5000);
setTimeout(done, 3000);
})
我正在尝试对 Promise 进行单元测试。这是代码:
it('it should return some 10 user data with ok status code when called with url ', (done) => {
return user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10)
})
.catch((err)=>{
console.log('me in error')
assert.fail('err')
})
})
以上代码在 运行 时抛出以下错误:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\ajay\jay-workspace\UniTestModule\test\user.test.js)
您应该在使用异步测试时调用 done() 回调(有关详细信息,请查看 https://mochajs.org/#asynchronous-code)。
it('it should return some 10 user data with ok status code when called with url ', (done) => {
user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10);
done();
})
.catch((err)=>{
console.log('me in error')
assert.fail('err');
done(err);
})
done
未被调用,这导致测试超时。
Mocha 原生支持 promises。 done
不应该在有承诺时使用;相反,应返回承诺:
it('it should return some 10 user data with ok status code when called with url ', () => {
return user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10)
});
})
拒绝的承诺将无法通过测试,也不需要assert.fail
。
你应该使用 --timeout
hook 并像 ./node_module/.bin/mocha test/ --timeout=5000
一样增加毫秒数
或者您可以在测试用例正文中添加 this.timeout(5000)
it('it should return some 10 user data with ok status code when called with url ', (done) => {
user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10);
this.timeout(5000);
setTimeout(done, 3000);
})