Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Last active April 7, 2025 02:03
Show Gist options
  • Save rstacruz/4e301b59a1c9642ecdca1e63dc210bfd to your computer and use it in GitHub Desktop.
Save rstacruz/4e301b59a1c9642ecdca1e63dc210bfd to your computer and use it in GitHub Desktop.

Revisions

  1. rstacruz revised this gist Apr 7, 2025. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions gistfile1.md
    Original 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', () => {
    }
    )
    })

    // 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)
    })
    })
    ```

    ## Why
  2. rstacruz revised this gist Apr 7, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original 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.
    - [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).
  3. rstacruz revised this gist Apr 7, 2025. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.md
    Original 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.
    - 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.
  4. rstacruz revised this gist Apr 7, 2025. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,11 @@

    Let's say you have these files:

    - `ChatApp.tsx`
    - `ChatApp.stories.tsx`
    - `ChatApp.test.tsx`
    - `Chat.tsx`
    - `Chat.stories.tsx`
    - `Chat.test.tsx`

    These can be used in the test file:
    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

    // Render storybook stories via React testing-library
    // 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.
  5. rstacruz revised this gist Apr 7, 2025. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion gistfile1.md
    Original 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 './ChatApp.stories'
    import * as stories from './Chat.stories'

    const Component = stories.default.component

  6. rstacruz renamed this gist Apr 7, 2025. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.txt → gistfile1.md
    Original 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'
  7. rstacruz created this gist Apr 7, 2025.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original 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)
    })
    })
    ```