You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There seems to be a slight misinterpretation of what the test-runner does.
The Storybook test-runner spawns a browser using Playwright, then accesses a running Storybook, and then visits every story to check whether they render correctly.
Because your Storybook is already running, the test-runner is more of a "visitor" than anything. That means, whatever you need to mock, in the context of your Storybook stories, should be done in your Storybook.
There are different mechanisms to mock things in Storybook, and what you're trying to achieve should be possible with the @storybook/jest package like so:
@HinataKah0 I'm mentioning you as well, this information might be useful to you. If your project needs mocking on another level, you might need module level mocking with Webpack/Vite.
Describe the bug
I am unable to mock (for example)
Math.random()
.Previously with storyshots, I had this line in my
storyshots.test.js
file:which ensured consistent randomization for snapshot tests. Other mocks were also set up in this file.
With the new test-runner, I have tried:
.storybook/test-runner.js
filesetup()
hookmodule.exports
setupFiles
in myjest.config.js
global.Math
without using jestsetup()
hook andtest-runner.js
rootglobalThis
instead ofglobal
None of these solutions have worked.
Steps to reproduce the behavior
FontAwesomeIcon
with atitle
prop, as discussed here.const TestComponent = () => <p>{Math.random()}</p>
.I am using the recipe from the docs of this project.
Expected behavior
Should be able to use jest mocks.
If
Math.random()
was successfully mocked, the snapshots would be consistent.Screenshots and/or logs
Snapshot failure example:
Environment
7.0.20
with Vite and React18.16.0
9.5.1
Additional context
If there is a correct way to do this that I've missed, it should be called out in the project docs.
The text was updated successfully, but these errors were encountered: