storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
84.62k stars 9.31k forks source link

Mocking in storyshots #962

Closed tehandyb closed 7 years ago

tehandyb commented 7 years ago

Is it possible to Mock components when using storyshots? I can't see any documentation to do it.

evenchange4 commented 7 years ago

Yes, it is possible. I mock several components for storyshots with Jest config:

"setupFiles": [
  "<rootDir>/.storybook/storyshots-setup.js"
],
// .storybook/storyshots-setup.js"
jest.mock('react-motion-ui-pack');
...

Reference: https://github.com/MCS-Lite/mcs-lite/blob/master/packages/mcs-lite-ui/package.json#L30

tehandyb commented 7 years ago

Awesome, thanks!

horacio10x commented 3 years ago

If you want to mock for the storyshots only you can put your mock above initStoryshots()

jest.mock('../someMock', () =>
  () => `mock`
);
initStoryshots();
dheerajk02 commented 2 years ago

If you want to mock for the storyshots only you can put your mock above initStoryshots()

jest.mock('../someMock', () =>
  () => `mock`
);
initStoryshots();

@horacio10x @evenchange4 I get the following error if I try to jest mock right before the initStoryshots(); Do not import @jest/globals outside of the Jest test environment

// @ts-nocheck
import initStoryshots, { multiSnapshotWithOptions } from "@storybook/addon-storyshots";
import ReactDOM from "react-dom";
import path from "path";
jest.mock("@storybook/core/server", () => ({
    toRequireContext: require("@storybook/core/dist/server/preview/to-require-context").toRequireContext,
}));
ReactDOM.createPortal = node => node;
initStoryshots({
    configPath: path.resolve(__dirname, "../../.storybook"),
    test: multiSnapshotWithOptions({}),
});

@akanksha-swiggy