denoland / std

The Deno Standard Library
https://jsr.io/@std
MIT License
3.17k stars 621 forks source link

toMatchObject doesn't support matchers but only exact values #6111

Open NaveenNeti opened 3 weeks ago

NaveenNeti commented 3 weeks ago

Thank you to all for the work on this project.

The JSR page for @std/expect states "Jest compatible expect assertion functions"

While trying to use toMatchObject I noticed it doesn't seem to support asymmetric matchers similar to what jest would allow.

e.g. within Jest

  expect({ position: { x: 0, y: 0 } }).toMatchObject({
    position: {
      x: expect.any(Number),
      y: expect.any(Number)
    }
  });

Running the same test in Deno:

Deno.test("CustomMatcher tests", () => {
    expect({ position: { x: 0, y: 0 } }).toMatchObject({
        position: {
            x: expect.any(Number),
            y: expect.any(Number),
        },
    });
});

Results in the output:

error: AssertionError: Values are not equal.
    [Diff] Actual / Expected
    {
      position: {
+       x: {
+         value: [Function: Number],
+       },
+       y: {
+         value: [Function: Number],
+       },
-       x: 0,
-       y: 0,
      },
    }

Describe the solution you'd like

Would be keen to help provide an MR to add support for it assuming the project owners see value.

Versions

deno: 2.0.0
@std/assert/1.0.6
eryue0220 commented 1 week ago

I'll check this later.

NaveenNeti commented 4 days ago

Thank you @eryue0220 I will test this tomorrow 🙏