Strange behavior of the assertRejects() method.
Behavior of the
assertRejects()
method.
assertRejects()
fails when a non-stubbed method is called before the stubbed
method.
Let's look at the following example:
execute: deno test
output:
Note that bar()
is stubbed to reject the promise with an error. However,
bar()
only rejects with an error if foo()
is not called:
execute: deno test
output:
What am I doing wrong? Is this a bug in the stub()
method, or what am I not
understanding?2 Replies
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code you've provided, there is one thing that stands out which might be causing the issue.
In your test code, you are not awaiting the
assertRejects()
call. Since assertRejects()
returns a Promise, you should await it to ensure that the test runner waits for the assertion to complete before moving on. Here's the corrected part of your test code:
Please note that the advice provided here is not based on the Deno documentation, and you should double-check the accuracy of the advice given. Additionally, ensure that your test framework supports asynchronous tests and that you are using the correct syntax for defining test cases.Thank you, AI. Yes, that is exactly the problem. You have been extremely helpful now. Congratulations.