admin管理员组文章数量:1317894
I'm trying to catch an error which should be throw by the function getUserRemendations
. This is my example:
it('should throw an error when no user ID is provided', (done) => {
expect(async () => {
await Client.getUserRemendations(null, {})
}).to.throw(/Missing/)
})
Unfortunately it doesn't work and I get as result that my test it doesn't pass along with this message:
AssertionError: expected [Function] to throw an error
I'm trying to catch an error which should be throw by the function getUserRemendations
. This is my example:
it('should throw an error when no user ID is provided', (done) => {
expect(async () => {
await Client.getUserRemendations(null, {})
}).to.throw(/Missing/)
})
Unfortunately it doesn't work and I get as result that my test it doesn't pass along with this message:
AssertionError: expected [Function] to throw an error
Share
Improve this question
edited Sep 9, 2016 at 12:45
Bergi
666k161 gold badges1k silver badges1.5k bronze badges
asked Sep 9, 2016 at 12:24
MazzyMazzy
14.2k47 gold badges133 silver badges217 bronze badges
2
- 1 Please don't edit solutions into the question, rather post an answer. – Bergi Commented Sep 9, 2016 at 12:46
-
Notice that the
async
/await
here is totally useless.expect(() => SClient.getUserRemendations(null, {}))
uses a function that returns a promise just as well. – Bergi Commented Sep 9, 2016 at 12:47
1 Answer
Reset to default 9The way you have set up the the test won't work because expect.to.throw
is not expecting a promise. At least I think that is what is going on based on this issue.
The best alternative is to use chai-as-promised
and do something like:
it('should throw an error when no user ID is provided', () => {
expect(Client.getUserRemendations(null, {})).be.rejectedWith(/Missing/);
});
本文标签: javascriptAsyncAwait throw an error in MochaStack Overflow
版权声明:本文标题:javascript - AsyncAwait throw an error in Mocha - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742037588a2417386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论