Interactions-addon relies on polyfilled `global` (via jest-mock)
See original GitHub issueDescribe the bug
createed a vite / react app - added storybook:
npx sb@next init --builder storybook-builder-vite
Got an error saying I needed to add:
yarn add -D @mdx-js/preact
Finally ran:
yarn storybook
Got:
{
"message":"Uncaught SyntaxError: The requested module '/node_modules/@storybook/addon-interactions/node_modules/jest-mock/build/index.js?v=bd021136' does not provide an export named 'fn'",
"source":"http://localhost:6006/node_modules/@storybook/addon-interactions/dist/esm/preset/argsEnhancers.js",
"line":28,
"column":10,
"error":{
"message":"The requested module '/node_modules/@storybook/addon-interactions/node_modules/jest-mock/build/index.js?v=bd021136' does not provide an export named 'fn'",
"name":"SyntaxError",
"stack":"SyntaxError: The requested module '/node_modules/@storybook/addon-interactions/node_modules/jest-mock/build/index.js?v=bd021136' does not provide an export named 'fn'"
},
"origin":"preview"
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:15
- Comments:18 (11 by maintainers)
Top Results From Across the Web
Interactions Addon | Storybook: Frontend workshop for UI ...
Interactions relies on "instrumented" versions of Jest and Testing Library, that you import from @storybook/jest and @storybook/testing-library instead of ...
Read more >@storybook/addon-jest | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >Vite + Storybook 6.3 + Jest: "global is not defined" - Issuehunt
Describe the bug I am trying to get Vite + Storybook 6.3 working with some existing stories. These stories use jest.fn() as mocks....
Read more >How can I mock Webpack's require.context in Jest?
I had the same problem, then I've made a 'solution'. I'm pretty sure that this is not the best choice. I ended up...
Read more >the requested module 'react' does not provide an export ...
storybookjs/storybookInteractions-addon relies on polyfilled `global` (via jest-mock)#17516. Created 10 months ago. 18. Describe the bug.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK, thanks for the reproduction. I’ve figured out what’s going on. Even with the fixes I’ve attempted, jest-mock still internally tries to run
ModuleMocker(global)
which fails whenglobal
is not defined. In jest version 28.0.0-alpha.4, they’ve made a fix to useglobalThis
instead, which does run in the browser, but that version is not compatible with the node versions that storybook supports. I’ve asked if they can release a version of 27 with that change. In the meantime, you can create apreview-head.html
with the contents of:It’s not ideal, and we need to find a fix for new users of storybook + vite, but I think this will at least get you up and running for now.
Hi, we have a report of this in the storybook-builder as well (https://github.com/eirslett/storybook-builder-vite/issues/242), although I think in the end, this is a problem either with jest, or with the way that addon-interactions is using jest-mock.
When I’ve needed to use jest-mock in my own project (which does not use addon-interactions), I can make it work by using:
I think the issue is that the addon-interactions imports
fn
directly from jest-mock: https://github.com/storybookjs/storybook/blob/f979a5cff227cd0e7fff495f23a198adb7eb656e/addons/interactions/src/preset/argsEnhancers.ts#L5Which breaks in non-node or at least non-node-pollyfilled environments, because it uses
global
instead ofwindow
: https://github.com/facebook/jest/blob/465518a2fa17ce45f7e3280dd0f54351273432c4/packages/jest-mock/src/index.ts#L1200-L1202CC @yannbf, would you consider a PR to change the way interactions-addon is importing jest-mock? I’ve seen this come up about half a dozen times in the last few days, now.