Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in rejects.toThrow() #6675

Closed
Yaojian opened this issue Jul 11, 2018 · 10 comments
Closed

Inconsistency in rejects.toThrow() #6675

Yaojian opened this issue Jul 11, 2018 · 10 comments

Comments

@Yaojian
Copy link

Yaojian commented Jul 11, 2018

🐛 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.

@SimenB
Copy link
Member

SimenB commented Jul 11, 2018

It passes with the synchronous version, it's just rejects which fails. It also passes if Exception extends Error when using rejects. So it seems to be a bug somehow with rejects.

@BetterCallSKY ideas?

@Yaojian
Copy link
Author

Yaojian commented Jul 11, 2018

May be the test fails because createMatcher checks rejects using isError while Exception is NOT a subclass of Error ?

@peterdanis
Copy link
Contributor

Behavior for promises changed in #5670. My understanding of toThrow matcher is that it should only pass in case of Error.

@peterdanis
Copy link
Contributor

To be honest, toThrow matcher doesn't make sense for promises. They just reject when you throw inside them, right?

@lstkz
Copy link
Contributor

lstkz commented Jul 19, 2018

@peterdanis
toThrow doesn't make sense for resolves, you can check discussion in #6678

@peterdanis
Copy link
Contributor

peterdanis commented Jul 19, 2018

I know, before #5670 even expect(someSuccesfulPromise()).resolves.toThrow() passed the test. Nonsense ofcourse.

Therefore I've tried to check, whether object checked in the toThrow matcher (for promises only) is an "error-like" object, otherwise the toThrow matcher would became the same as toEqual. I see in your PR, that assuming "error-like" object was not so good idea :)

But does it make sense to check whether a promise "throws"? Normally one does not use throw in promise, and even if something "throws" inside a promise (e.g. an inner function), it will just reject to thrown value. Afaik promise can only resolve/reject, can't throw.

@MarcusPope
Copy link

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
  });

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants