vitest-plugin-vis 3.1.0 → 3.3.0

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.
package/src/setup.ts CHANGED
@@ -4,6 +4,5 @@ import './client/page/extend.ts'
4
4
 
5
5
  export * from './client/setup/vis.ts'
6
6
 
7
- // This is exported for main/app boundary separation
8
- // They can co-exist in the same export file thanks to ESM.
7
+ // This is deprecated and moved to `/client-api`.
9
8
  export * from './client/setup/create_vis.ts'
@@ -0,0 +1,122 @@
1
+ import { Meta, Source } from "@storybook/addon-docs/blocks";
2
+
3
+ <Meta title="vitest-plugin-vis/setup/vis" />
4
+
5
+ `vis` from `vitest-plugin-vis/setup` is the main mechanism to setup the plugin on the client side.
6
+
7
+ ```ts
8
+ // vitest.setup.ts
9
+ import { vis } from 'vitest-plugin-vis/setup'
10
+
11
+ vis.setup()
12
+ ```
13
+
14
+ ## Basic Setup
15
+
16
+ ```ts
17
+ // vitest.setup.ts
18
+ import { vis } from 'vitest-plugin-vis/setup'
19
+
20
+ // Basic setup - enables visual testing without auto snapshots
21
+ vis.setup()
22
+
23
+ // Enable auto snapshots for all tests
24
+ vis.setup({ auto: true })
25
+
26
+ // Disable auto snapshots (same as vis.setup())
27
+ vis.setup({ auto: false })
28
+ ```
29
+
30
+ ## Conditional Auto Snapshots
31
+
32
+ You can control when auto snapshots are taken based on test metadata:
33
+
34
+ ```ts
35
+ // vitest.setup.ts
36
+ import { vis } from 'vitest-plugin-vis/setup'
37
+
38
+ // Take snapshots only for tests with specific metadata
39
+ vis.setup({
40
+ auto: async ({ meta }) => meta['visualTest'] === true
41
+ })
42
+
43
+ // Skip snapshots for tests marked as 'skipSnapshot'
44
+ vis.setup({
45
+ auto: async ({ meta }) => !meta['skipSnapshot']
46
+ })
47
+ ```
48
+
49
+ ## Multi-Theme Setup
50
+
51
+ Set up automatic snapshots for multiple themes (e.g., light and dark mode):
52
+
53
+ ```ts
54
+ // vitest.setup.ts
55
+ import { vis } from 'vitest-plugin-vis/setup'
56
+
57
+ vis.setup({
58
+ auto: {
59
+ async light() {
60
+ document.body.classList.remove('dark')
61
+ document.body.classList.add('light')
62
+ },
63
+ async dark() {
64
+ document.body.classList.add('dark')
65
+ document.body.classList.remove('light')
66
+ }
67
+ }
68
+ })
69
+ ```
70
+
71
+ ## Conditional Theme Snapshots
72
+
73
+ You can conditionally skip themes based on test metadata:
74
+
75
+ ```ts
76
+ // vitest.setup.ts
77
+ import { vis } from 'vitest-plugin-vis/setup'
78
+
79
+ vis.setup({
80
+ auto: {
81
+ async light({ meta }) {
82
+ if (meta['skipLight']) return false
83
+ document.body.classList.remove('dark')
84
+ },
85
+ async dark({ meta }) {
86
+ if (meta['skipDark']) return false
87
+ document.body.classList.add('dark')
88
+ }
89
+ }
90
+ })
91
+ ```
92
+
93
+ ## Advanced Theme Configuration
94
+
95
+ You can set up more complex theme configurations:
96
+
97
+ ```ts
98
+ // vitest.setup.ts
99
+ import { vis } from 'vitest-plugin-vis/setup'
100
+
101
+ vis.setup({
102
+ auto: {
103
+ async desktop() {
104
+ document.body.style.width = '1200px'
105
+ document.body.classList.add('desktop')
106
+ },
107
+ async tablet() {
108
+ document.body.style.width = '768px'
109
+ document.body.classList.add('tablet')
110
+ },
111
+ async mobile() {
112
+ document.body.style.width = '375px'
113
+ document.body.classList.add('mobile')
114
+ },
115
+ async 'dark-desktop'({ meta }) {
116
+ if (meta['skipDark']) return false
117
+ document.body.style.width = '1200px'
118
+ document.body.classList.add('desktop', 'dark')
119
+ }
120
+ }
121
+ })
122
+ ```
@@ -0,0 +1,153 @@
1
+ import { Meta, Source } from "@storybook/addon-docs/blocks";
2
+
3
+ <Meta title="vitest-plugin-vis/config/trimCommonFolder" />
4
+
5
+ # trimCommonFolder
6
+
7
+ The `trimCommonFolder` function is a utility that removes common folder prefixes from file paths to generate cleaner snapshot sub-paths.
8
+
9
+ ## Purpose
10
+
11
+ When generating snapshot paths, test files are often located in common directories like `src`, `tests`, `test`, etc. The `trimCommonFolder` function removes these common prefixes to create shorter, more meaningful snapshot directory structures.
12
+
13
+ ## Usage
14
+
15
+ The default `snapshotSubpath` option in `vitest-plugin-vis/config` uses this function to trim common folder prefixes from the test file path:
16
+
17
+ ```ts
18
+ // vitest.config.ts
19
+ import { vis, trimCommonFolder } from 'vitest-plugin-vis/config'
20
+
21
+ export default defineConfig({
22
+ plugins: [vis({
23
+ snapshotSubpath: ({ subpath }) => trimCommonFolder(subpath)
24
+ })]
25
+ })
26
+ ```
27
+
28
+ You can use it in your own custom `snapshotSubpath` function.
29
+
30
+ ## Function Signature
31
+
32
+ ```ts
33
+ function trimCommonFolder(suiteName: string): string
34
+ ```
35
+
36
+ ## Supported Common Folders
37
+
38
+ The function recognizes and removes the following common folder prefixes:
39
+
40
+ - `tests` - Common test directory
41
+ - `test` - Alternative test directory
42
+ - `src` - Source code directory
43
+ - `source` - Alternative source directory
44
+ - `js` - JavaScript files directory
45
+ - `ts` - TypeScript files directory
46
+ - `lib` - Library files directory
47
+
48
+ ## Examples
49
+
50
+ ### Trimming Source Directories
51
+
52
+ ```ts
53
+ trimCommonFolder('src/components/Button.spec.ts')
54
+ // Returns: 'components/Button.spec.ts'
55
+
56
+ trimCommonFolder('source/utils/helpers.test.ts')
57
+ // Returns: 'utils/helpers.test.ts'
58
+ ```
59
+
60
+ ### Trimming Test Directories
61
+
62
+ ```ts
63
+ trimCommonFolder('tests/unit/Button.spec.ts')
64
+ // Returns: 'unit/Button.spec.ts'
65
+
66
+ trimCommonFolder('test/integration/api.test.ts')
67
+ // Returns: 'integration/api.test.ts'
68
+ ```
69
+
70
+ ### Trimming Language-Specific Directories
71
+
72
+ ```ts
73
+ trimCommonFolder('js/components/Modal.spec.js')
74
+ // Returns: 'components/Modal.spec.js'
75
+
76
+ trimCommonFolder('ts/types/User.test.ts')
77
+ // Returns: 'types/User.test.ts'
78
+
79
+ trimCommonFolder('lib/utils/format.spec.ts')
80
+ // Returns: 'utils/format.spec.ts'
81
+ ```
82
+
83
+ ### No Trimming Cases
84
+
85
+ ```ts
86
+ // Unrecognized folder - no trimming
87
+ trimCommonFolder('app/components/Button.spec.ts')
88
+ // Returns: 'app/components/Button.spec.ts'
89
+
90
+ // File in root - no trimming
91
+ trimCommonFolder('Button.spec.ts')
92
+ // Returns: 'Button.spec.ts'
93
+
94
+ // No slash separator - no trimming
95
+ trimCommonFolder('index.ts')
96
+ // Returns: 'index.ts'
97
+ ```
98
+
99
+ ### Before and After Snapshot Paths
100
+
101
+ Without `trimCommonFolder`:
102
+ ```
103
+ __vis__/local/__baselines__/src/components/Button.spec.ts/Button/should-render-correctly-auto.png
104
+ ```
105
+
106
+ With `trimCommonFolder`:
107
+ ```
108
+ __vis__/local/__baselines__/components/Button.spec.ts/Button/should-render-correctly-auto.png
109
+ ```
110
+
111
+ ## Custom Implementation
112
+
113
+ You can create your own trimming logic by implementing a similar function:
114
+
115
+ ```ts
116
+ // Custom trimming function
117
+ function customTrimFolder(suiteName: string) {
118
+ const customFolders = ['app', 'pages', 'components']
119
+ const suiteDir = suiteName.split('/', 1)[0]!
120
+
121
+ if (customFolders.includes(suiteDir)) {
122
+ return suiteName.slice(suiteDir.length + 1)
123
+ }
124
+
125
+ return suiteName
126
+ }
127
+
128
+ // Use in config
129
+ export default defineConfig({
130
+ plugins: [
131
+ vis({
132
+ snapshotSubpath: ({ subpath }) => customTrimFolder(subpath)
133
+ })
134
+ ]
135
+ })
136
+ ```
137
+
138
+ ### Case Sensitivity
139
+
140
+ The function is case-sensitive and only matches exact lowercase folder names:
141
+
142
+ ```ts
143
+ trimCommonFolder('SRC/components/Button.spec.ts')
144
+ // Returns: 'SRC/components/Button.spec.ts' (no trimming)
145
+
146
+ trimCommonFolder('Src/components/Button.spec.ts')
147
+ // Returns: 'Src/components/Button.spec.ts' (no trimming)
148
+ ```
149
+
150
+ ## Related
151
+
152
+ - [vis config](/docs/vitest-plugin-vis-config-vis--docs) - Main plugin configuration
153
+ - [Snapshot Path Customization](/docs/vitest-plugin-vis-config-vis--docs#customizing-snapshot-path) - How to customize snapshot paths