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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions e2e/__tests__/__snapshots__/failures.test.ts.snap
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
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!');
});
11 changes: 11 additions & 0 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Expand Up @@ -76,6 +76,9 @@ const getOperatorName = (operator: string | undefined, stack: string) => {
if (stack.match('.throws')) {
return 'throws';
}
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 +158,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
11 changes: 11 additions & 0 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Expand Up @@ -42,6 +42,9 @@ const getOperatorName = (operator: string | null, stack: string) => {
if (stack.match('.throws')) {
return 'throws';
}
if (stack.match('.fail')) {
return 'fail';
}
return '';
};

Expand Down Expand Up @@ -121,6 +124,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