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));
}
For example, the async
function aliasIsAdmin()
should throw TypeError: Entity "alias" must be a string but got "undefined" instead.
because the alias
param is undefined
for that specific call.
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)
I can't find examples with async
functions so I guess this is the reason why it doesn't work.
Is there a solution to this?
Try wrapping it in try/catch
since it's throwing 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.');
}
});
(or)
Edit
it('throws TypeError when alias param is not typeof "string"', async () => {
await expect(aliasIsAdmin()).to.be.rejectedWith(TypeError);
});
(finally) For a more complete solution, a message can be tested:
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.');
});
for previous chai versions that don't have .rejectedWith()
. the only async test way would be handled is to wrap it inside a try catch and check expect
the error in the catch.
it('should fail', async () => {
try{
await failingPromise();
}catch(err){
expect(err).to.exist
const { message }=err
expect(message).to.contain("error message")
return
}
//if there is no error from promise throw one here
throw new Error("should throw")
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.