Skip to content

rejects.toThrowError again #8140

@nshaikhinurov

Description

@nshaikhinurov

💥 Regression Report

Related to #3601. I was reading .rejects and .toThrow docs and found inconsistent example imho. Google got me to #3601. Please could anyone help me to understand this issue if i misunderstood anything.

Last working version

Worked up to version: 20.0.3 I think

Stopped working in version: after #4884 i think

To Reproduce

Steps to reproduce the behavior:

sorry for broken code block. Couldn't fix

test("rejects-toThrow test", async () => {
		const myError = new Error("error message")

		// (1) [passes]
		// rejects unwraps the reason to the string literal
		await expect(Promise.reject('octopus')).rejects.toBe('octopus')

		// (2) [passes]
		// rejects unwraps the reason to myError object
		await expect(Promise.reject(myError)).rejects.toBe(myError)

		// (3) [passes]
		// rejects unwraps the reason to the function which throws when called
		// prettier-ignore
		await expect(Promise.reject(() => {throw myError})).rejects.toThrow(myError)

		// (4) [passes] why?
		// INCONSISTENT BEHAVIOUR IMHO
		// from test case (2) we know that rejects unwraps the reason to myError object
		// toThrow supposed to work with functions and not objects that's why test cases (5),(5.1) fail as expected
		// P.S. toThrow with error object as an argument checks error message equality
		await expect(Promise.reject(myError)).rejects.toThrow(myError)

		// (5) [fails]
		// --------------------------------
		// Matcher error: received value must be a function
		// Received has type:  object
		// Received has value: [Error: error message]
		// --------------------------------
		expect(myError).toThrow(myError)

		// (5.1) [fails]
		// --------------------------------
		// Matcher error: received value must be a function
		// Received has type:  string
		// Received has value: "octopus"
		// --------------------------------
		expect('octopus').toThrow('octopus')

		// (6) [fails]
		// So what behaviour should be in this test case?
		// A BUG IMHO
		// from test case (1) we know that rejects unwraps the reason to the string literal
		// from test case (5.1) we know that toThrow receiving a string fails with a Matcher error
		// but here there is no Matcher error. Jest says:
		// --------------------------------
		// Expected substring: "octopus"
		// Received function did not throw
		// --------------------------------
		// Why it now considers unwraped reason from rejects to be a function?
		// P.P.S. toThrow with string as an argument checks error message includes the substring
		await expect(Promise.reject('octopus')).rejects.toThrow('octopus')
	})

Expected behavior

As stated by @BetterCallSKY in #3601 in his 1st message before mergin PR #4884

Link to repl or repo (highly encouraged)

repl.it demo

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: Windows 10
    CPU: (8) x64 AMD Ryzen 5 2500U with Radeon Vega Mobile Gfx
  Binaries:
    Node: 10.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

"jest": "^24.5.0"

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions