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

Jest: expected/received diff appears twice #3634

Closed
OliverJAsh opened this issue Feb 17, 2023 · 7 comments · Fixed by #3638
Closed

Jest: expected/received diff appears twice #3634

OliverJAsh opened this issue Feb 17, 2023 · 7 comments · Fixed by #3638

Comments

@OliverJAsh
Copy link

🐛 Bug Report

const fc = require("fast-check");

it("works", () => {
  fc.assert(
    fc.property(fc.string(), (a) => {
      expect("foo").toEqual("bar");
    })
  );
});

To Reproduce

In a new directory:

  • yarn add jest fast-check
  • Copy the contents above into test.js
  • Run jest

Result:

$ jest
 FAIL  ./test.js
  ✕ works (7 ms)

  ● works

    Property failed after 1 tests
    { seed: -1070580960, path: "0:0", endOnFailure: true }
    Counterexample: [""]
    Shrunk 1 time(s)
    Got error: Error: expect(received).toEqual(expected) // deep equality

    Expected: "bar"
    Received: "foo"

    Stack trace: Error: expect(received).toEqual(expected) // deep equality

    Expected: "bar"
    Received: "foo"

      4 |   fc.assert(
      5 |     fc.property(fc.string(), (a) => {
    > 6 |       expect("foo").toEqual("bar");
        |                     ^
      7 |     })
      8 |   );
      9 | });

      at toEqual (test.js:6:21)
      at Property.predicate (node_modules/fast-check/lib/check/property/Property.js:17:86)
      at Property.run (node_modules/fast-check/lib/check/property/Property.generic.js:49:33)
      at runIt (node_modules/fast-check/lib/check/runner/Runner.js:21:30)
      at check (node_modules/fast-check/lib/check/runner/Runner.js:73:11)
      at Object.assert (node_modules/fast-check/lib/check/runner/Runner.js:77:17)
      at Object.assert (test.js:4:6)
      Hint: Enable verbose mode in order to have the list of all failing values encountered during the run
      at buildError (node_modules/fast-check/lib/check/runner/utils/RunDetailsFormatter.js:131:15)
      at throwIfFailed (node_modules/fast-check/lib/check/runner/utils/RunDetailsFormatter.js:143:11)
      at reportRunDetails (node_modules/fast-check/lib/check/runner/utils/RunDetailsFormatter.js:156:16)
      at Object.assert (node_modules/fast-check/lib/check/runner/Runner.js:81:52)
      at Object.assert (test.js:4:6)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.198 s, estimated 1 s
Ran all test suites.

Observe how the expected/received diff appears twice:

    Got error: Error: expect(received).toEqual(expected) // deep equality

    Expected: "bar"
    Received: "foo"

    Stack trace: Error: expect(received).toEqual(expected) // deep equality

    Expected: "bar"
    Received: "foo"

Expected behavior

The expected/received diff should only appear once.

If you have one, please provide a minimal repository reproducing the issue on GitHub.

Your environment

Packages / Softwares Version(s)
fast-check 3.6.3
node 16.3.0
TypeScript*

*Only for TypeScript's users


This issue seems similar to #399, although not exactly the same.

@dubzzz
Copy link
Owner

dubzzz commented Feb 17, 2023

Hey,

It's a partly known issue. If you are using the latest version of Jest, can you give a try to the flag errorWithCause.
Now that Jest supports Error with cause, it should be enough to fully fix the problem or at least I hope so.

It can easily be done by running the following code either on top of your file or in the setups file of Jest if you do use one:

fc.configureGlobal({ errorWithCause: true });

I'll probably add this setup as the default one in @fast-check/jest but not immediately in fast-check as it would require a major bump.

@OliverJAsh
Copy link
Author

OliverJAsh commented Feb 17, 2023

This is what I tried:

 const fc = require("fast-check");
+fc.configureGlobal({ errorWithCause: true });
 
 it("works", () => {
   fc.assert(
     fc.property(fc.string(), (a) => {
       expect("foo").toEqual("bar");
     })
   );
 });

Below is the result. Unfortunately it looks like the diff ("expected" and "received") doesn't appear at all now. 🤔

$ jest
 FAIL  ./test.js
  ✕ works (6 ms)

  ● works

    Property failed after 1 tests
    { seed: -380473213, path: "0:0", endOnFailure: true }
    Counterexample: [""]
    Shrunk 1 time(s)

    Hint: Enable verbose mode in order to have the list of all failing values encountered during the run

      3 |
      4 | it("works", () => {
    > 5 |   fc.assert(
        |      ^
      6 |     fc.property(fc.string(), (a) => {
      7 |       expect("foo").toEqual("bar");
      8 |     })

      at buildError (node_modules/fast-check/lib/check/runner/utils/RunDetailsFormatter.js:134:19)
      at throwIfFailed (node_modules/fast-check/lib/check/runner/utils/RunDetailsFormatter.js:143:11)
      at reportRunDetails (node_modules/fast-check/lib/check/runner/utils/RunDetailsFormatter.js:156:16)
      at Object.assert (node_modules/fast-check/lib/check/runner/Runner.js:81:52)
      at Object.assert (test.js:5:6)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.225 s, estimated 1 s
Ran all test suites.

@dubzzz
Copy link
Owner

dubzzz commented Feb 18, 2023

I'll check on jest side 🤔 In theory starting at 29.4.x they were supposed to properly extract causes linked to errors. Anyway, I'll also recheck the double-print we have on the default option as it's not good.

Seems that the cause is properly forwarded by fast-check as vitest is capable of reading it:
image

Same for bare-metal node:
image

@dubzzz
Copy link
Owner

dubzzz commented Feb 19, 2023

For the Error with cause, issue seems to be coming from Jest. I'm currently working on a PR to make it fully supported. For the other issue, I'll attempt a fix on Jest side too. I'll let you know as soon as something gets merged on Jest side.

@dubzzz
Copy link
Owner

dubzzz commented Feb 19, 2023

A PR attempting to make Error with cause working has been opened on Jest's side, see jestjs/jest#13935

@dubzzz
Copy link
Owner

dubzzz commented Feb 19, 2023

Regarding the remaining issue at the origin the bug report, I mean:

    Got error: Error: expect(received).toEqual(expected) // deep equality

    Expected: "bar"
    Received: "foo"

    Stack trace: Error: expect(received).toEqual(expected) // deep equality

    Expected: "bar"
    Received: "foo"

Actually the bug is fully on fast-check's side 🤔 I'll have to check how I can deal with it properly. It seems that most of the time stack includes the message itself. So printing both implies duplicates.

dubzzz added a commit that referenced this issue Feb 19, 2023
While the change could have been considered as a bug fix, I prefer including it as a fresh new feature to avoid risks of breaking people relying on this bug.

Fixes #3634
dubzzz added a commit that referenced this issue Feb 23, 2023
* ✨ Stop repeating the error twice in reports

While the change could have been considered as a bug fix, I prefer including it as a fresh new feature to avoid risks of breaking people relying on this bug.

Fixes #3634

* Drop Error: from report

* versions

* unfify stacks cross os

* safer poisoned

* format

* Update packages/fast-check/test/e2e/NoRegressionStack.spec.ts

* Update packages/fast-check/test/e2e/__snapshots__/NoRegressionStack.spec.ts.snap

* Update packages/fast-check/test/e2e/__snapshots__/NoRegressionStack.spec.ts.snap

* Update packages/fast-check/test/e2e/__snapshots__/NoRegressionStack.spec.ts.snap

* Update packages/fast-check/test/e2e/__snapshots__/NoRegressionStack.spec.ts.snap

* Update packages/fast-check/test/e2e/__snapshots__/NoRegressionStack.spec.ts.snap

* fix no reg

* fix unit
@dubzzz
Copy link
Owner

dubzzz commented Feb 23, 2023

Fix for non-cause mode will be included in version 3.7.0 of fast-check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants