timkindberg / jest-when

Jest support for mock argument-matched return values.
MIT License
738 stars 39 forks source link

expect.assertions(1) doesn't work with v3.4.1 #88

Closed voda closed 3 years ago

voda commented 3 years ago

Hey, after updating to v3.4.1, this code starts to fail

const { when } = require("jest-when");

it('works', () => {
    expect.assertions(1);
    expect(true).toBe(true);
});

with:

 FAIL  ./test.js
  ✕ works (3 ms)

  ● works

    expect.assertions(1)

    Expected one assertion to be called but received two assertion calls.

      2 |
      3 | it('works', () => {
    > 4 |     expect.assertions(1);
        |            ^
      5 |     expect(true).toBe(true);
      6 | });
      7 |

      at Object.<anonymous> (test.js:4:12)

Test Suites: 1 failed, 1 total

After removing the require the test passes.

timkindberg commented 3 years ago

ah shoot, yep cause I have to make an assertion in order to grab the reference to the this.expect. I'm not quite sure what to do now if this is going to break folks code. Except I'm not sure most folks use expect.assertions.

voda commented 3 years ago

I think with this behavior there isn't any good workaround. If you copy the test twice into the file, only the first one fails and the second one passes, which can be quite confusing.

I have two possible ideas, how it could be solved (don't know that much about jest, so not sure if it is doable): 1) try to load the expect from jest's node_modules: if require('expect/build/jasmineUtils') fails then try require('jest/node_modules/expect/build/jasmineUtils') 2) since the extra assertion only affects the first test, is there a way to reset the done assertions?

timkindberg commented 3 years ago

Some good ideas and can give them a try.

On Thu, Oct 7, 2021 at 4:44 AM Ondřej Vodáček @.***> wrote:

I think with this behavior there isn't any good workaround. If you copy the test twice into the file, only the first one fails and the second one passes, which can be quite confusing.

I have two possible ideas, how it could be solved (don't know that much about jest, so not sure if it is doable):

  1. try to load the expect from jest's node_modules: if require('expect/build/jasmineUtils') fails then try require('jest/node_modules/expect/build/jasmineUtils')
  2. since the extra assertion only affects the first test, is there a way to reset the done assertions?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/timkindberg/jest-when/issues/88#issuecomment-937582598, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABJA3WVOPQ5CWDD7RN42RDUFVMXXANCNFSM5FOIHRWQ .

-- Thanks,

Tim Kindberg

timkindberg commented 3 years ago

Fixed in v3.4.2 (thanks for the suggestion it helped me fix it)

voda commented 2 years ago

Thanks for the fix, I can confirm it works and I haven't found any more issues with the new version and our project.