Skip to content

Inconsistency in rejects.toThrow() #6675

@Yaojian

Description

@Yaojian

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions