1
我有一些简单的基于Promise的代码,尽管我尝试了最好的时间(> 2000ms)。帮帮我?节点/摩卡测试超时
export function listCurrentUserPermissions(req, res, next) {
return UserPermission.findAll({
where: { accountId: req.user.tenant() }
}).catch((error) => {
console.log('-----------------------------------');
console.log(error);
console.log.bind(console);
console.log('-----------------------------------');
}).then((permissions) => {
return res.json({ userPermission: permissions.map(serializeUserPermission) });
}, next);
而且测试:
describe('GET /api/v0/permissions',() => {
it('shows the current users permissions',() => {
return api.listCurrentUserPermissions(req, res, next).then(() => {
expect(UserPermission.findAll).to.have.been.calledWithMatch({
where: { accountId: req.user.tenant() }
});
expect(next).to.have.beenCalled;
expect(next.lastCall.args[0].output.payload.statusCode).to.equal(200);
expect(next.lastCall.args[0].output.payload.permission).to.include(nonAdminPermission.permission);
});
});
})
的错误,我得到:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
console.log
不会被调用,但我可以证实,该代码被称为与适当的参数,可以等。
不幸的是这种重构:( –
哦发生同样的错误,woops不能通过'next'作为成功回调,因为它的节点式的(错误结果) (()=> {next()})' – jasonkarns
虽然你通常不需要调用'next',因为这条路线是最后一行(它发送一个响应),不需要调用后续的中间件,所以我通常在promise上调用'.done'。 – jasonkarns