Mocha & Chai - 测试异步函数在传递无效参数时是否抛出 TypeError?
Mocha & Chai - Test if async function throws TypeError when passed invalid param?
我有这个功能:
const DEFAULT_ROLES = [
'Director',
'Admin',
]; // Default Policy
async function aliasIsAdmin(alias, roles = DEFAULT_ROLES) {
const url = 'https://foobarapi.execute-api.us-west-2.amazonaws.com/foo/bar/';
//How to test for this throw, for example?
if (typeof alias !== 'string') {
throw new TypeError(`Entity "alias" must be a string but got "${typeof alias}" instead.`);
}
if (!Array.isArray(roles)) {
throw new TypeError(`Entity "roles" must be an array but got "${typeof roles}" instead.`);
} else if (roles.some((role) => typeof role !== 'string')) {
throw new TypeError(`Entity "roles" must be an array of strings but got at least one element of type: "${typeof roles}" instead.`);
}
const { data } = (await axios.get(url + alias)); // Fetch roles that the alias holds.
return data.some((role) => roles.includes(role));
}
我如何使用 Mocha 和 Chai 来测试函数在传递无效参数时是否抛出异常?
例如,async
函数 aliasIsAdmin()
应该抛出 TypeError: Entity "alias" must be a string but got "undefined" instead.
因为 alias
参数是 undefined
该特定调用。
如果我这样做:
it('throws TypeError when alias param is not typeof "string"', async () => {
assert.throws(async function () { await aliasIsAdmin() }, TypeError, /must be string/);
});
我得到:
aliasIsAdmin
1) throws TypeError when alias param is not typeof "string"
(node:77600) UnhandledPromiseRejectionWarning: TypeError: Entity "alias" must be a string
but got "undefined" instead.
[... stacktrace continues ...]
1) aliasIsAdmin
throws TypeError when alias param is not typeof "string":
AssertionError: expected [Function] to throw TypeError
at Context.<anonymous> (test/permissions-manager-client.test.js:25:16)
at processImmediate (internal/timers.js:439:21)
at process.topLevelDomainCallback (domain.js:130:23)
我找不到带有 async
函数的示例,所以我想这就是它不起作用的原因。
有解决办法吗?
尝试将其包装在 try/catch
中,因为它正在抛出 Error
it('throws TypeError when alias param is not typeof "string"', async () => {
try {
await aliasIsAdmin();
} catch(error) {
// Do your assertion here
expect(error).to.be.an('error');
expect(error.name).to.be.equal('TypeError');
expect(error.message).to.be.equal('Entity "alias" must be a string but got "undefined" instead.');
}
});
(或)
编辑
it('throws TypeError when alias param is not typeof "string"', async () => {
await expect(aliasIsAdmin()).to.be.rejectedWith(TypeError);
});
(终于)
更完整的解决方案,可以测试留言:
it('throws TypeError when alias param is not typeof "string"', async () => {
await expect(aliasIsAdmin()).to.be.rejectedWith(TypeError, 'Entity "alias" must be a string but got "undefined" instead.');
});
我有这个功能:
const DEFAULT_ROLES = [
'Director',
'Admin',
]; // Default Policy
async function aliasIsAdmin(alias, roles = DEFAULT_ROLES) {
const url = 'https://foobarapi.execute-api.us-west-2.amazonaws.com/foo/bar/';
//How to test for this throw, for example?
if (typeof alias !== 'string') {
throw new TypeError(`Entity "alias" must be a string but got "${typeof alias}" instead.`);
}
if (!Array.isArray(roles)) {
throw new TypeError(`Entity "roles" must be an array but got "${typeof roles}" instead.`);
} else if (roles.some((role) => typeof role !== 'string')) {
throw new TypeError(`Entity "roles" must be an array of strings but got at least one element of type: "${typeof roles}" instead.`);
}
const { data } = (await axios.get(url + alias)); // Fetch roles that the alias holds.
return data.some((role) => roles.includes(role));
}
我如何使用 Mocha 和 Chai 来测试函数在传递无效参数时是否抛出异常?
例如,async
函数 aliasIsAdmin()
应该抛出 TypeError: Entity "alias" must be a string but got "undefined" instead.
因为 alias
参数是 undefined
该特定调用。
如果我这样做:
it('throws TypeError when alias param is not typeof "string"', async () => {
assert.throws(async function () { await aliasIsAdmin() }, TypeError, /must be string/);
});
我得到:
aliasIsAdmin
1) throws TypeError when alias param is not typeof "string"
(node:77600) UnhandledPromiseRejectionWarning: TypeError: Entity "alias" must be a string
but got "undefined" instead.
[... stacktrace continues ...]
1) aliasIsAdmin
throws TypeError when alias param is not typeof "string":
AssertionError: expected [Function] to throw TypeError
at Context.<anonymous> (test/permissions-manager-client.test.js:25:16)
at processImmediate (internal/timers.js:439:21)
at process.topLevelDomainCallback (domain.js:130:23)
我找不到带有 async
函数的示例,所以我想这就是它不起作用的原因。
有解决办法吗?
尝试将其包装在 try/catch
中,因为它正在抛出 Error
it('throws TypeError when alias param is not typeof "string"', async () => {
try {
await aliasIsAdmin();
} catch(error) {
// Do your assertion here
expect(error).to.be.an('error');
expect(error.name).to.be.equal('TypeError');
expect(error.message).to.be.equal('Entity "alias" must be a string but got "undefined" instead.');
}
});
(或)
编辑
it('throws TypeError when alias param is not typeof "string"', async () => {
await expect(aliasIsAdmin()).to.be.rejectedWith(TypeError);
});
(终于) 更完整的解决方案,可以测试留言:
it('throws TypeError when alias param is not typeof "string"', async () => {
await expect(aliasIsAdmin()).to.be.rejectedWith(TypeError, 'Entity "alias" must be a string but got "undefined" instead.');
});