deno_std: assertRejects does not fail when an error is thrown, but a promise was not rejected
Describe 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?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 19 (19 by maintainers)
Commits related to this issue
- fix(assertRejects): fails on synchronous throw #1302 (#2234) Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com> — committed to denoland/deno_std by Sorikairox 2 years ago
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 synchronouslyAsync functions capture thrown errors and turn them into promise rejections. If the function wasn’t actually async and just returns a promise normally and it were to throw instead, the
assertRejects
assertion should fail because the function doesn’t reject, it throws.I think if
assertThrows
returns a rejected promise, it would fail because the function didn’t throw. I think that would be desired behavior.Having this behavior would make it easier to catch and fix issues like errors being thrown incorrectly instead of being rejected and vice versa.
Here’s showing the critical difference between the two and something that will pass type checking:
A more common scenario of using this might be along the lines of:
I think we should…
assertRejects
to only pass when the function executed returns a promise that rejects.assertRejects
.