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.

Inconsistency in `rejects.toThrow()`

See original GitHub issue

🐛 Bug Report

The doc says the argument of toThrow() can be a class for the error, it works when a promise throws an Error instance, but fails when the promise throws a custom object.

To Reproduce

describe('When a promise throws an expression, rejects.toThrow should match the expression by its type', () => {
    it('when the expression is an error', async () => {
        const p = new Promise(() => {
            throw new Error('some-info');
        });
        await expect(p).rejects.toThrow(Error); // passed
    });
    it('when the expression is a custom object', async () => {
        class Exception {
            constructor(public readonly message: string) {
            }
        }

        const p = new Promise(() => {
            throw new Exception('some-info');
        });
        await expect(p).rejects.toThrow(Exception); // failed
    });
});

The first test passes, while the second test fails with the following message:

Expected the function to throw an error of type:
  "Exception"
But it didn't throw anything.

Expected behavior

Both the above two tests pass.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
MarcusPopecommented, Oct 2, 2019

This is also a problem when throwing objects in async/await that are not Error instances and expecting the toThrow() default with no arguments:

test("Bug in jest", async () => {
    expect(() => {
      throw "this is fine";
    }).toThrow();

    await expect(
      (async () => {
        throw new Error("this is fine too");
      })()
    ).rejects.toThrow();

    await expect(
      (async () => {
        throw "this is not fine";
      })()
    ).rejects.toThrow(); // FAILS TEST
  });
0reactions
github-actions[bot]commented, Apr 29, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better Error Handling In NodeJS With Error Classes
This article is for JavaScript and NodeJS developers who want to improve error-handling in their applications.
Read more >
Paulo Freire's Pedagogy of the Oppressed
The justification for a pedagogy of the oppressed; the contradiction between the oppressors and the oppressed, and how it is overcome;.
Read more >
Safari Technology Preview Release Notes - Apple Developer
toMatrix() to throw an exception if its length is not compatible with a px unit ... Fixed Navigator.share() rejecting with the wrong exception...
Read more >
Considering When To Throw Errors, Why To Chain Them, And ...
Inconsistent error handling can lead to many problems in a system such as duplicated code, overly-complex algorithms, error logs that are too ...
Read more >
Parenting Practices and Child Disruptive Behavior Problems ...
Correspondingly, inconsistency is a second parenting practice that has often been linked with elevated rates of child oppositional and aggressive behavior ( ...
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