assertRejects does not fail when an error is thrown, but a promise was not rejected
See original GitHub issueDescribe the bug
import { assertRejects } from "https://raw.githubusercontent.com/denoland/deno_std/main/testing/asserts.ts";
await assertRejects(function () { throw new Error("test") });
Expected behavior Should throw because no promise was rejected.
Node’s assertRejects
behaviour:
> assert.rejects(function() { throw new Error("test"); }).catch(err => console.log(err));
> Error: test
at REPL16:1:35
at waitForActual (assert.js:786:21)
at Function.rejects (assert.js:921:31)
at REPL16:1:8
at Script.runInThisContext (vm.js:134:12)
at REPLServer.defaultEval (repl.js:486:29)
at bound (domain.js:416:15)
at REPLServer.runBound [as eval] (domain.js:427:12)
at REPLServer.onLine (repl.js:819:10)
at REPLServer.emit (events.js:387:35)
This might just be an issue with the renaming from assertThrowsAsync
-> assertRejects
and perhaps we should keep assertThrowsAsync
then change assertRejects
to assert that a promise is rejected?
Issue Analytics
- State:
- Created 2 years ago
- Comments:19 (19 by maintainers)
Top Results From Across the Web
Mocha / Node: how to use assert.rejects()? - Stack Overflow
Returning a Promise (or using the done parameter) in Mocha signals to it that the test is asynchronous so the test waits for...
Read more >assert.rejects() - QUnit API
When testing code that is expected to return a rejected promise based on a specific set of circumstances, use assert.rejects() for testing and...
Read more >assert.rejects() function in Node.js - Tutorialspoint
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not...
Read more >assert.rejects passes if the given function throws synchronously
In this case, no Promise is returned at all, so there is nothing that "rejects". From an ergonomics perspective, if I were writing...
Read more >Node.js assert.rejects() Function - GeeksforGeeks
The assert module provides a set of assertion functions for verifying invariants. The assert.rejects() function awaits the asyncFn promise ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay my bad, I totally missed the point of synchronicity, which was the subject of this issue, and went in an insanely wrong direction. Doesn’t feel good to be dumb 😆
Sorry for (unintentionally) wasting your time.
View async functions as only rejecting a promise because that’s what they do.
Before changes :
function
throws => useassertThrows
async function
rejects/throws => useassertRejects
function
that returns a promise throws synchronously => useassertThrows
, but right nowassertRejects
incorrectly passesAfter changes:
function
throws => useassertThrows
async function
rejects/throws => useassertRejects
function
that returns a promise throws synchronously => useassertThrows
andassertRejects
should fail because it throws synchronously