Skip to content

Commit

Permalink
Clearer messages for Node assert errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Aug 7, 2019
1 parent e740de2 commit 5ec8ba5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
12 changes: 6 additions & 6 deletions e2e/__tests__/__snapshots__/failures.test.ts.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 5ec8ba5

Please sign in to comment.