嗨,我有以下函数返回promise
module.exports.getJwtToken = async() => {
const httpSearchAddressUXConfig = {
headers: {
Accept: 'application/json',
mock: false,
'Content-Type': 'application/json',
},
data: reqBody,
method: 'POST',
url: `${config.app.entTokens.host}`, // need to get from env variables
timeout: config.app.enterpriseHTTPTimeout
};
try {
const token = await axios(httpSearchAddressUXConfig);
return token.data;
} catch (err) {
throw err;
}
我有以下测试用例,由于未处理的Promise拒绝错误而失败
it('should find Jwt token ', async(done) => {
const actualTokenfound = jwtTokenService.getJwtToken();
return actualTokenfound
.then(result => expect(result).toBe(Object))
.then(done);
});
有什么建议吗?
答案 0 :(得分:1)
如果定义异步函数,则无需使用“完成”。我想类似的东西会起作用。
it('should find Jwt token ', async () => {
const actualTokenfound = await jwtTokenService.getJwtToken();
expect(result).toBe(Object));
});