From be3c4a1ce3b17cab3f34cc516eda7d9ece9b2a33 Mon Sep 17 00:00:00 2001 From: Stuart Cook Date: Wed, 7 Aug 2019 18:31:02 +1000 Subject: [PATCH] Clearer messages for Node assert errors --- CHANGELOG.md | 1 + .../__snapshots__/failures.test.ts.snap | 12 ++++----- .../jest-circus/src/formatNodeAssertErrors.ts | 20 +++++++-------- .../src/assertionErrorMessage.ts | 25 ++++++++++--------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6283466826be..916baad0cc56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `[@jest/core, @jest/test-sequencer]` Support async sort in custom `testSequencer` ([#8642](https://github.com/facebook/jest/pull/8642)) - `[jest-runtime, @jest/fake-timers]` Add `jest.advanceTimersToNextTimer` ([#8713](https://github.com/facebook/jest/pull/8713)) - `[@jest-transform]` Extract transforming require logic within `jest-core` into `@jest-transform` ([#8756](https://github.com/facebook/jest/pull/8756)) +- `[jest-circus/jest-jasmine2]` Give clearer output for Node assert errors ([#8792](https://github.com/facebook/jest/pull/8792)) ### Fixes diff --git a/e2e/__tests__/__snapshots__/failures.test.ts.snap b/e2e/__tests__/__snapshots__/failures.test.ts.snap index a9ad9ea74cf2..058132cbe250 100644 --- a/e2e/__tests__/__snapshots__/failures.test.ts.snap +++ b/e2e/__tests__/__snapshots__/failures.test.ts.snap @@ -399,7 +399,7 @@ FAIL __tests__/assertionError.test.js ● assert - assert.equal(received, expected) or assert(received) + assert(received) Expected value to be equal to: true @@ -418,7 +418,7 @@ FAIL __tests__/assertionError.test.js ● assert with a message - assert.equal(received, expected) or assert(received) + assert(received) Expected value to be equal to: true @@ -440,7 +440,7 @@ FAIL __tests__/assertionError.test.js ● assert.ok - assert.equal(received, expected) or assert(received) + assert(received) Expected value to be equal to: true @@ -459,7 +459,7 @@ FAIL __tests__/assertionError.test.js ● assert.ok with a message - assert.equal(received, expected) or assert(received) + assert(received) Expected value to be equal to: true @@ -481,7 +481,7 @@ FAIL __tests__/assertionError.test.js ● assert.equal - assert.equal(received, expected) or assert(received) + assert.equal(received, expected) Expected value to be equal to: 2 @@ -747,7 +747,7 @@ FAIL __tests__/assertionError.test.js ● async - assert.equal(received, expected) or assert(received) + assert.equal(received, expected) Expected value to be equal to: "hello" diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 2d086e7a2558..498688538195 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -101,10 +101,17 @@ const assertThrowingMatcherHint = (operatorName: string) => const assertMatcherHint = ( operator: string | undefined | null, operatorName: string, + expected: unknown, ) => { let message = ''; - if (operatorName) { + if (operator === '==' && expected === true) { + message = + chalk.dim('assert') + + chalk.dim('(') + + chalk.red('received') + + chalk.dim(')'); + } else if (operatorName) { message = chalk.dim('assert') + chalk.dim('.' + operatorName + '(') + @@ -114,15 +121,6 @@ const assertMatcherHint = ( chalk.dim(')'); } - if (operator === '==') { - message += - ' or ' + - chalk.dim('assert') + - chalk.dim('(') + - chalk.red('received') + - chalk.dim(') '); - } - return message; }; @@ -160,7 +158,7 @@ function assertionErrorMessage( } return ( - buildHintString(assertMatcherHint(operator, operatorName)) + + buildHintString(assertMatcherHint(operator, operatorName, expected)) + chalk.reset(`Expected value ${operatorMessage(operator)}`) + ` ${printExpected(expected)}\n` + chalk.reset(`Received:\n`) + diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index 1cb63a9ecb20..6dbb8959654f 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -62,10 +62,20 @@ const assertThrowingMatcherHint = (operatorName: string) => chalk.dim(')') : ''; -const assertMatcherHint = (operator: string | null, operatorName: string) => { +const assertMatcherHint = ( + operator: string | null, + operatorName: string, + expected: unknown, +) => { let message = ''; - if (operatorName) { + if (operator === '==' && expected === true) { + message = + chalk.dim('assert') + + chalk.dim('(') + + chalk.red('received') + + chalk.dim(')'); + } else if (operatorName) { message = chalk.dim('assert') + chalk.dim('.' + operatorName + '(') + @@ -75,15 +85,6 @@ const assertMatcherHint = (operator: string | null, operatorName: string) => { chalk.dim(')'); } - if (operator === '==') { - message += - ' or ' + - chalk.dim('assert') + - chalk.dim('(') + - chalk.red('received') + - chalk.dim(') '); - } - return message; }; @@ -121,7 +122,7 @@ function assertionErrorMessage( } return ( - buildHintString(assertMatcherHint(operator, operatorName)) + + buildHintString(assertMatcherHint(operator, operatorName, expected)) + chalk.reset(`Expected value ${operatorMessage(operator)}`) + ` ${printExpected(expected)}\n` + chalk.reset(`Received:\n`) +