diff --git a/CHANGELOG.md b/CHANGELOG.md index 84cb35b41293..e45388058ff2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixes +- `[jest-circus, jest-jasmine2]` Fix error messages for Node's `assert.throes` ([#13322](https://github.com/facebook/jest/pull/13322)) - `[jest-haste-map]` Remove `__proto__` usage ([#13256](https://github.com/facebook/jest/pull/13256)) - `[jest-mock]` Improve `spyOn` typings to handle optional properties ([#13247](https://github.com/facebook/jest/pull/13247)) - `[jest-snapshot]` Throw useful error when an array is passed as property matchers ([#13263](https://github.com/facebook/jest/pull/13263)) diff --git a/e2e/__tests__/__snapshots__/failures.test.ts.snap b/e2e/__tests__/__snapshots__/failures.test.ts.snap index c570616b4240..4e71842dc24b 100644 --- a/e2e/__tests__/__snapshots__/failures.test.ts.snap +++ b/e2e/__tests__/__snapshots__/failures.test.ts.snap @@ -422,6 +422,8 @@ exports[`works with node assert 1`] = ` ✕ assert.ifError ✕ assert.doesNotThrow ✕ assert.throws + ✕ assert.throws with different error messages + ✕ assert.throws with different error types ✕ async ✕ assert.fail ✕ assert.fail with a message @@ -795,10 +797,51 @@ exports[`works with node assert 1`] = ` | ^ 77 | }); 78 | - 79 | test('async', async () => { + 79 | test('assert.throws with different error messages', () => { at Object.throws (__tests__/assertionError.test.js:76:10) + ● assert.throws with different error messages + + assert.throws(function) + + Expected values to be strictly deep-equal: + + actual - expected + + Comparison { + + message: 'message 1' + - message: 'message 2' + } + + 78 | + 79 | test('assert.throws with different error messages', () => { + > 80 | assert.throws( + | ^ + 81 | () => { + 82 | throw new Error('message 1'); + 83 | }, + + at Object.throws (__tests__/assertionError.test.js:80:10) + + ● assert.throws with different error types + + assert.throws(function) + + The "TypeError" validation function is expected to return "true". Received TypeError: SyntaxError: message 1 + + 89 | + 90 | test('assert.throws with different error types', () => { + > 91 | assert.throws(() => { + | ^ + 92 | throw new SyntaxError('message 1'); + 93 | }, TypeError); + 94 | }); + + at Object.throws (__tests__/assertionError.test.js:91:10) + Caught error: + SyntaxError: message 1 + at Object.throws (__tests__/assertionError.test.js:91:10) + ● async assert.equal(received, expected) @@ -820,29 +863,29 @@ exports[`works with node assert 1`] = ` hello + goodbye - 78 | - 79 | test('async', async () => { - > 80 | assert.equal('hello\\ngoodbye', 'hello', 'hmmm'); - | ^ - 81 | }); - 82 | - 83 | test('assert.fail', () => { + 95 | + 96 | test('async', async () => { + > 97 | assert.equal('hello\\ngoodbye', 'hello', 'hmmm'); + | ^ + 98 | }); + 99 | + 100 | test('assert.fail', () => { - at Object.equal (__tests__/assertionError.test.js:80:10) + at Object.equal (__tests__/assertionError.test.js:97:10) ● assert.fail assert.fail(received, expected) - 82 | - 83 | test('assert.fail', () => { - > 84 | assert.fail(); - | ^ - 85 | }); - 86 | - 87 | test('assert.fail with a message', () => { + 99 | + 100 | test('assert.fail', () => { + > 101 | assert.fail(); + | ^ + 102 | }); + 103 | + 104 | test('assert.fail with a message', () => { - at Object.fail (__tests__/assertionError.test.js:84:10) + at Object.fail (__tests__/assertionError.test.js:101:10) ● assert.fail with a message @@ -851,14 +894,14 @@ exports[`works with node assert 1`] = ` Message: error! - 86 | - 87 | test('assert.fail with a message', () => { - > 88 | assert.fail('error!'); - | ^ - 89 | }); - 90 | + 103 | + 104 | test('assert.fail with a message', () => { + > 105 | assert.fail('error!'); + | ^ + 106 | }); + 107 | - at Object.fail (__tests__/assertionError.test.js:88:10)" + at Object.fail (__tests__/assertionError.test.js:105:10)" `; exports[`works with snapshot failures 1`] = ` diff --git a/e2e/failures/__tests__/assertionError.test.js b/e2e/failures/__tests__/assertionError.test.js index cc5909918e5b..ee9e62c1ed05 100644 --- a/e2e/failures/__tests__/assertionError.test.js +++ b/e2e/failures/__tests__/assertionError.test.js @@ -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'); }); diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 4ea53b16c660..75fd0ed17683 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -150,6 +150,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') + diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index 7c149088f43f..3408af159c92 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -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') +