-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storybook: Set up local visual regression testing
- Loading branch information
Showing
8 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Storybook Playwright Tests | ||
|
||
This is currently set up for testing visual regressions in the `components` package. The tests do not run on CI, and is meant as a testing tool for local development. | ||
|
||
## How to run | ||
|
||
First, prepare a static build of the Storybook. This is required to generate the `stories.json` file, which contains metadata for every story. | ||
|
||
```sh | ||
npm run storybook:build | ||
``` | ||
|
||
Then serve the Storybook locally, using either the static build prepared in the previous step, or the dev server if you want to iterate in watch mode. | ||
|
||
```sh | ||
# Using the static build | ||
npx http-server storybook/build -p 50240 | ||
|
||
# Using the dev server | ||
npm run storybook:dev | ||
``` | ||
|
||
You are now ready to run the tests. The first run will generate the reference images, and subsequent runs will compare against them. | ||
|
||
```sh | ||
npm run test:e2e:storybook | ||
``` | ||
|
||
To update the reference images, pass the `--update-snapshots` flag. | ||
|
||
```sh | ||
npm run test:e2e:storybook -- --update-snapshots | ||
``` |
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,45 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { test, expect } from '@playwright/test'; | ||
import { readFileSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
import type { StoryIndex, StoryIndexEntry } from '@storybook/store'; | ||
|
||
const STORYBOOK_PORT = '50240'; | ||
const STORYBOOK_DIR = resolve( __dirname, '../../storybook/build' ); | ||
|
||
const { stories }: StoryIndex = JSON.parse( | ||
readFileSync( resolve( STORYBOOK_DIR, 'stories.json' ) ).toString() | ||
); | ||
|
||
const includeIds = [ /^components-/ ]; | ||
const excludeIds = [ /animate/, /zstack/ ]; | ||
|
||
const filterMatches = ( story: StoryIndexEntry ) => { | ||
const isIncluded = includeIds.some( ( includeRegex ) => | ||
includeRegex.test( story.id ) | ||
); | ||
const isExcluded = excludeIds.some( ( excludeRegex ) => | ||
excludeRegex.test( story.id ) | ||
); | ||
return isIncluded && ! isExcluded; | ||
}; | ||
|
||
test.describe.parallel( 'Storybook visual regressions', () => { | ||
Object.values( stories ) | ||
.filter( filterMatches ) | ||
.forEach( ( story ) => { | ||
test( `${ story.title }: ${ story.name }`, async ( { page } ) => { | ||
await page.goto( | ||
`http://localhost:${ STORYBOOK_PORT }/iframe.html?id=${ story.id }`, | ||
{ waitUntil: 'load' } | ||
); | ||
expect( | ||
await page | ||
.locator( '#root' ) | ||
.screenshot( { animations: 'disabled' } ) | ||
).toMatchSnapshot( [ story.title, `${ story.id }.png` ] ); | ||
} ); | ||
} ); | ||
} ); |
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,13 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { PlaywrightTestConfig } from '@playwright/test'; | ||
|
||
const config: PlaywrightTestConfig = { | ||
outputDir: 'test-results/output', | ||
reporter: [ | ||
[ 'html', { open: 'on-failure', outputFolder: 'test-results/report' } ], | ||
], | ||
}; | ||
|
||
export default config; |