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] Context leak when using msw with multiple stories in the same file #244

Open
sneko opened this issue Jan 18, 2023 · 0 comments
Open
Labels
bug Something isn't working

Comments

@sneko
Copy link

sneko commented Jan 18, 2023

Describe the bug

Let's say in the same file I have StoryA and StoryB both using msw to mock API calls. They use the same underlying component that fetches data.

I was doing some advanced mocking with msw to delay some requests but I figured out the initial call from StoryA was leaking over StoryB handlers (I was able to differentiate them because I forced them using different inputs for their requests).

In my example I wanted on StoryB to count the number of requests done to delay the response differently, it appears instead of receiving 2 calls as expected, I received 3 (the extra one was from StoryA and was the first received).

I spent hours trying to debug everything, trying to use manually msw.resetHandlers() everywhere where I could but it didn't change anything, trying to set maxWorkers=1...

Maybe it's related to my setup but I found a workaround, in the test runner hook preRender(), just waiting for the context to be ready remove all the leakage. Just using inside /.storybook/test-runner.js:

const { getStoryContext } = require('@storybook/test-runner');

module.exports = {
  setup() {},
  async preRender(page, context) {
    // This is the workaround
    await getStoryContext(page, context);
  },
  async postRender(page, context) {},
};

While debugging I ended using headless: false to see what happened, it seems between each .stories.jsx the browser for testing is totally relaunched, but for multiple stories inside the same .stories.jsx the browser is reused. Maybe something is going too fast? Or in a sense the parameters are merged and I end up with the same msw handlers for both the stories?

(sorry for not providing a reproductible repository)

Hope it helps 👍

@sneko sneko added the bug Something isn't working label Jan 18, 2023
@sneko sneko changed the title [Bug] Context leak when using [Bug] Context leak when using msw with multiple stories in the same file Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant