Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message format for Node's assert.fail #9262

Merged
merged 3 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Comment on lines +80 to +82
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stack.match fallback is only needed for Node versions older than v10.11 because it wan't until v10.11.0 that the AssertionError of assert.fail had an operator value set by default (nodejs/node#22694).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention that in a comment, it's common to grep the codebase for things like node 10 when phasing out support for an EOL version

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