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

toStrictEqual should not compare return value of get function #344

Open
yjhmelody opened this issue Apr 29, 2021 · 9 comments
Open

toStrictEqual should not compare return value of get function #344

yjhmelody opened this issue Apr 29, 2021 · 9 comments
Assignees
Labels
Wontfix 🚫 This will not be worked on

Comments

@yjhmelody
Copy link

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.

@jtenner
Copy link
Contributor

jtenner commented Apr 30, 2021

Yes. This behavior is mirrored in jest object comparisons. It was by design.

@yjhmelody
Copy link
Author

yjhmelody commented Apr 30, 2021

Yes. This behavior is mirrored in jest object comparisons. It was by design.

Can you provide a comparison without access to set/get ?

Do you mean that it returns 3/4 not 1/2?

@jtenner
Copy link
Contributor

jtenner commented 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));

@jtenner jtenner added the Wontfix 🚫 This will not be worked on label Apr 30, 2021
@jtenner jtenner self-assigned this Apr 30, 2021
@yjhmelody
Copy link
Author

but this is not ts. how to define the function genericly?and compare the private field

@jtenner
Copy link
Contributor

jtenner commented May 1, 2021

I can't deviate from developer expectations. Perhaps make a method on the class instead?

class Foo {
  __asBar(): Bar {
    // Make whatever you want to compare here
  }
}

Yes. This is not an elegant solution. Aspect happens to be very opinionated. I hope this answer suffices.

@jtenner
Copy link
Contributor

jtenner commented May 2, 2021

but this is not ts. how to define the function genericly?and compare the private field

By the way, private fields are compared. The problem is that computed fields are also compared. This is how jest currently works in production when working in a js environment.

@yjhmelody
Copy link
Author

This is really bad, because the type deduction ability of As is much worse than that of TS. Many times a value val type is T | null, but it is not null at a certain position, so I need to cast val to T. The code for the cast is really Too much. So I wrote a get function to encapsulate the forced conversion logic, but even if it is private or not used in the unit test, this function will be called and then all panic. That is to say, the current design of as-pect affects the code implementation details of some as libraries.

@yjhmelody yjhmelody reopened this May 6, 2021
@jtenner
Copy link
Contributor

jtenner commented May 11, 2021

Again. This testing suite is designed with Web Developer ergonomics and expectations. You can leave this open as long as you would like. Sorry for the inconvenience.

@yjhmelody
Copy link
Author

Well, I got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Wontfix 🚫 This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants