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

[Bug]: Explicit action mocks are not always reset between tests in test runner (race condition) #26501

Closed
bodograumann opened this issue Mar 14, 2024 · 6 comments

Comments

@bodograumann
Copy link
Contributor

bodograumann commented Mar 14, 2024

Describe the bug

In storybook 7 we could use argTypes: { onClick: true } on meta to register the event as an action and have it automatically mocked in play functions.
Now in storybook 8 we instead use args: { onClick: fn() }.

In our project we now had the problem, that the mocks are not cleared between tests and so negative assertions on events were failing.
It took me a while to create a minimal reproduction, because in a naive setup this does not occur right away.
Turns out it is some kind of race condition. Sometimes it works, sometimes it does not. I had to preload fn in preset.js to cause the race condition in the reproduction + run the tests on a built storybook instead of a live server.

To Reproduce

  1. git clone https://github.com/bodograumann/storybook-8-fn-clear
  2. cd storybook-8-fn-clear
  3. yarn
  4. npx storybook build --test
  5. npx http-server storybook-static -a 127.0.0.1 --port 6005
  6. In a new terminal: npx test-storybook --url http://localhost:6005
  7. Retry 6. a few times. It fails most of the time (~89/100) and sometimes succeeds.

System

npx storybook@latest info

Storybook Environment Info:
(node:44360) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

  System:
    OS: Linux 6.7 Arch Linux
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 21.7.0 - /usr/bin/node
    Yarn: 1.22.21 - /usr/bin/yarn
    npm: 10.5.0 - /usr/bin/npm <----- active
    pnpm: 8.15.2 - /usr/bin/pnpm
  npmPackages:
    @storybook/addon-designs: ~7.0.9 => 7.0.9
    @storybook/addon-essentials: ^8.0.0 => 8.0.0
    @storybook/addon-interactions: ^8.0.0 => 8.0.0
    @storybook/blocks: ^8.0.0 => 8.0.0
    @storybook/mdx2-csf: ^1.1.0 => 1.1.0
    @storybook/test: ^8.0.0 => 8.0.0
    @storybook/test-runner: ^0.17.0 => 0.17.0
    @storybook/vue3: ^8.0.0 => 8.0.0
    @storybook/vue3-vite: ^8.0.0 => 8.0.0
    chromatic: ^11.0.0 => 11.0.8
    eslint-plugin-storybook: ^0.8.0 => 0.8.0
    storybook: ^8.0.0 => 8.0.0
  npmGlobalPackages:
    @storybook/cli: 7.6.8

Additional context

Original discussion, where clearing the mocks was discussed: #23649

Not sure how hard this is to solve, but I'll try and work around the issue by manually clearing the mocks, where tests are currently failing. Of course actually failing tests might be obscured by this bug, because I cannot manually clear the mocks in every play function.

@kasperpeulen
Copy link
Contributor

Really weird, this seems to solve it:

import { fn, restoreAllMocks } from "@storybook/test";

const _dummy = fn();
_dummy.mockClear();

export default {
  loaders: [
    () => {
      restoreAllMocks();
    },
  ],
};

But that is exactly what we do by default. Investigating.

@kasperpeulen
Copy link
Contributor

Ah we have already solved this in 8.1.0-alpha.1. Let's release 8.0.1 soon @vanessayuenn

@vanessayuenn
Copy link
Contributor

Thanks @kasperpeulen! I will close this issue for now then. @bodograumann please feel free to reopen if this issue persists in 8.1-alpha. This fix should also be patched to 8.0.1 soon (probably next week) -- stay tuned!

@bodograumann
Copy link
Contributor Author

v8.0.1 has been released, but I cannot find a fix related to this issue in the changelog, @vanessayuenn.
Was it included and it's just not obvious, or was in not backported yet?

@bodograumann
Copy link
Contributor Author

I tried installing 8.0.1 and afaict the issue is gone. So thanks everyone! <3

@kasperpeulen
Copy link
Contributor

@bodograumann The issue was that we were installing multiple version of storybook/test, which is fixed in:
#26411

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

No branches or pull requests

3 participants