Skip to content

Commit

Permalink
fix: adjusted assert.throws error format
Browse files Browse the repository at this point in the history
fixes #13264
  • Loading branch information
lpizzinidev committed Sep 26, 2022
1 parent 35e8b6a commit 96cbd79
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions e2e/failures/__tests__/assertionError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ test('assert.throws', () => {
assert.throws(() => {});
});

test('assert.throws with different error messages', () => {
assert.throws(
() => {
throw new Error('message 1');
},
{
message: 'message 2',
},
);
});

test('assert.throws with different error types', () => {
assert.throws(() => {
throw new SyntaxError('message 1');
}, TypeError);
});

test('async', async () => {
assert.equal('hello\ngoodbye', 'hello', 'hmmm');
});
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ function assertionErrorMessage(
}

if (operatorName === 'throws') {
if (error.generatedMessage) {
// Thrown error do not match expected one
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset(error.message) +
chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
trimmedStack
);
}
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset('Expected the function to throw an error.\n') +
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ function assertionErrorMessage(
}

if (operatorName === 'throws') {
if (error.generatedMessage) {
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset(error.message) +
chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
trimmedStack
);
}
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset('Expected the function to throw an error.\n') +
Expand Down

0 comments on commit 96cbd79

Please sign in to comment.