question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

assertRejects does not fail when an error is thrown, but a promise was not rejected

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
Sorikairoxcommented, May 15, 2022

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.

1reaction
dsherretcommented, May 15, 2022

View async functions as only rejecting a promise because that’s what they do.

Before changes :

  • assert that a function throws => use assertThrows
  • assert that an async function rejects/throws => use assertRejects
  • assert that a function that returns a promise throws synchronously => use assertThrows, but right now assertRejects incorrectly passes

After changes:

  • assert that a function throws => use assertThrows
  • assert that an async function rejects/throws => use assertRejects
  • assert that a function that returns a promise throws synchronously => use assertThrows and assertRejects should fail because it throws synchronously
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found