Vite + Storybook 6.3 + Jest: "global is not defined"
See original GitHub issueDescribe the bug
I am trying to get Vite + Storybook 6.3 working with some existing stories. These stories use jest.fn()
as mocks. I previously added this in preview.js
to get jest.fn()
working within storybook:
import jest from "jest-mock";
window.jest = jest;
But when using the Vite integration I am now getting an error when running storybook:
Uncaught ReferenceError: global is not defined
at ../node_modules/jest-mock/build/index.js (index.js:948)
at __require2 (chunk-IHTDASF6.js?v=3b66b68d:17)
at dep:jest-mock:1
To Reproduce
I am trying vite
with storybook 6.3 and npm 7.19 workspaces. My repro repo is here: https://github.com/adiun/vite-monorepo:
- Ensure you have
npm 7.19
installed for the workspaces functionality - In the root, run
npm i
- In the
app
folder, runnpm run storybook
. - In the browser console for storybook you will see:
Uncaught ReferenceError: global is not defined
at ../node_modules/jest-mock/build/index.js (index.js:948)
at __require2 (chunk-IHTDASF6.js?v=3b66b68d:17)
at dep:jest-mock:1
System
Environment Info:
System:
OS: macOS 11.3
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Binaries:
Node: 14.16.0 - /usr/local/bin/node
npm: 7.19.0 - /usr/local/bin/npm
Browsers:
Chrome: 91.0.4472.114
Edge: 91.0.864.48
Firefox: 88.0.1
Safari: 14.1
npmPackages:
@storybook/addon-docs: 6.3.0 => 6.3.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:15 (7 by maintainers)
Top Results From Across the Web
jest referenceerror: global is not defined - You.com | The AI ...
To Reproduce I am trying vite with storybook 6.3 and npm 7.19 workspaces. My repro repo is here: https://github.com/adiun/vite-monorepo: Ensure you have npm ......
Read more >Jest: ReferenceError: global is not defined - Stack Overflow
So I am writing unit test using "react-testing-library" on Jest and I have this error:
Read more >@storybook/builder-vite - npm
A plugin to run and build Storybooks with Vite. Latest version: 0.2.6, last published: 9 days ago. Start using @storybook/builder-vite in ...
Read more >Storybook for Vite
In Storybook 6.3, I'm excited to announce the Storybook Vite builder, a community-led project to support one of the hottest build tools on ......
Read more >@storybook/addon-jest | Yarn - Package Manager
React storybook addon that show component jest report. addon, jest, react, report ... Storybook Jest Addon Demo ... Ember: Remove global Ember usage...
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
Following up on this… it’s actually a bad idea to redefine global like this in
main.js
:The Vite docs even mention this.
The problem is that when running a
build-storybook
, any references toglobal
in libraries like@storybook/client-logger
will be rewritten towindow
and it will break since it’s running innode
.So I removed this code in
main.js
to getbuild-storybook
working and added this hack to.storybook/preview-head.html
to getstart-storybook
working:main.js:
preview.js:
preview-head.html:
Alright, I finally fixed this…
jest-mock
doesn’t explicitly provide a default export so in storybook’spreview.js
this is what I have:This combined with assigning
global
towindow
inmain.js
fixed this issue.preview.js
main.js
Updated my repo too