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

Clearer messages for Node assert errors #8792

Merged
merged 2 commits into from Aug 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@
- `[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-matcher-utils]` Add color options to `matcherHint` ([#8795](https://github.com/facebook/jest/pull/8795))
- `[jest-circus/jest-jasmine2]` Give clearer output for Node assert errors ([#8792](https://github.com/facebook/jest/pull/8792))

### Fixes

Expand Down
12 changes: 6 additions & 6 deletions e2e/__tests__/__snapshots__/failures.test.ts.snap
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 9 additions & 11 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Expand Up @@ -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 + '(') +
Expand All @@ -114,15 +121,6 @@ const assertMatcherHint = (
chalk.dim(')');
}

if (operator === '==') {
message +=
' or ' +
chalk.dim('assert') +
chalk.dim('(') +
chalk.red('received') +
chalk.dim(') ');
}

return message;
};

Expand Down Expand Up @@ -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`) +
Expand Down
25 changes: 13 additions & 12 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Expand Up @@ -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 + '(') +
Expand All @@ -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;
};

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