From d938ca053a960ba869c5df12c976aa31a118e88b Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Mon, 26 Sep 2022 15:57:42 +0200 Subject: [PATCH 1/3] fix: adjusted assert.throws error format fixes #13264 --- e2e/failures/__tests__/assertionError.test.js | 17 +++++++++++++++++ .../jest-circus/src/formatNodeAssertErrors.ts | 9 +++++++++ .../jest-jasmine2/src/assertionErrorMessage.ts | 8 ++++++++ 3 files changed, 34 insertions(+) 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..23cfa07e0b51 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -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') + 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') + From 1a23fe7f247855f16cfa95535ad36dfed389c14a Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Tue, 27 Sep 2022 14:57:06 +0200 Subject: [PATCH 2/3] fix snapshot --- .../__snapshots__/failures.test.ts.snap | 91 ++++++++++++++----- .../jest-circus/src/formatNodeAssertErrors.ts | 1 - 2 files changed, 67 insertions(+), 25 deletions(-) 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/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 23cfa07e0b51..75fd0ed17683 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -151,7 +151,6 @@ function assertionErrorMessage( if (operatorName === 'throws') { if (error.generatedMessage) { - // Thrown error do not match expected one return ( buildHintString(assertThrowingMatcherHint(operatorName)) + chalk.reset(error.message) + From c063135c18a3012ec9c5513b501e660e0938fd60 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 28 Sep 2022 08:44:20 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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))