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.

`toStrictEqual` should not compare return value of `get` function

See original GitHub issue
let a = 0;
class Foo {
    get foo(): i32 {
        return ++a;
    }
}

expect(new Foo()).toStrictEqual(new Foo());
  [Actual]: Foo {
      foo: 3
  }
[Expected]: Foo {
      foo: 4
  }

It not only compares, but also produces side effects.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jtennercommented, Apr 30, 2021

Steps to prove this:

  1. Install jest
  2. create a test that uses expect(someRef).toStrictEqual(anotherRef)
  3. validate the get property function on someRef is executed.

This behavior is by design.

If you think that you truly need strict equality, I might recommend wrapping your object with a special function like this:

function wrap(a: A): B {
  // copy each a value you want to test to a type B
  let b = new B();
  b.prop1 = a.prop1;
  return b;
}

expect(wrap(myA)).toStrictEqual(wrap(otherA));
0reactions
yjhmelodycommented, May 12, 2021

Well, I got it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
This is a deep-equality function that will return true if two objects have the same values (recursively). this.expand ​. A boolean to let...
Read more >
toStrictEqual fails to distinguish 0 from 5e-324 #7941 - GitHub
Be careful about Date objects because they needs value comparison instead of object identity. While we are looking at it, can you take...
Read more >
Testing anonymous function equality with Jest - Stack Overflow
toBe(expected) Expected value to be (using ===): [Function anonymous] Received: [Function anonymous] Difference: Compared values have no visual ...
Read more >
Jest Expect - w3resource
The expect function is used whenever you want to test a value. ... but the error messages that you get on failing tests...
Read more >
Frontend Testing with Jest - Assertions deep dive
Discover how you can write clever Javascript unit tests with the Jest ... identity of values, while toEqual does a deep comparison of...
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