-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addon-interactions: Mock window in browser environments #17535
Conversation
☁️ Nx Cloud ReportCI ran the following commands for commit 5952b57. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this branch ✅ Successfully ran 1 targetSent with 💌 from NxCloud. |
I spent a few hours trying to get E2E tests running locally, and I have to give up now. :(. I'm running
during the I can copy/paste the |
e2e test is now passing. :success: |
Hah, looks like jest took care of this in their latest alpha too. jestjs/jest#12447 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Great stuff @IanVS
import { FORCE_REMOUNT, STORY_RENDER_PHASE_CHANGED } from '@storybook/core-events'; | ||
import { AnyFramework, ArgsEnhancer } from '@storybook/csf'; | ||
import { instrument } from '@storybook/instrumenter'; | ||
import { ModuleMocker } from 'jest-mock'; | ||
|
||
const { fn } = new ModuleMocker(globalWindow); | ||
const JestMock = new ModuleMocker(global); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, I screwed this up, it needed to be globalThis
. I'll need to send another PR to fix this.
Issue: #17516
What I did
Importing
fn
directly fromjest-mock
does not work in a browser, since jest expects to run in node and mocks outglobal
instead ofwindow
(https://github.com/facebook/jest/blob/465518a2fa17ce45f7e3280dd0f54351273432c4/packages/jest-mock/src/index.ts#L1200-L1202). This breaks in vite, which does not polyfillglobal
like webpack does.The change here was to use the
global
package, as is used elsewhere in storybook, to determine which global to mock (window
in browsers,global
in node).How to test
I've tested this by building, copying
dist
into my project, and verifying that the interactions addon works correctly, whereas it crashes if I just install the addon from npm.