diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc41106eef2..370578243102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ - `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) - `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196)) - `[jest-fake-timers]` Support `util.promisify` on `setTimeout` ([#9180](https://github.com/facebook/jest/pull/9180)) +- `[jest-jasmine2, jest-circus]` Improve error message format for Node's assert.fail ([#9262](https://github.com/facebook/jest/pull/9262)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/failures.test.ts.snap b/e2e/__tests__/__snapshots__/failures.test.ts.snap index 5c0197b587e7..7d5c151ef298 100644 --- a/e2e/__tests__/__snapshots__/failures.test.ts.snap +++ b/e2e/__tests__/__snapshots__/failures.test.ts.snap @@ -396,6 +396,8 @@ FAIL __tests__/assertionError.test.js ✕ assert.doesNotThrow ✕ assert.throws ✕ async + ✕ assert.fail + ✕ assert.fail with a message ● assert @@ -772,8 +774,39 @@ FAIL __tests__/assertionError.test.js | ^ 81 | }); 82 | + 83 | test('assert.fail', () => { at Object.equal (__tests__/assertionError.test.js:80:10) + + ● assert.fail + + assert.fail(received, expected) + + 82 | + 83 | test('assert.fail', () => { + > 84 | assert.fail(); + | ^ + 85 | }); + 86 | + 87 | test('assert.fail with a message', () => { + + at Object.fail (__tests__/assertionError.test.js:84:10) + + ● assert.fail with a message + + assert.fail(received, expected) + + Message: + error! + + 86 | + 87 | test('assert.fail with a message', () => { + > 88 | assert.fail('error!'); + | ^ + 89 | }); + 90 | + + at Object.fail (__tests__/assertionError.test.js:88:10) `; exports[`works with snapshot failures 1`] = ` diff --git a/e2e/failures/__tests__/assertionError.test.js b/e2e/failures/__tests__/assertionError.test.js index f1f563c7b728..cc5909918e5b 100644 --- a/e2e/failures/__tests__/assertionError.test.js +++ b/e2e/failures/__tests__/assertionError.test.js @@ -79,3 +79,11 @@ test('assert.throws', () => { test('async', async () => { assert.equal('hello\ngoodbye', 'hello', 'hmmm'); }); + +test('assert.fail', () => { + assert.fail(); +}); + +test('assert.fail with a message', () => { + assert.fail('error!'); +}); diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index ddc2e7f63a6c..0b3f79954804 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -76,6 +76,10 @@ const getOperatorName = (operator: string | undefined, stack: string) => { if (stack.match('.throws')) { return 'throws'; } + // this fallback is only needed for versions older than node 10 + if (stack.match('.fail')) { + return 'fail'; + } return ''; }; @@ -155,6 +159,14 @@ function assertionErrorMessage( ); } + if (operatorName === 'fail') { + return ( + buildHintString(assertMatcherHint(operator, operatorName, expected)) + + chalk.reset(hasCustomMessage ? 'Message:\n ' + message : '') + + trimmedStack + ); + } + return ( buildHintString(assertMatcherHint(operator, operatorName, expected)) + chalk.reset(`Expected value ${operatorMessage(operator)}`) + diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index 1da6fb9b5688..27bcb4b51b08 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -42,6 +42,10 @@ const getOperatorName = (operator: string | null, stack: string) => { if (stack.match('.throws')) { return 'throws'; } + // this fallback is only needed for versions older than node 10 + if (stack.match('.fail')) { + return 'fail'; + } return ''; }; @@ -121,6 +125,14 @@ function assertionErrorMessage( ); } + if (operatorName === 'fail') { + return ( + buildHintString(assertMatcherHint(operator, operatorName, expected)) + + chalk.reset(hasCustomMessage ? 'Message:\n ' + message : '') + + trimmedStack + ); + } + return ( buildHintString(assertMatcherHint(operator, operatorName, expected)) + chalk.reset(`Expected value ${operatorMessage(operator)}`) +