我正在尝试使用Jest测试异步功能。到目前为止,我似乎通过了三个测试。
但是,测试完成运行后,我得到一个错误UnhandledPromiseRejectionWarning: Error: expect(received).toThrow(expected)...
(请参阅下面的完整日志)。我已经尝试过在此处以及在GitHub上这样的其他地方发布的多种解决方案,但是我无法摆脱错误。
我有以下异步功能:
async function aliasIsAdmin(alias, roles = DEFAULT_ROLES) {
try {
const url = "https://xxxxxx.execute-api.us-west-2.amazonaws.com/local/xxxxx/";
if (typeof alias !== "string") {
throw new Error('Entity "alias" must be a string.');
}
if(!Array.isArray(roles)){
throw new Error('Entity "roles" must be an array but got "' + typeof roles + '". Permission Denied.');
}
else if(roles.some((role) => typeof role !== "string")){
throw 'Entity "roles" must be an array of strings but got at least one element of type: "' + typeof alias + '". Permission Denied.';
}
let { data } = (await axios.get(url + alias)); //Fetch roles that the alias holds.
return data.some((role) => roles.includes(role)); //If alias has any of the roles in the policy, grant access.
} catch (error) {
console.error("Error when attempting to authorize user @" + alias
+ ". Access for this user has been denied, please try again.\nError:\n" + error);
return false;
}
}
我编写了以下测试:
const { aliasIsAdmin, getRolesByAlias } = require("../authorization");
describe('aliasIsAdmin', () => {
it('should return true', () => {
expect.assertions(1);
return aliasIsAdmin("anAdmin")
.then((r => {expect(r).toBe(true)}));
});
it('should return false', () => {
expect.assertions(1);
return aliasIsAdmin("notAnAdmin")
.then((r => {expect(r).toBe(false)}));
});
it('should throw', () => {
aliasIsAdmin(1)
.then((r => {expect(r).toThrow('Entity "alias" must be a string.')}));
});
});
当我运行测试时,我得到:
(node:94497) UnhandledPromiseRejectionWarning: Error: expect(received).toThrow(expected)
Matcher error: received value must be a function
Received has type: boolean
Received has value: false
(node:94497) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:94497) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PASS public/utils/test/authorization.test.js
aliasIsAdmin
✓ should return true (895 ms)
✓ should return false (238 ms)
✓ should throw (37 ms)
console.error
Error when attempting to authorize user @1. Access for this user has been denied, please try again.
Error:
Error: Entity "alias" must be a string.
41 |
42 | } catch (error) {
> 43 | console.error("Error when attempting to authorize user @" + alias
| ^
44 | + ". Access for this user has been denied, please try again.\nError:\n" + error);
45 | return false;
46 | }
at aliasIsAdmin (public/utils/authorization.js:43:13)
at Object.<anonymous> (public/utils/test/authorization.test.js:17:9)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
我的测试命令是: jest --coverage public/utils/test
如错误消息所述,toThrow
期望函数作为其接收值,在您的情况下,这是一个Promise解析的布尔值,因为您在aliasIsAdmin
catch块中捕获了所有错误并返回false
。
如果更改catch块中的逻辑并重新引发错误,则可以在单元测试中使用以下内容:
it('should throw', async () => {
await expect(aliasIsAdmin(1)).rejects.toThrow('Entity "alias" must be a string.');
});
如果当前逻辑是正确的,并且您实际上并不希望引发错误,则需要更改单元测试以检查已解析的false
布尔值:
it('should return false if non-string parameter is passed', async () => {
const result = await aliasIsAdmin(1);
expect(result).toBe(false);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句