-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Test: Refactor .test prototype #32314
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
Merged
yannbf
merged 9 commits into
yann/test-fn-prototype
from
kasper/refactor-test-prototype
Aug 21, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9719ea6
Refactor test prototype
kasperpeulen 82b84e4
Improve a bit
kasperpeulen eca1ace
fix unit tests
yannbf ee34e40
fix chromatic failures
yannbf 6ab99d4
Fix override in portable stories
kasperpeulen 024cde6
Merge remote-tracking branch 'origin/kasper/refactor-test-prototype' …
kasperpeulen 7def7d9
add fixes for portable stories and prepareStory
yannbf 3ab5db3
Improved doTest type
kasperpeulen 8e74582
Improve test execution in StoryRender
kasperpeulen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -154,6 +154,7 @@ export interface Story< | |||||
| annotations: StoryAnnotations<TRenderer, TRenderer['args']>, | ||||||
| fn: TestFunction<TRenderer> | ||||||
| ): void; | ||||||
| getAllTests(): Record<string, { story: Story<TRenderer>; test: TestFunction<TRenderer> }>; | ||||||
| } | ||||||
|
|
||||||
| export function isStory<TRenderer extends Renderer>(input: unknown): input is Story<TRenderer> { | ||||||
|
|
@@ -165,7 +166,6 @@ function defineStory< | |||||
| TInput extends StoryAnnotations<TRenderer, TRenderer['args']>, | ||||||
| >(input: TInput, meta: Meta<TRenderer>): Story<TRenderer, TInput> { | ||||||
| let composed: ComposedStoryFn<TRenderer>; | ||||||
| input.__tests ??= {}; | ||||||
| const compose = () => { | ||||||
| if (!composed) { | ||||||
| composed = composeStory( | ||||||
|
|
@@ -177,6 +177,9 @@ function defineStory< | |||||
| } | ||||||
| return composed; | ||||||
| }; | ||||||
|
|
||||||
| const tests: Record<string, { story: Story<TRenderer>; test: TestFunction<TRenderer> }> = {}; | ||||||
|
|
||||||
| return { | ||||||
| _tag: 'Story', | ||||||
| input, | ||||||
|
|
@@ -191,27 +194,22 @@ function defineStory< | |||||
| get play() { | ||||||
| return input.play ?? meta.input?.play ?? (async () => {}); | ||||||
| }, | ||||||
| get run() { | ||||||
| return compose().run ?? (async () => {}); | ||||||
| async run(context, testName?: string) { | ||||||
| const composedRun = compose().run; | ||||||
| await composedRun(context, tests[testName!]?.test); | ||||||
| }, | ||||||
| test( | ||||||
| name: string, | ||||||
| overridesOrTestFn: StoryAnnotations<TRenderer, TRenderer['args']> | TestFunction<TRenderer>, | ||||||
| testFn?: TestFunction<TRenderer> | ||||||
| ): void { | ||||||
| const hasOverrides = typeof overridesOrTestFn !== 'function'; | ||||||
| const annotations = (hasOverrides ? overridesOrTestFn : {}) as StoryAnnotations< | ||||||
| TRenderer, | ||||||
| TRenderer['args'] | ||||||
| >; | ||||||
| const testFunction = (hasOverrides ? testFn : overridesOrTestFn) as TestFunction<TRenderer>; | ||||||
|
|
||||||
| // A test is a clone of the story + the test function | ||||||
| const testStory = this.extend({ ...annotations, tags: ['test-fn'] }); | ||||||
| testStory.input.__testFunction = testFunction; | ||||||
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||||||
| // @ts-ignore TODO: Kasper to figure out later | ||||||
| this.input.__tests![name] = testStory; | ||||||
| const annotations = typeof overridesOrTestFn !== 'function' ? overridesOrTestFn : {}; | ||||||
| const testFunction = typeof overridesOrTestFn !== 'function' ? testFn! : overridesOrTestFn; | ||||||
|
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. logic: Using non-null assertion
Suggested change
|
||||||
|
|
||||||
| tests[name] = { | ||||||
| story: this.extend({ ...annotations, tags: [...(annotations.tags ?? []), 'test-fn'] }), | ||||||
| test: testFunction, | ||||||
| }; | ||||||
| }, | ||||||
| extend<TInput extends StoryAnnotations<TRenderer, TRenderer['args']>>(input: TInput) { | ||||||
| return defineStory( | ||||||
|
|
@@ -243,5 +241,8 @@ function defineStory< | |||||
| this.meta | ||||||
| ); | ||||||
| }, | ||||||
| getAllTests() { | ||||||
| return tests; | ||||||
| }, | ||||||
| }; | ||||||
| } | ||||||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
syntax: Comment syntax is incorrect - should use
//instead of//*