jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.22k stars 507 forks source link

`expect(jest.mock()).toHaveBeenCalledTimes` retains state of previous calls #1007

Closed denistsoi closed 3 years ago

denistsoi commented 3 years ago

Sorry if I've missed something but test fails when importing jest test into tsdx repo.

Current Behavior

I'm migrating a component into a project that uses tsdx.

The jest test above passes in the original repo. git-actions

👇 (attached screenshot)

Screenshot 2021-04-10 at 4 37 15 PM
    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 4

      38 |
      39 |     await screen.findByAltText('QR Code');
    > 40 |     expect(QrCode.toDataURL).toHaveBeenCalledTimes(1);
         |                              ^
      41 |   });
      42 |
      43 |   it('calls QrCode.toDataUrl with default properties', async () => {

Now if I move this test to the top (as the first expect, the test passes).

Expected behavior

The following should pass regardless of where this is included in a it closure.

expect(jest.mock()).toHaveBeenCalledTimes(1);

Suggested solution(s)

Not sure -

Additional context

Your environment

  System:
    OS: macOS 11.0.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 615.20 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.15.5 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.11 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 89.1.22.71
    Chrome: 89.0.4389.114
    Safari: 14.0.1
  npmPackages:
    tsdx: ^0.14.1 => 0.14.1
    typescript: ^4.1.3 => 4.1.3
  npmGlobalPackages:
    typescript: 3.9.5
agilgur5 commented 3 years ago

Sorry if I've missed something but test fails when importing jest test into tsdx repo.

It looks like the repo you're moving it from is on a newer version of Jest than TSDX, and that's likely why there's some difference. TSDX also uses ts-jest by default. I would suggest checking such whether such things as versions and configs are equivalent in a migration before filing an issue, as those are often responsible for inconsistencies.

TSDX has a tiny wrapper around Jest merely to set default config (and some arg processing due to inability to use import Jest and execute it); there are no modifications to Jest internals, hence no real reason why TSDX could be responsible for such an issue.