storybook-addon-vis 0.18.2 → 0.18.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,8 @@
1
+ import * as vitest_plugin_vis_setup from 'vitest-plugin-vis/setup';
1
2
  import { SnapshotMeta } from 'vitest-plugin-vis';
2
3
 
3
- declare const vis: {
4
- presets: {
5
- manual(): void;
6
- auto(): void;
7
- theme(themes: Record<string, (options: SnapshotMeta<any> & {
8
- tags: string[];
9
- }) => Promise<boolean> | Promise<void> | boolean | void>): void;
10
- };
11
- beforeAll: {
12
- setup(): Promise<void>;
13
- };
14
- afterEach: {
15
- matchImageSnapshot(): Promise<void>;
16
- matchPerTheme(themes: Record<string, (options: SnapshotMeta<any> & {
17
- tags: string[];
18
- }) => Promise<boolean> | Promise<void> | boolean | void>): () => Promise<void>;
19
- };
20
- };
4
+ declare const vis: vitest_plugin_vis_setup.VisClientConfigurator<SnapshotMeta<any> & {
5
+ tags: string[];
6
+ }>;
21
7
 
22
8
  export { vis };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-vis",
3
- "version": "0.18.2",
3
+ "version": "0.18.4",
4
4
  "description": "Storybook Vitest visual testing addon",
5
5
  "keywords": [
6
6
  "image-snapshot",
@@ -58,7 +58,7 @@
58
58
  ],
59
59
  "dependencies": {
60
60
  "type-plus": "8.0.0-beta.7",
61
- "vitest-plugin-vis": "2.1.0"
61
+ "vitest-plugin-vis": "2.2.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@storybook/addon-essentials": "^8.4.2",
@@ -72,9 +72,9 @@
72
72
  "@types/react": "^18.2.65",
73
73
  "@types/react-dom": "^18.2.21",
74
74
  "@vitejs/plugin-react": "^4.2.1",
75
- "@vitest/browser": "^3.0.5",
76
- "@vitest/coverage-istanbul": "^3.0.5",
77
- "@vitest/expect": "^3.0.5",
75
+ "@vitest/browser": "^3.0.6",
76
+ "@vitest/coverage-istanbul": "^3.0.6",
77
+ "@vitest/expect": "^3.0.6",
78
78
  "boxen": "^8.0.1",
79
79
  "cross-env": "^7.0.3",
80
80
  "dedent": "^1.5.1",
@@ -86,7 +86,7 @@
86
86
  "tsup": "^8.3.5",
87
87
  "typescript": "^5.7.2",
88
88
  "vite": "^6.0.9",
89
- "vitest": "^3.0.5",
89
+ "vitest": "^3.0.6",
90
90
  "vitest-browser-react": "^0.1.0",
91
91
  "webdriverio": "latest",
92
92
  "zx": "^8.0.0"
package/readme.md CHANGED
@@ -8,6 +8,7 @@
8
8
  [`storybook-addon-vis`][storybook-addon-vis] captures and compares image snapshot automatically and manually.
9
9
 
10
10
  This addon is inspired by [`jest-image-snapshot`][jest-image-snapshot].
11
+ Internally, it uses [`vitest-plugin-vis`][vitest-plugin-vis] to do the heavy lifting.
11
12
 
12
13
  Starting from [Storybook] 8.3,
13
14
  Storybook introduces [Storybook Test addon][storybook-test-addon].
@@ -43,7 +44,7 @@ thus you need to add it to both [Storybook] and [Vitest].
43
44
 
44
45
  For [Vitest], you need to:
45
46
 
46
- - Add the `storybookVis` plugin in your `vitest.config.ts`.
47
+ - Add the `storybookVis` plugin in `vitest.config.ts`.
47
48
  - Add project annotations and setup Vitest life cycle in `vitest.setup.ts`.
48
49
 
49
50
  #### Edit Vitest Config
@@ -60,12 +61,20 @@ export default defineConfig({
60
61
  storybookVis(/* options */)
61
62
  ],
62
63
  test: {
64
+ // vitest v2
63
65
  browser: {
64
- // typical browser config
65
66
  enabled: true,
66
67
  provider: 'playwright',
67
- name: 'chromium'
68
+ name: 'chromium',
68
69
  },
70
+ // vitest v3
71
+ browser: {
72
+ enabled: true,
73
+ provider: 'playwright',
74
+ instances: [
75
+ { browser: 'chromium' }
76
+ ]
77
+ }
69
78
  // recommend to set to false
70
79
  globals: false,
71
80
  // Needed by both Storybook Test Addon and Storybook Visual Testing
@@ -105,7 +114,7 @@ export default defineConfig({
105
114
  }) => `__vis__/${ci ? platform : 'local'}`,
106
115
  platform: '...', // {process.platform} or `local` (deprecated use `snapshotRootDir` instead)
107
116
  customizeSnapshotSubpath: (subpath) => trimCommonFolder(subpath),
108
- customizeSnapshotId: (id, index) => `${id}-${index}`,
117
+ customizeSnapshotId: ({ id, index }) => `${id}-${index}`,
109
118
  // set a default subject (e.g. 'subject') to capture image snapshot
110
119
  subjectDataTestId: undefined,
111
120
  comparisonMethod: 'pixel',
@@ -119,7 +128,7 @@ export default defineConfig({
119
128
  })
120
129
  ```
121
130
 
122
- `storybookVis()` does not provide the `auto` or `manual` presets because you will need to [provide your `vitest.setup.ts`][storybook-test-addon#example-config] to make the story configuration available to Vitest anyway.
131
+ `storybookVis()` does not provide the `auto`, `enable`, or `manual` presets because you will need to [provide your `vitest.setup.ts`][storybook-test-addon#example-config] to make the story configuration available to Vitest anyway.
123
132
  So it is better to do the setup in one place.
124
133
 
125
134
  ##### Snapshot folder
@@ -151,7 +160,16 @@ import { defineConfig } from 'vitest/config'
151
160
  export default defineConfig({
152
161
  plugins: [
153
162
  storybookVis({
154
- snapshotRootDir: 'path/to/snapshot'
163
+ snapshotRootDir: 'path/to/snapshot',
164
+ // or if you need super power
165
+ snapshotRootDir: ({
166
+ ci, // true if running on CI
167
+ platform, // process.platform
168
+ providerName, // 'playwright' or 'webdriverio'
169
+ browserName,
170
+ screenshotFailures, // from `browser` config
171
+ screenshotDirectory, // from `browser` config
172
+ }) => string,
155
173
  })
156
174
  ],
157
175
  // ...
@@ -267,14 +285,23 @@ const project = setProjectAnnotations([
267
285
  projectAnnotations
268
286
  ])
269
287
 
270
- // setup visual testing but no post-test image snapshot
288
+ // setup visual testing but no post-test image snapshot support
271
289
  vis.presets.manual()
272
290
 
291
+ // setup visual testing with post-test image snapshot support.
292
+ // use `setAutoSnapshotOptions()` in your test to enable it.
293
+ vis.presets.enable()
294
+
273
295
  // capture image snapshot at the end of each test and story with `snapshot` tag
296
+ // Note: starting in 1.0.0, the `snapshot` tag will be automatically added.
274
297
  vis.presets.auto()
275
298
 
276
299
  // capture image snapshot at the end of each test and story with `snapshot` tag
277
- // for multiple themes (light and dark in this example)
300
+ // for multiple themes (light and dark in this example).
301
+ //
302
+ // Note that this changes the theme in the `afterEach` hook.
303
+ // If you want to capture manual snapshots in different themes,
304
+ // configure Vitest to run the tests in different themes.
278
305
  vis.presets.theme({
279
306
  async light() { document.body.classList.remove('dark') },
280
307
  async dark() { document.body.classList.add('dark') },
@@ -344,7 +371,7 @@ To address this, you can add the following to your `tsconfig.json`:
344
371
 
345
372
  ## Usage - automatic snapshot
346
373
 
347
- With the `auto` preset, [`storybook-addon-vis`][storybook-addon-vis] automatically captures image snapshot for stories with `snapshot` tag.
374
+ With the `auto` or `enable` preset, [`storybook-addon-vis`][storybook-addon-vis] automatically captures image snapshot for stories with `snapshot` tag.
348
375
 
349
376
  As how tags work in [Storybook], you can add the tag globally, per story file, or per story.
350
377
 
@@ -399,8 +426,8 @@ export const MyStory = {
399
426
 
400
427
  // in vitest.setup.ts
401
428
  vis.presets.theme({
402
- light(options) {
403
- if (options.tags.includes('!light')) return false
429
+ async light({ tags }) {
430
+ if (tags.includes('!light')) return false
404
431
  document.body.classList.remove('dark')
405
432
  }
406
433
  })
@@ -495,8 +522,8 @@ With the default snapshot folder structure, you should add the following to your
495
522
 
496
523
  ```ini
497
524
  # .gitignore
498
- **/__vis__/__diffs__
499
- **/__vis__/__results__
525
+ **/__vis__/**/__diffs__
526
+ **/__vis__/**/__results__
500
527
  **/__vis__/local
501
528
  ```
502
529
 
@@ -522,6 +549,23 @@ or hoist the `pathe` package:
522
549
  hoist-pattern[] = pathe
523
550
  ```
524
551
 
552
+ > It takes empty snapshots on Vitest tests
553
+
554
+ If you are using Storybook 8.5 and using the workaround to run both stories and tests [as described here](https://github.com/storybookjs/storybook/issues/30307),
555
+ Storybook are also transforming the tests as if they are stories.
556
+ That causes it to inject elements into the DOM and this addon detect that as some rendering by your tests,
557
+ thus taking an image snapshot.
558
+
559
+ Since it is a bug to be addressed soon,
560
+ please disable the snapshots for your tests by adding a `beforeAll` hook:
561
+
562
+ ```ts
563
+ import { setAutoSnapshotOptions } from 'storybook-addon-vis'
564
+ import { beforeAll } from 'vitest'
565
+
566
+ beforeAll(() => setAutoSnapshotOptions(false))
567
+ ```
568
+
525
569
  [@storybook/experimental-addon-test]: https://www.npmjs.com/package/@storybook/experimental-addon-test
526
570
  [downloads_image]: https://img.shields.io/npm/dm/storybook-addon-vis.svg?style=flat
527
571
  [github_action_url]: https://github.com/repobuddy/storybook-addon-vis/actions