question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

expect `toBe` has misleading error message

See original GitHub issue

Do you want to request a feature or report a bug? Feature (it is more like a fix)

What is the current behavior? Sometimes developer may want to check for identity. Currently, expect.toBe does this. But the error message is misleading:

Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.

Because the message suggests to change toBe -> toEqual. So new developer may make this change which may lead to a bug in the project (or deoptimization).

I also think, that toBe and toEqual do not communicate the difference between each other clearly.

Suggestion

  1. Maybe change name toBe -> toBeExact or toShallowEqual.
  2. Maybe change message You probably need to use 'toEqual' instead. -> If you want to check for deep equality, pease, use 'toEqual' instead.
  3. Maybe change toEqual -> toDeepEqual

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, May 1, 2018

I like it

1reaction
ggarekcommented, May 1, 2018

Sure

file

test('identity check', () => {
  const value = {};
  const identity = x => Object.assign({}, x); // Wrong implementation
  expect(identity(value)).toBe(value);
});

output

  FAIL  src/test.js
  ● identity check

    expect(received).toBe(expected) // Object.is equality

    Expected value to be:
      {}
    Received:
      {}

    Difference:

    Compared values have no visual difference. Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.

      2 |   const value = {};
      3 |   const identity = x => Object.assign({}, x); // Wrong implementation
    > 4 |   expect(identity(value)).toBe(value);
      5 | });
      6 |

      at Object.<anonymous> (src/test.js:4:27)

jest v22.4.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
Thus, when pass is false, message should return the error message for when expect(x).yourMatcher() fails. And when pass is true, ...
Read more >
How to add custom message to Jest expect? - Stack Overflow
expect.extend({ toBeValid(received, validator) { if ... Another way to add a custom error message is by using the fail() method:
Read more >
Expect · Jest
Thus, when pass is false, message should return the error message for when expect(x).yourMatcher() fails. And when pass is true, message should return...
Read more >
Jest Expect - w3resource
Thus, if pass is false, message will have to return the error message for when expect(x).yourMatcher() fails. And if pass is true, ...
Read more >
Test script examples - Postman Learning Center
If the response code is 200 , the test will pass, otherwise it will ... { //errors array is empty pm.expect(jsonData.errors).to.be.empty; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found