-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(storybook): support running visual tests in chromatic
To better support visual tests, Modals are now opened automatically (this does not happen in docs mode, so it doesn't affect the docs pages) For now, Chromatic is not integrated into CI. To run visual tests, add (or edit) the file `.env` at the root of the repo. This file is in .gitignore and will not be committed. Add the following to `.env`: ``` CHROMATIC_PROJECT_TOKEN=<token> ``` Replace `<token>` with the token found [here](https://www.chromatic.com/manage?appId=66fe736b9d639fe6801bf130&setup=true), under "Setup Chromatic with this project token". Then run these commands: ``` yarn build:storybook yarn chromatic ``` You can also replace the last command with e.g. ``` yarn chromatic:all --onlyStoryNames "Komponenter/Modal/*" ``` ...to only run tests for the Modal components
- Loading branch information
Showing
15 changed files
with
245 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Decorator } from '@storybook/react'; | ||
|
||
/** | ||
* This decorator is used to customize the style of the root story element. | ||
* It is useful for customizing the layout, or when you need to account | ||
* for elements that would otherwise not be visible in Chromatic's visual snapshots. | ||
* | ||
* The decorator is added globally, and can be configured through `parameters.customStyles` | ||
* at the meta or story level. E.g. | ||
* ```ts | ||
* parameters: { | ||
* customStyles: { | ||
* // These apply both in docs mode and story mode | ||
* display: 'flex', | ||
* gap: '8px', | ||
* docs: { | ||
* // These apply only when the story renders in a docs page | ||
* height: '200px' | ||
* }, | ||
* story: { | ||
* // These apply only when the story is viewed individually | ||
* height: '100vh' | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* By default, the decorator sets `overflow: hidden` so you can see in Storybook exactly | ||
* what Chromatic's snapshot will be, and `padding: 1rem` to account for most overflowing | ||
* elements like focus styles, badges etc. | ||
* | ||
* From Chromatic's documentation: | ||
* > Snapshots can sometimes exclude outline and other focus styles because Chromatic | ||
* > trims each snapshot to the dimensions of the root node of the story. To capture | ||
* > those styles, wrap the story in a decorator that adds slight padding. | ||
*/ | ||
export const customStylesDecorator: Decorator = (Story, ctx) => { | ||
const { docs, story, ...style } = ctx.parameters.customStyles ?? {}; | ||
|
||
return ( | ||
<div | ||
className='storybook-decorator' | ||
style={{ | ||
overflow: 'hidden', | ||
padding: '1rem', | ||
...style, | ||
...(ctx.viewMode === 'docs' && docs), | ||
...(ctx.viewMode === 'story' && story), | ||
}} | ||
> | ||
<Story /> | ||
</div> | ||
); | ||
}; |
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,6 @@ | ||
import { fromPairs } from 'ramda'; | ||
export const viewportWidths = [320, 375, 576, 768, 992, 1200, 1440] as const; | ||
|
||
export const allModes = fromPairs( | ||
viewportWidths.map((width) => [width, { viewport: { width } }]), | ||
); |
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,81 @@ | ||
import type {} from '@storybook/types'; | ||
import type { CSSProperties } from 'react'; | ||
|
||
type ChromaticViewport = { | ||
width?: number | `${string}px`; | ||
height?: number | `${string}px`; | ||
}; | ||
|
||
declare module '@storybook/types' { | ||
interface Parameters { | ||
/** | ||
* Set custom styling for the story's root element. The default styling is: | ||
* ```css | ||
* { overflow: hidden; padding: 1rem; } | ||
* ``` | ||
* | ||
* This is a custom parameter, implemented by `customStylesDecorator.ts`. | ||
* */ | ||
customStyles?: CSSProperties & { | ||
/** Styles that only apply when viewing a docs page */ | ||
docs?: CSSProperties; | ||
/** Styles that only apply when viewing an individual story */ | ||
story?: CSSProperties; | ||
}; | ||
|
||
/** | ||
* Set the story layout. | ||
* | ||
* This is a standard Storybook parameter, | ||
* [see the docs](https://storybook.js.org/docs/configure/story-layout) | ||
*/ | ||
layout?: 'centered' | 'fullscreen' | 'padded'; | ||
|
||
/** | ||
* Configure Chromatic. See [the documentation](https://www.chromatic.com/docs/config-with-story-params/). | ||
*/ | ||
chromatic?: { | ||
/** Disable visual snapshots at the component or story level */ | ||
disableSnapshot?: true; | ||
/** | ||
* By default, CSS animations are paused at the end of their animation cycle | ||
* when tests are run in Chromatic. Setting this to false will pause animations | ||
* at the first frame instead. | ||
*/ | ||
pauseAnimationAtEnd?: false; | ||
/** Delay in ms before running tests in Chromatic */ | ||
delay?: number; | ||
/** | ||
* Allows you to fine-tune the threshold for visual change between snapshots before | ||
* Chromatic flags them. Must be a number from 0 to 1. 0 is the most accurate, while | ||
* 1 is the least accurate. | ||
* | ||
* @default 0.063 | ||
*/ | ||
diffThreshold?: number; | ||
/** | ||
* Modes allow separate snapshots and baselines for a collection | ||
* of parameters like viewport size, theme etc. | ||
*/ | ||
modes?: Record< | ||
string, | ||
{ | ||
/** | ||
* Disable a mode that has been enabled at a higher level. | ||
* E.g. disable a global mode for a specific story. | ||
**/ | ||
disable?: true; | ||
/** | ||
* The viewport to use. | ||
* | ||
* This parameter can either be an object with height and/or width (in px), or | ||
* the name of one of the viewports configured in `parameters.viewports` in `.storybook/preview.tsx` | ||
*/ | ||
viewport?: ChromaticViewport | string; | ||
// ...any other globals from Storybook, addons or decorators which we want | ||
// to use in modes can also be added here | ||
} | ||
>; | ||
}; | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"$schema": "https://www.chromatic.com/config-file.schema.json", | ||
"onlyChanged": true, | ||
"projectId": "Project:66fe736b9d639fe6801bf130", | ||
"storybookBaseDir": "apps/storybook", | ||
"storybookBuildDir": "apps/storybook/dist", | ||
"zip": true | ||
} |
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
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