-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28255 from storybookjs/beforeAll-hook
Core: Add support for `beforeAll` hook
- Loading branch information
Showing
15 changed files
with
310 additions
and
26 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
code/core/src/preview-api/modules/store/csf/beforeAll.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
import { composeBeforeAllHooks } from './beforeAll'; | ||
|
||
const calls: string[] = []; | ||
|
||
beforeEach(() => { | ||
calls.length = 0; | ||
}); | ||
|
||
const basicHook = (label: string) => () => { | ||
calls.push(label); | ||
}; | ||
const asyncHook = (label: string, delay: number) => async () => { | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
calls.push(label); | ||
}; | ||
const cleanupHook = (label: string) => () => { | ||
calls.push(label); | ||
return () => { | ||
calls.push(label + ' cleanup'); | ||
}; | ||
}; | ||
const asyncCleanupHook = (label: string, delay: number) => () => { | ||
calls.push(label); | ||
return async () => { | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
calls.push(label + ' cleanup'); | ||
}; | ||
}; | ||
|
||
describe('composeBeforeAllHooks', () => { | ||
it('should return a composed hook function', async () => { | ||
await composeBeforeAllHooks([basicHook('one'), basicHook('two'), basicHook('three')])(); | ||
expect(calls).toEqual(['one', 'two', 'three']); | ||
}); | ||
|
||
it('should execute cleanups in reverse order', async () => { | ||
const cleanup = await composeBeforeAllHooks([ | ||
cleanupHook('one'), | ||
cleanupHook('two'), | ||
cleanupHook('three'), | ||
])(); | ||
expect(calls).toEqual(['one', 'two', 'three']); | ||
|
||
await cleanup?.(); | ||
expect(calls).toEqual(['one', 'two', 'three', 'three cleanup', 'two cleanup', 'one cleanup']); | ||
}); | ||
|
||
it('should execute async hooks in sequence', async () => { | ||
await composeBeforeAllHooks([ | ||
asyncHook('one', 10), | ||
asyncHook('two', 100), | ||
asyncHook('three', 10), | ||
])(); | ||
expect(calls).toEqual(['one', 'two', 'three']); | ||
}); | ||
|
||
it('should execute async cleanups in reverse order', async () => { | ||
const hooks = [ | ||
asyncCleanupHook('one', 10), | ||
asyncCleanupHook('two', 100), | ||
asyncCleanupHook('three', 10), | ||
]; | ||
|
||
const cleanup = await composeBeforeAllHooks(hooks)(); | ||
expect(calls).toEqual(['one', 'two', 'three']); | ||
|
||
await cleanup?.(); | ||
expect(calls).toEqual(['one', 'two', 'three', 'three cleanup', 'two cleanup', 'one cleanup']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { type BeforeAll, type CleanupCallback } from '@storybook/csf'; | ||
|
||
// Execute all the hooks in sequence, and return a function that will execute cleanups in reverse order | ||
export const composeBeforeAllHooks = (hooks: BeforeAll[]): BeforeAll => { | ||
return async () => { | ||
const cleanups: CleanupCallback[] = []; | ||
for (const hook of hooks) { | ||
const cleanup = await hook(); | ||
if (cleanup) cleanups.unshift(cleanup); | ||
} | ||
return async () => { | ||
for (const cleanup of cleanups) { | ||
await cleanup(); | ||
} | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.