-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Test: Add mock capabilities #31987
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
Test: Add mock capabilities #31987
Conversation
|
View your CI Pipeline Execution ↗ for commit 7f7556c
☁️ Nx Cloud last updated this comment at |
c3f853f to
74b1ea4
Compare
|
Failed to publish canary version of this pull request, triggered by @valentinpalkovic. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/16170019992 |
|
Failed to publish canary version of this pull request, triggered by @valentinpalkovic. See the failed workflow run at: https://github.com/storybookjs/storybook/actions/runs/16170625921 |
Package BenchmarksCommit: The following packages have significant changes to their size or dependencies:
|
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 51 | 56 | 🚨 +5 🚨 |
| Self size | 31.77 MB | 41.35 MB | 🚨 +9.58 MB 🚨 |
| Dependency size | 17.43 MB | 18.20 MB | 🚨 +762 KB 🚨 |
| Bundle Size Analyzer | Link | Link |
@storybook/vue3-vite
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 104 | 105 | 🚨 +1 🚨 |
| Self size | 34 KB | 34 KB | 0 B |
| Dependency size | 42.73 MB | 42.78 MB | 🚨 +51 KB 🚨 |
| Bundle Size Analyzer | Link | Link |
sb
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 52 | 57 | 🚨 +5 🚨 |
| Self size | 1 KB | 1 KB | 0 B |
| Dependency size | 49.20 MB | 59.54 MB | 🚨 +10.34 MB 🚨 |
| Bundle Size Analyzer | Link | Link |
@storybook/cli
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 217 | 221 | 🚨 +4 🚨 |
| Self size | 582 KB | 584 KB | 🚨 +2 KB 🚨 |
| Dependency size | 93.68 MB | 103.94 MB | 🚨 +10.25 MB 🚨 |
| Bundle Size Analyzer | Link | Link |
@storybook/codemod
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 186 | 190 | 🚨 +4 🚨 |
| Self size | 31 KB | 31 KB | 0 B |
| Dependency size | 77.77 MB | 88.03 MB | 🚨 +10.25 MB 🚨 |
| Bundle Size Analyzer | Link | Link |
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.
30 files reviewed, 28 comments
Edit PR Review Bot Settings | Greptile
code/core/src/core-server/presets/vitePlugins/vite-mock-build-manifest/plugin.ts
Outdated
Show resolved
Hide resolved
9b31466 to
5b4f063
Compare
|
@valentinpalkovic please add a manual testing section, so we can do QA |
Closes #31856
What I did
This PR introduces powerful module mocking capabilities directly into Storybook, powered by Vitest's core mocking capabilities. Users can now mock modules, including node modules, local files, and even spy on module functions, with a simple and declarative API. The mocking works seamlessly in both dev mode and production builds, ensuring consistent behavior across all environments.
A new
sb.mock()API is exposed and can be used within the .storybook/preview.js file to define global mocks that apply to all stories.Example Stories
To showcase and test these new capabilities, several new stories have been added under the test category:
playfunctions.__mocks__file automatically replaces a module.lodash-esboth via__mocks__and automocking.🛠️ Implementation Details
The solution leverages @vitest/mocker's core logic but adapts it to Storybook's architecture, particularly to support static builds where a dev server is not available. This is achieved through two new custom Vite plugins.
Dev Mode
In dev mode, mocking relies on Vite's module graph invalidation. When a mock is added, changed, or removed (either in
preview.jsor the__mocks__directory), the plugin intelligently invalidates all affected modules and triggers a hot reload. This provides a fast and interactive development experience.Dev and Build Mode
viteMockPlugin, scans.storybook/preview.jsfor allsb.mock()calls during the build process.__mocks__Redirects: If a corresponding file is found in the top-level__mocks__directory, that file is loaded and transformed by Vite.__mocks__file is found, the original module's code is transformed at build-time to replace its exports with mocks or spies.🚀 New sb.mock() API
You can now mock any module by calling
sb.mock()from your .storybook/preview.js (or .ts) file.Automocking a Local Module
If a file exists in the top-level
__mocks__directory, it will automatically be used. Otherwise, the module will be automocked, replacing all its exports with mock functions.Spying on a Module
To keep the original implementation but spy on its function calls, use the `{ spy: true } option. This is useful for asserting that a function was called without altering its behavior.
Mocking Node Modules
You can mock node modules in the same way, including deep imports.
Comparison to Vitest Mocking
While this feature uses Vitest's mocking engine, the implementation within Storybook has some key differences:
.storybook/preview.js. Unlike Vitest, you cannot callsb.mock()inside individual story files.sb.unmock()or equivalent, as the module graph is fixed in a production build.beforeEachhook (e.g.,mocked(myFunction).mockReturnValue('new value')).sb.mock('path', () => ({...}))). This is because all mocking decisions are resolved at build time, whereas factories are executed at runtime.Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.tsMake sure this PR contains one of the labels below:
Available labels
bug: Internal changes that fixes incorrect behavior.maintenance: User-facing maintenance tasks.dependencies: Upgrading (sometimes downgrading) dependencies.build: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup: Minor cleanup style change. Will not show up in release changelog.documentation: Documentation only changes. Will not show up in release changelog.feature request: Introducing a new feature.BREAKING CHANGE: Changes that break compatibility in some way with current major version.other: Changes that don't fit in the above categories.🦋 Canary release
This pull request has been released as version
0.0.0-pr-31987-sha-d358bfc5. Try it out in a new sandbox by runningnpx [email protected] sandboxor in an existing project withnpx [email protected] upgrade.More information
0.0.0-pr-31987-sha-d358bfc5valentin/storybook-mockd358bfc51752839003)To request a new release of this pull request, mention the
@storybookjs/coreteam.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=31987Greptile Summary
Introduces powerful module mocking capabilities to Storybook powered by Vitest's core mocking engine, enabling seamless mocking of both local and node modules during development and production builds.
sb.mock()API incode/core/src/test/index.tsfor declarative module mocking configuration in preview filescode/core/src/core-server/presets/vitePluginsfor build-time mock analysis and runtime injectioncode/core/src/test/storiesdemonstrating auto-mocking, spy-mocking, and node module mocking featurescode/core/templatesfor injecting mocking runtime code into builds__mocks__for overriding module implementations