Giter Club home page Giter Club logo

Comments (7)

conradogarciaberrotaran avatar conradogarciaberrotaran commented on June 12, 2024 1

Got it working, instead of using mswLoader, I'm now using mswDecorator. Not sure why.

from msw-storybook-addon.

yannbf avatar yannbf commented on June 12, 2024 1

That's great! Thank you for reporting back. I will be closing this issue, let me know if this happens again.

from msw-storybook-addon.

conradogarciaberrotaran avatar conradogarciaberrotaran commented on June 12, 2024 1

Thank you!

from msw-storybook-addon.

conradogarciaberrotaran avatar conradogarciaberrotaran commented on June 12, 2024

I'm having issues too. made it work with this workaround: #119 (comment)
But I can't define handlers for each Story.
Did you have any luck?

from msw-storybook-addon.

yannbf avatar yannbf commented on June 12, 2024

Hey @conradogarciaberrotaran and @Mexflubber thanks for reporting this.
The mswLoader is the recommended way to use the addon because it avoids possible flakiness, as the decorator only runs as the story is rendering, while the loader runs before the story renders. But the mswLoader waits for the msw promise to resolve, and it seems like it never resolves for you. Could you please share a reproduction? That will make it much easier for me to understand what's going on and to provide a fix for it.

from msw-storybook-addon.

conradogarciaberrotaran avatar conradogarciaberrotaran commented on June 12, 2024

Thank you @yannbf , I've just tried to replicate the issue, basically I ran:

npx create-next-app@latest msw-storybook-issue --use-npm
npx storybook@latest init
npm i msw msw-storybook-addon -D
npx msw init public/

Added the public route of my nextjs to the staticDirs in main.js and added this story:

import { rest } from 'msw';
import { useEffect, useState } from 'react';

const SomeComponent = () => {
  const [response, setResponse] = useState(null);
  useEffect(() => {
    fetch("/api/some-endpoint")
      .then((response) => response.json())
      .then((data) => setResponse(data));
  }, []);

  return <div>{response?.firstName}</div>;
};


export default {
  title: 'Example/SomeComponent',
  component: SomeComponent,
};

export const Something = {
  parameters: {
    msw: {
      handlers: [
        rest.get('/api/some-endpoint', (req, res, ctx) => {
          return res(
            ctx.json({
              firstName: 'Neil',
              lastName: 'Maverick',
            })
          )
        }),
      ]
    },
  }
};

export const SomethingElse = {
  parameters: {
    msw: {
      handlers: [
        rest.get('/api/some-endpoint', (req, res, ctx) => {
          return res(
            ctx.json({
              firstName: 'bob',
              lastName: 'Maverick',
            })
          )
        }),
      ]
    },
  }
};

And it's working correctly.
I'm not able to reproduce it. I will try updating the versions on my project to see if the issue is fixed.

from msw-storybook-addon.

conradogarciaberrotaran avatar conradogarciaberrotaran commented on June 12, 2024

Updating the libraries fixed my issue, I updated msw, storybook and msw-storybook-addon to latest

from msw-storybook-addon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.