我有一些简单的基于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
,但我可以确认使用适当的参数调用代码等。
答案 0 :(得分:0)
看起来next
延续作为then
方法的错误处理程序(第二个arg)传递。
我想你想要:
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) });
}).then(next);