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

fix(expect): improve toThrow(asymmetricMatcher) failure message #5000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected error not to be instance of ${name}`,
expected,
thrown,
false,
)
}

Expand All @@ -601,9 +600,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
thrown && matcher.asymmetricMatch(thrown),
'expected error to match asymmetric matcher',
'expected error not to match asymmetric matcher',
matcher.toString(),
matcher,
thrown,
false,
)
}

Expand Down
83 changes: 83 additions & 0 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,86 @@ exports[`asymmetric matcher error 16`] = `
"message": "expected 'hello' to deeply equal StringContaining{…}",
}
`;

exports[`asymmetric matcher error 17`] = `
{
"actual": "hello",
"diff": null,
"expected": "StringContaining "xx"",
"message": "expected error to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 18`] = `
{
"actual": "hello",
"diff": "- Expected:
stringContainingCustom<xx>

+ Received:
"hello"",
"expected": "stringContainingCustom<xx>",
"message": "expected error to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 19`] = `
{
"actual": "hello",
"diff": null,
"expected": "StringContaining "ll"",
"message": "expected error not to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 20`] = `
{
"actual": "hello",
"diff": "- Expected:
stringContainingCustom<ll>

+ Received:
"hello"",
"expected": "stringContainingCustom<ll>",
"message": "expected error not to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 21`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected:
StringContaining "ll"

+ Received:
[Error: hello]",
"expected": "StringContaining "ll"",
"message": "expected error to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 22`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected:
stringContainingCustom<ll>

+ Received:
[Error: hello]",
"expected": "stringContainingCustom<ll>",
"message": "expected error to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 23`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected:
[Function MyError1]

+ Received:
[Error: hello]",
"expected": "[Function MyError1]",
"message": "expected error to be instance of MyError1",
}
`;
25 changes: 25 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,31 @@ it('asymmetric matcher error', () => {

// simple truncation if pretty-format is too long
snapshotError(() => expect('hello').toEqual(expect.stringContaining('a'.repeat(40))))

// error message on `toThrow(asymmetricMatcher)` failure
function throwError() {
// eslint-disable-next-line no-throw-literal
throw 'hello'
}
snapshotError(() => expect(throwError).toThrow(expect.stringContaining('xx')))
snapshotError(() => expect(throwError).toThrow((expect as any).stringContainingCustom('xx')))
snapshotError(() => expect(throwError).not.toThrow(expect.stringContaining('ll')))
snapshotError(() => expect(throwError).not.toThrow((expect as any).stringContainingCustom('ll')))
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

snapshotError(() => expect(() => {
throw new Error('hello')
}).toThrow(expect.stringContaining('ll')))
snapshotError(() => expect(() => {
throw new Error('hello')
}).toThrow((expect as any).stringContainingCustom('ll')))

// error constructor
class MyError1 extends Error {}
class MyError2 extends Error {}

snapshotError(() => expect(() => {
throw new MyError2('hello')
}).toThrow(MyError1))
})

it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))
Loading