Last active
April 7, 2025 02:03
-
-
Save rstacruz/4e301b59a1c9642ecdca1e63dc210bfd to your computer and use it in GitHub Desktop.
Revisions
-
rstacruz revised this gist
Apr 7, 2025 . 1 changed file with 14 additions and 13 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -15,6 +15,20 @@ import * as stories from './Chat.stories' const Component = stories.default.component // Example: making assertions on certain stories describe('story: Primary', () => { it('render with assertions', () => { const story = stories.Primary render(<Component {...story.args} />) // Check at least one chat message exists const chatMessages = screen.getAllByRole('article', { hidden: true }) expect(chatMessages.length).toBeGreaterThan(0) }) }) // Example: render storybook stories via React testing-library describe('all stories', () => { const storyEntries = Object.entries(stories).filter( @@ -31,19 +45,6 @@ describe('all stories', () => { } ) }) ``` ## Why -
rstacruz revised this gist
Apr 7, 2025 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -53,4 +53,4 @@ describe('story: Primary', () => { ## Also see - [Vitest browser mode](https://vitest.dev/guide/browser/) takes this idea one step further and removes the need for Storybook (at least for this use case). -
rstacruz revised this gist
Apr 7, 2025 . 1 changed file with 5 additions and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -49,4 +49,8 @@ describe('story: Primary', () => { ## Why - Storybook stories can act like _smoke tests_. Tests will fail if there are any stories that emit errors on render. - Test stories can be inspected visually in Storybook. ## Also see - [Vitest browser mode](https://vitest.dev/guide/browser/) takes this idea one step further and removes the need for Storybook. -
rstacruz revised this gist
Apr 7, 2025 . 1 changed file with 11 additions and 6 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -2,11 +2,11 @@ Let's say you have these files: - `Chat.tsx` - `Chat.stories.tsx` - `Chat.test.tsx` The test file can be written to use Storybook stories: ```typescript import { describe, it } from 'vitest' @@ -15,7 +15,7 @@ import * as stories from './Chat.stories' const Component = stories.default.component // Example: render storybook stories via React testing-library describe('all stories', () => { const storyEntries = Object.entries(stories).filter( ([key, story]) => @@ -44,4 +44,9 @@ describe('story: Primary', () => { expect(chatMessages.length).toBeGreaterThan(0) }) }) ``` ## Why - Storybook stories can act like _smoke tests_. Tests will fail if there are any stories that emit errors on render. - Test stories can be inspected visually in Storybook. -
rstacruz revised this gist
Apr 7, 2025 . 1 changed file with 9 additions and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,17 @@ # Test storybook stories in Vitest + React testing library Let's say you have these files: - `ChatApp.tsx` - `ChatApp.stories.tsx` - `ChatApp.test.tsx` These can be used in the test file: ```typescript import { describe, it } from 'vitest' import { render, screen } from '@testing-library/react' import * as stories from './Chat.stories' const Component = stories.default.component -
rstacruz renamed this gist
Apr 7, 2025 . 1 changed file with 2 additions and 0 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # Test storybook stories in Vitest + React testing library ```typescript import { describe, it } from 'vitest' import { render, screen } from '@testing-library/react' -
rstacruz created this gist
Apr 7, 2025 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ ```typescript import { describe, it } from 'vitest' import { render, screen } from '@testing-library/react' import * as stories from './ChatApp.stories' const Component = stories.default.component // Render storybook stories via React testing-library describe('all stories', () => { const storyEntries = Object.entries(stories).filter( ([key, story]) => typeof story === 'object' && key !== 'default' && 'args' in story ) it.each(storyEntries)( 'renders %s story without errors', (_storyName, story) => { if (!('args' in story)) throw new Error('Invariant') render(<Component {...story.args} />) } ) }) // Example: making assertions on certain stories describe('story: Primary', () => { it('render with assertions', () => { const story = stories.Primary render(<Component {...story.args} />) // Check at least one chat message exists const chatMessages = screen.getAllByRole('article', { hidden: true }) expect(chatMessages.length).toBeGreaterThan(0) }) }) ```