-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
HMR: Fix race conditions causing stale play functions to fire on re-rendered stories #33930
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -644,20 +644,52 @@ describe('registerIndexJsonRoute', () => { | |
| onChange(`${workingDir}/src/nested/Button.stories.ts`); | ||
| onChange(`${workingDir}/src/nested/Button.stories.ts`); | ||
|
|
||
| // Wait for first batch to be processed and emit (leading edge) | ||
| // With trailing-only debounce, the first emit only fires after the debounce period | ||
| await vi.waitFor(() => { | ||
| expect(mockServerChannel.emit).toHaveBeenCalledTimes(1); | ||
| }); | ||
| expect(mockServerChannel.emit).toHaveBeenCalledWith(STORY_INDEX_INVALIDATED); | ||
|
|
||
| // Fire another change event after the first batch is processed | ||
| // This will trigger the trailing edge of the debounce | ||
| // This will trigger the trailing edge of the debounce again | ||
| onChange(`${workingDir}/src/nested/Button.stories.ts`); | ||
|
|
||
| // Wait for trailing debounce to trigger second emit | ||
| // Wait for the trailing debounce to trigger a second emit | ||
| await vi.waitFor(() => { | ||
| expect(mockServerChannel.emit).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| it('only emits once per file change (no double-fire from leading+trailing edges)', async () => { | ||
| vi.mocked(debounce).mockImplementation( | ||
| (await vi.importActual<typeof import('es-toolkit/function')>('es-toolkit/function')) | ||
| .debounce | ||
| ); | ||
|
|
||
| const mockServerChannel = { emit: vi.fn() } as any as ServerChannel; | ||
| registerIndexJsonRoute({ | ||
| app, | ||
| channel: mockServerChannel, | ||
| workingDir, | ||
| normalizedStories, | ||
| storyIndexGeneratorPromise: getStoryIndexGeneratorPromise(), | ||
| }); | ||
|
|
||
| const watcher = Watchpack.mock.instances[0]; | ||
| const onChange = watcher.on.mock.calls[0][1]; | ||
|
|
||
| // Fire a single change event | ||
| onChange(`${workingDir}/src/nested/Button.stories.ts`); | ||
|
|
||
| // Wait for the trailing debounce to fire | ||
| await vi.waitFor(() => { | ||
| expect(mockServerChannel.emit).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| // Ensure it was only called once (no double-fire from both leading and trailing edges) | ||
| await new Promise((resolve) => setTimeout(resolve, 200)); | ||
| expect(mockServerChannel.emit).toHaveBeenCalledTimes(1); | ||
|
Comment on lines
+663
to
+691
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# 1. Check the exact code at lines 663-691
echo "=== Code at lines 663-691 ==="
sed -n '663,691p' code/core/src/core-server/utils/index-json.test.ts
echo -e "\n=== DEBOUNCE constant definition ==="
rg -n "const DEBOUNCE" code/core/src/core-server/utils/index-json.test.tsRepository: storybookjs/storybook Length of output: 1263 🏁 Script executed: #!/bin/bash
# 2. Check for other mockImplementation calls in the test file
echo "=== All mockImplementation calls ==="
rg -n "mockImplementation\(" code/core/src/core-server/utils/index-json.test.tsRepository: storybookjs/storybook Length of output: 267 🏁 Script executed: #!/bin/bash
# 3. Check the structure of beforeEach blocks in the file
echo "=== beforeEach blocks structure ==="
rg -n "beforeEach" code/core/src/core-server/utils/index-json.test.ts -A 5Repository: storybookjs/storybook Length of output: 722 🏁 Script executed: #!/bin/bash
# Search for DEBOUNCE more broadly (could be imported)
echo "=== Search for DEBOUNCE usage ==="
rg -n "DEBOUNCE" code/core/src/core-server/utils/index-json.test.ts
echo -e "\n=== Also check the main implementation file for debounce timing ==="
rg -n "DEBOUNCE|debounce.*\d+" code/core/src/core-server/utils/index-json.ts | head -20Repository: storybookjs/storybook Length of output: 362 🏁 Script executed: #!/bin/bash
# Check context at line 605 for mockImplementation
echo "=== Context around line 605 ==="
sed -n '600,620p' code/core/src/core-server/utils/index-json.test.tsRepository: storybookjs/storybook Length of output: 790 Move mock setup to beforeEach and derive debounce wait time from DEBOUNCE constant. Lines 664–667 set mock behavior inside the test body, violating Vitest guidelines to implement mock behaviors in Suggested adjustment- it('only emits once per file change (no double-fire from leading+trailing edges)', async () => {
- vi.mocked(debounce).mockImplementation(
- (await vi.importActual<typeof import('es-toolkit/function')>('es-toolkit/function'))
- .debounce
- );
+ describe('single-emission behavior', () => {
+ beforeEach(async () => {
+ vi.mocked(debounce).mockImplementation(
+ (await vi.importActual<typeof import('es-toolkit/function')>('es-toolkit/function'))
+ .debounce
+ );
+ });
+
+ it('only emits once per file change (no double-fire from leading+trailing edges)', async () => {
// ...
- await new Promise((resolve) => setTimeout(resolve, 200));
+ await new Promise((resolve) => setTimeout(resolve, DEBOUNCE * 2));
expect(mockServerChannel.emit).toHaveBeenCalledTimes(1);
+ });
+ });🤖 Prompt for AI Agents |
||
| expect(mockServerChannel.emit).toHaveBeenCalledWith(STORY_INDEX_INVALIDATED); | ||
| }); | ||
| }); | ||
| }); | ||
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.
This looks broken, since it's not a template string but a regular string.