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

rejects.toThrow() only works when an instance of Error is thrown #9748

Closed
farah opened this issue Apr 2, 2020 · 2 comments
Closed

rejects.toThrow() only works when an instance of Error is thrown #9748

farah opened this issue Apr 2, 2020 · 2 comments

Comments

@farah
Copy link

farah commented Apr 2, 2020

🐛 Bug Report

Currently the .rejects.toThrow() assertion only works when the argument is an instance of new Error(). It does not work when it's a string or a plain object.

To Reproduce

describe('Jest bug', () => {
	it('fails when string is thrown', async () => {
		await expect(async () => {
			throw 'octopus';
		}).rejects.toThrow();
	});

	it('fails when plain object is thrown', async () => {
		await expect(async () => {
			throw { octopus: 'octopus' };
		}).rejects.toThrow();
	});

	it('passes when Error object is thrown', async () => {
		await expect(async () => {
			throw Error('octopus');
		}).rejects.toThrow();
	});
});

Expected behavior

Test cases 1 and 2 should pass. I know this may very well be a feature and not a bug and in that case I'd like to open the discussion in changing the behaviour of .toThrow() to pass on the first two cases. I think the results are rather inconsistent.

@jeysal
Copy link
Contributor

jeysal commented May 25, 2020

Correct me if I'm wrong, but I think this is a duplicate of #6675

@jeysal jeysal closed this as completed May 25, 2020
@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 May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants