Skip to content

Commit

Permalink
Improve error message format for Node's assert.fail (#9262)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmd authored and SimenB committed Dec 7, 2019
1 parent 9ac2dcd commit f4940ae
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions e2e/__tests__/__snapshots__/failures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ FAIL __tests__/assertionError.test.js
✕ assert.doesNotThrow
✕ assert.throws
✕ async
✕ assert.fail
✕ assert.fail with a message
● assert
Expand Down Expand Up @@ -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`] = `
Expand Down
8 changes: 8 additions & 0 deletions e2e/failures/__tests__/assertionError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});
12 changes: 12 additions & 0 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
};

Expand Down Expand Up @@ -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)}`) +
Expand Down
12 changes: 12 additions & 0 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
};

Expand Down Expand Up @@ -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)}`) +
Expand Down

0 comments on commit f4940ae

Please sign in to comment.