|
| 1 | +import Admonition from "@theme/Admonition"; |
| 2 | + |
| 3 | +# @testplane/storybook |
| 4 | + |
| 5 | +## Overview {#overview} |
| 6 | + |
| 7 | +Use the [@testplane/storybook][testplane-storybook] plugin to automatically check the changes in your Storybook components with screenshot testing without a single line of test code. |
| 8 | + |
| 9 | +This plugin adds: |
| 10 | + |
| 11 | +- automatic screenshot tests for storybook stories; |
| 12 | +- an ability to write Testplane tests for storybook stories right inside of the stories. |
| 13 | + |
| 14 | +## Installation {#install} |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @testplane/storybook --save-dev |
| 18 | +``` |
| 19 | + |
| 20 | +## Setup {#setup} |
| 21 | + |
| 22 | +<Admonition type="info">Storybook 6.4+ is required to use this plugin.</Admonition> |
| 23 | + |
| 24 | +### Storybook |
| 25 | + |
| 26 | +If you use `storybook@6`, you will need to enable [buildStoriesJson][build-stories] feature in your `storybook` config: |
| 27 | + |
| 28 | +```typescript tytle=".storybook/main.js" |
| 29 | +export default { |
| 30 | + // ... |
| 31 | + features: { |
| 32 | + // ... |
| 33 | + buildStoriesJson: true, |
| 34 | + }, |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +You don't need to do this with storybook@7 or storybook@8. |
| 39 | + |
| 40 | +### Testplane |
| 41 | + |
| 42 | +Add `@testplane/storybook` plugin into your Testplane config: |
| 43 | + |
| 44 | +```typescript title=".testplane.conf.ts" |
| 45 | +export default { |
| 46 | + plugins: { |
| 47 | + "@testplane/storybook": {}, |
| 48 | + |
| 49 | + // other Testplane plugins... |
| 50 | + }, |
| 51 | + |
| 52 | + // other Testplane settings... |
| 53 | +}; |
| 54 | +``` |
| 55 | + |
| 56 | +With this minimal config, you will be able to run `npx testplane --storybook` to autotest each storybook story with [Testplane assertView][assert-view] command. Testplane will open each story, wait for play function to finish (if defined), and then call `assertView` command. These tests would be generated in runtime. |
| 57 | + |
| 58 | +### Description of configuration parameters {#setup_description} |
| 59 | + |
| 60 | +<table> |
| 61 | + <thead> |
| 62 | + <tr> |
| 63 | + <td>**Parameter**</td> |
| 64 | + <td>**Type**</td> |
| 65 | + <td>**Default**</td> |
| 66 | + <td>**Description**</td> |
| 67 | + </tr> |
| 68 | + </thead> |
| 69 | + <tbody> |
| 70 | + <tr> |
| 71 | + <td>enabled</td> |
| 72 | + <td>`Boolean`</td> |
| 73 | + <td>true</td> |
| 74 | + <td>Enable / disable plugin.</td> |
| 75 | + </tr> |
| 76 | + <tr> |
| 77 | + <td>storybookConfigDir</td> |
| 78 | + <td>`String`</td> |
| 79 | + <td>".storybook"</td> |
| 80 | + <td>Path to the storybook configuration directory.</td> |
| 81 | + </tr> |
| 82 | + <tr> |
| 83 | + <td>autoScreenshots</td> |
| 84 | + <td>`Boolean`</td> |
| 85 | + <td>true</td> |
| 86 | + <td>Enable / disable auto-screenshot tests</td> |
| 87 | + </tr> |
| 88 | + <tr> |
| 89 | + <td>localport</td> |
| 90 | + <td>`Number`</td> |
| 91 | + <td>6006</td> |
| 92 | + <td>Port to launch storybook dev server on.</td> |
| 93 | + </tr> |
| 94 | + <tr> |
| 95 | + <td>remoteStorybookUrl</td> |
| 96 | + <td>`String`</td> |
| 97 | + <td>""</td> |
| 98 | + <td>URL of the remote Storybook. If specified, local storybook dev sever would not be launched.</td> |
| 99 | + </tr> |
| 100 | + <tr> |
| 101 | + <td>browserIds</td> |
| 102 | + <td>`Array<String | RegExp>`</td> |
| 103 | + <td>[]</td> |
| 104 | + <td>Array of `browserId` to run storybook tests on. By default, all of browsers, specified in Testplane config would be used</td> |
| 105 | + </tr> |
| 106 | + </tbody> |
| 107 | +</table> |
| 108 | + |
| 109 | +<Admonition type="info"> |
| 110 | + Storybook tests performance greatly depends on [Testplane testsPerSession][testsPerSession] |
| 111 | + parameter, as these tests speeds up on reusing existing sessions, so setting values around 20+ |
| 112 | + is preferred. These tests ignore [Testplane isolation][isolation]. It would be turned off |
| 113 | + unconditionally. |
| 114 | +</Admonition> |
| 115 | + |
| 116 | +## Advanced usage |
| 117 | + |
| 118 | +If you have `ts-node` in your project, you can write your Testplane tests right inside of storybook story files: |
| 119 | + |
| 120 | +<Admonition type="info"> |
| 121 | + Storybook story files must have `.js` or `.ts` extension for this to work. Formats `jsx/tsx` are |
| 122 | + not supported yet. |
| 123 | +</Admonition> |
| 124 | + |
| 125 | +```typescript title="stories/Primary.stories.ts" |
| 126 | +import type { StoryObj } from "@storybook/react"; |
| 127 | +import type { WithTestplane } from "@testplane/storybook"; |
| 128 | + |
| 129 | +export const Primary: WithTestplane<StoryObj> = { |
| 130 | + args: { |
| 131 | + primary: true, |
| 132 | + label: "Button", |
| 133 | + }, |
| 134 | + testplane: { |
| 135 | + "my test": async ({ browser, expect }) => { |
| 136 | + const element = await browser.$(".storybook-button"); |
| 137 | + |
| 138 | + await expect(element).toHaveText("Button"); |
| 139 | + }, |
| 140 | + }, |
| 141 | +}; |
| 142 | +``` |
| 143 | + |
| 144 | +You can also specify extra options in story default config: |
| 145 | + |
| 146 | +```typescript |
| 147 | +import type { WithTestplane } from "@testplane/storybook"; |
| 148 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 149 | + |
| 150 | +const meta: WithTestplane<Meta<typeof Button>> = { |
| 151 | + title: "Example/Button", |
| 152 | + component: Button, |
| 153 | + testplane: { |
| 154 | + skip: false, // if true, skips all Testplane tests from this story file |
| 155 | + autoscreenshotSelector: ".my-selector", // Custom selector to auto-screenshot elements |
| 156 | + browserIds: ["chrome"], // Testplane browsers to run tests from this story file |
| 157 | + assertViewOpts: { |
| 158 | + // override default assertView options for tests from this file |
| 159 | + ignoreDiffPixelCount: 5, |
| 160 | + }, |
| 161 | + }, |
| 162 | +}; |
| 163 | + |
| 164 | +export default meta; |
| 165 | +``` |
| 166 | + |
| 167 | +If you decide to create separate config for storybook auto-tests (which is suggested), you need to specify config path via CLI option. For example: |
| 168 | + |
| 169 | +```bash |
| 170 | +npx testplane --storybook -c .testplane.storybook.conf.ts |
| 171 | +``` |
| 172 | + |
| 173 | +This allows you to store references next to your story files: |
| 174 | + |
| 175 | +```typescript title=".testplane.conf.ts" |
| 176 | +import path from "path"; |
| 177 | +import { getStoryFile } from "@testplane/storybook"; |
| 178 | + |
| 179 | +export default { |
| 180 | + screenshotsDir: test => { |
| 181 | + const relativeStoryFilePath = getStoryFile(test); |
| 182 | + const relativeStoryFileDirPath = path.dirname(relativeStoryFilePath); |
| 183 | + |
| 184 | + return path.join(relativeStoryFileDirPath, "screens", test.id, test.browserId); |
| 185 | + }, |
| 186 | + // other Testplane settings... |
| 187 | +}; |
| 188 | +``` |
| 189 | + |
| 190 | +In this example, screenshot references would be stored in `screens/<testId>/<browserId>` folder, next to each of your story files. |
| 191 | + |
| 192 | +## Useful links {#useful_links} |
| 193 | + |
| 194 | +- [@testplane/storybook plugin sources][testplane-storybook] |
| 195 | +- [assertView command][assert-view] |
| 196 | + |
| 197 | +[assert-view]: ../commands/browser/assertView.mdx |
| 198 | +[build-stories]: https://storybook.js.org/docs/6.4/configure/overview#feature-flags |
| 199 | +[isolation]: ../config/browsers.mdx#isolation |
| 200 | +[testplane-storybook]: https://github.com/gemini-testing/testplane-storybook |
| 201 | +[testplane]: https://github.com/gemini-testing/testplane |
| 202 | +[testsPerSession]: ../config/browsers.mdx#tests_per_session |
0 commit comments