oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.47k stars 2.78k forks source link

Nested proxies with `toEqual` fails assertion that works in Jest #4941

Open tomhicks opened 1 year ago

tomhicks commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Darwin 22.2.0 arm64 arm

What steps can reproduce the bug?

function proxify<T extends object>(thing: T): T {
  return new Proxy(thing, {
    get: function (target, prop) {
      return (target as any)[prop];
    },
    set: function (target, prop, value) {
      (target as any)[prop] = value;
      return true;
    },
  });
}

test("nested proxy equality", () => {

  const thing = { a: [1] };
  const pr = proxify(thing);

  thing.a = proxify(thing.a);

  expect(equal(thing, pr)).toBe(true); // this is fine
  expect(pr).toEqual({ a: [1] }); // this fails
});

What is the expected behavior?

The test should pass. Deeply-nested proxies should be considered equal according to toEqual. This works in Jest.

What do you see instead?

The test fails, printing out nearly identical objects.

error: expect(received).toEqual(expected)

+ {"a":[1]}
- {
-   a: [
-     1
-   ]
- }

- Expected  - 5
+ Received  + 1

Additional information

No response

tomhicks commented 1 year ago

Tagging @dai-shi as this prevents Valtio from working as it should, as it deeply nests proxies.