vitest-plugin-vis 1.6.5 → 1.7.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/esm/client/snapshot_options.d.ts +1 -0
- package/esm/client/snapshot_options.d.ts.map +1 -1
- package/esm/client/snapshot_options.internal.d.ts +1 -1
- package/esm/client/snapshot_options.internal.d.ts.map +1 -1
- package/esm/setup/create_vis.d.ts +4 -3
- package/esm/setup/create_vis.d.ts.map +1 -1
- package/esm/setup/create_vis.js +4 -1
- package/esm/setup/vis.d.ts +2 -2
- package/package.json +1 -1
- package/readme.md +23 -7
- package/src/client/snapshot_options.internal.ts +1 -1
- package/src/client/snapshot_options.ts +1 -0
- package/src/setup/create_vis.ts +8 -6
|
@@ -2,6 +2,7 @@ import type { AutoSnapshotOptions, ComparisonMethod } from '../shared/types.ts';
|
|
|
2
2
|
import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapshot.types.ts';
|
|
3
3
|
export type SnapshotMeta<M extends ComparisonMethod> = ToMatchImageSnapshotOptions<M> & AutoSnapshotOptions & {
|
|
4
4
|
enable?: boolean | undefined;
|
|
5
|
+
[key: string]: unknown;
|
|
5
6
|
};
|
|
6
7
|
type Suite = {
|
|
7
8
|
meta: Record<string, any>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapshot_options.d.ts","sourceRoot":"","sources":["../../src/client/snapshot_options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAE/E,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAE5F,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,gBAAgB,IAAI,2BAA2B,CAAC,CAAC,CAAC,GACpF,mBAAmB,GAAG;IACrB,MAAM,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"snapshot_options.d.ts","sourceRoot":"","sources":["../../src/client/snapshot_options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAE/E,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAE5F,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,gBAAgB,IAAI,2BAA2B,CAAC,CAAC,CAAC,GACpF,mBAAmB,GAAG;IACrB,MAAM,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC5B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACtB,CAAA;AAEF,KAAK,KAAK,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAAA;CAAE,CAAA;AAErE,MAAM,MAAM,QAAQ,GACjB;IACA,IAAI,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,GAAG,SAAS,CAAA;IAChD,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAAA;IACzB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CACxB,GACD,SAAS,CAAA;AAEZ;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,gBAAgB,EAChE,IAAI,EAAE,QAAQ,EACd,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,OAAO,GAC7B,IAAI,CAAA;AACP,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,gBAAgB,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,IAAI,CAAA"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { MetaTask, SnapshotMeta } from './snapshot_options.ts';
|
|
2
|
-
export declare function getAutoSnapshotOptions(task: MetaTask):
|
|
2
|
+
export declare function getAutoSnapshotOptions<M extends SnapshotMeta<any> = SnapshotMeta<any>>(task: MetaTask): M | undefined;
|
|
3
3
|
//# sourceMappingURL=snapshot_options.internal.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapshot_options.internal.d.ts","sourceRoot":"","sources":["../../src/client/snapshot_options.internal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAEnE,wBAAgB,sBAAsB,CAAC,
|
|
1
|
+
{"version":3,"file":"snapshot_options.internal.d.ts","sourceRoot":"","sources":["../../src/client/snapshot_options.internal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAEnE,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,YAAY,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,GAAG,CAAC,GAAG,SAAS,CAkBrH"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { type SnapshotMeta } from '../client.ts';
|
|
1
2
|
import type { SetupVisSuiteCommand } from '../server/commands/setup_vis_suite.ts';
|
|
2
|
-
export declare function createVis(commands: SetupVisSuiteCommand): {
|
|
3
|
+
export declare function createVis<M extends SnapshotMeta<any>>(commands: SetupVisSuiteCommand): {
|
|
3
4
|
presets: {
|
|
4
5
|
/**
|
|
5
6
|
* Enable manual visual testing.
|
|
@@ -26,14 +27,14 @@ export declare function createVis(commands: SetupVisSuiteCommand): {
|
|
|
26
27
|
* })
|
|
27
28
|
* ```
|
|
28
29
|
*/
|
|
29
|
-
theme(themes: Record<string, () =>
|
|
30
|
+
theme(themes: Record<string, (options: M) => Promise<boolean> | Promise<void> | boolean | void>): void;
|
|
30
31
|
};
|
|
31
32
|
beforeAll: {
|
|
32
33
|
setup(): Promise<void>;
|
|
33
34
|
};
|
|
34
35
|
afterEach: {
|
|
35
36
|
matchImageSnapshot(): Promise<void>;
|
|
36
|
-
matchPerTheme(themes: Record<string, () => Promise<void> | void>): () => Promise<void>;
|
|
37
|
+
matchPerTheme(themes: Record<string, (options: M) => Promise<boolean> | Promise<void> | boolean | void>): () => Promise<void>;
|
|
37
38
|
};
|
|
38
39
|
};
|
|
39
40
|
//# sourceMappingURL=create_vis.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create_vis.d.ts","sourceRoot":"","sources":["../../src/setup/create_vis.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create_vis.d.ts","sourceRoot":"","sources":["../../src/setup/create_vis.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,YAAY,EAAY,MAAM,cAAc,CAAA;AAI1D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAA;AAEjF,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,oBAAoB;;QAOlF;;WAEG;;QAIH;;;;WAIG;;QAKH;;;;;;;;;;;;;;WAcG;sBACW,MAAM,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,GAAG,IAAI,CAAC;;;;;;;8BAqBzE,MAAM,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,GAAG,IAAI,CAAC;;EA4CzG"}
|
package/esm/setup/create_vis.js
CHANGED
|
@@ -72,7 +72,10 @@ export function createVis(commands) {
|
|
|
72
72
|
const errors = [];
|
|
73
73
|
for (const themeId in themes) {
|
|
74
74
|
try {
|
|
75
|
-
await new Promise((a) => setTimeout(
|
|
75
|
+
await new Promise((a) => setTimeout(a, 10));
|
|
76
|
+
const r = await themes[themeId](meta);
|
|
77
|
+
if (r === false)
|
|
78
|
+
continue;
|
|
76
79
|
await test.context
|
|
77
80
|
.expect(getSubject(meta?.subjectDataTestId ?? subjectDataTestId))
|
|
78
81
|
.toMatchImageSnapshot({
|
package/esm/setup/vis.d.ts
CHANGED
|
@@ -5,14 +5,14 @@ export declare const vis: {
|
|
|
5
5
|
presets: {
|
|
6
6
|
manual(): void;
|
|
7
7
|
auto(): void;
|
|
8
|
-
theme(themes: Record<string, () =>
|
|
8
|
+
theme(themes: Record<string, (options: import("../client.ts").SnapshotMeta<any>) => Promise<boolean> | Promise<void> | boolean | void>): void;
|
|
9
9
|
};
|
|
10
10
|
beforeAll: {
|
|
11
11
|
setup(): Promise<void>;
|
|
12
12
|
};
|
|
13
13
|
afterEach: {
|
|
14
14
|
matchImageSnapshot(): Promise<void>;
|
|
15
|
-
matchPerTheme(themes: Record<string, () => Promise<void> | void>): () => Promise<void>;
|
|
15
|
+
matchPerTheme(themes: Record<string, (options: import("../client.ts").SnapshotMeta<any>) => Promise<boolean> | Promise<void> | boolean | void>): () => Promise<void>;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
18
18
|
//# sourceMappingURL=vis.d.ts.map
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -123,7 +123,7 @@ vis.presets.theme({
|
|
|
123
123
|
The main usage of this addon is to use the `toMatchImageSnapshot` matcher.
|
|
124
124
|
|
|
125
125
|
Since it is exposed under the `expect` object of `vitest`,
|
|
126
|
-
you typically do not need to import `
|
|
126
|
+
you typically do not need to import `vitest-plugin-vis` directly.
|
|
127
127
|
|
|
128
128
|
Because of this, TypeScript may not recognize the matcher.
|
|
129
129
|
To address this, you can add the following to your `tsconfig.json`:
|
|
@@ -198,6 +198,22 @@ setAutoSnapshotOptions({
|
|
|
198
198
|
})
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
+
You can also provide additional options, which you can use during theme to enable/disable snapshot for each theme:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
setAutoSnapshotOptions({
|
|
205
|
+
skipDark: true
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
// in vitest.setup.ts
|
|
209
|
+
vis.presets.theme({
|
|
210
|
+
async dark(options) {
|
|
211
|
+
if (options.skipDark) return false
|
|
212
|
+
document.body.classList.add('dark')
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
```
|
|
216
|
+
|
|
201
217
|
### Manual Snapshot
|
|
202
218
|
|
|
203
219
|
You can also set the preset to `manual` and compare snapshots manually:
|
|
@@ -218,9 +234,9 @@ export default defineConfig({
|
|
|
218
234
|
// some.test.ts
|
|
219
235
|
import 'vitest-browser-react' // do this in your setup file to get `page.render`
|
|
220
236
|
import { page } from '@vitest/browser/context'
|
|
221
|
-
import {
|
|
237
|
+
import { it } from 'vitest'
|
|
222
238
|
|
|
223
|
-
it('manual snapshot', async () => {
|
|
239
|
+
it('manual snapshot', async ({ expect }) => {
|
|
224
240
|
page.render(<div data-testid="subject">hello world</div>)
|
|
225
241
|
await expect(document.body).toMatchImageSnapshot(/* options */)
|
|
226
242
|
// or
|
|
@@ -235,9 +251,9 @@ You can customize the snapshot comparison options per assertion:
|
|
|
235
251
|
// some.test.ts
|
|
236
252
|
import 'vitest-browser-react' // do this in your setup file to get `page.render`
|
|
237
253
|
import { page } from '@vitest/browser/context'
|
|
238
|
-
import {
|
|
254
|
+
import { it } from 'vitest'
|
|
239
255
|
|
|
240
|
-
it('manual snapshot with options', async () => {
|
|
256
|
+
it('manual snapshot with options', async ({ expect }) => {
|
|
241
257
|
page.render(<div data-testid="subject">hello world</div>)
|
|
242
258
|
const subject = page.getByTestId('subject')
|
|
243
259
|
await expect(subject).toMatchImageSnapshot({
|
|
@@ -258,9 +274,9 @@ While less common, you can also check if a snapshot exists:
|
|
|
258
274
|
|
|
259
275
|
```ts
|
|
260
276
|
import { page } from '@vitest/browser/context'
|
|
261
|
-
import {
|
|
277
|
+
import { it } from 'vitest'
|
|
262
278
|
|
|
263
|
-
it('Has Snapshot', async () => {
|
|
279
|
+
it('Has Snapshot', async ({ expect }) => {
|
|
264
280
|
const hasSnapshot = await page.hasImageSnapshot(/* options */)
|
|
265
281
|
if (!hasSnapshot) {
|
|
266
282
|
// do something
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NAME } from '../shared/constants.ts'
|
|
2
2
|
import type { MetaTask, SnapshotMeta } from './snapshot_options.ts'
|
|
3
3
|
|
|
4
|
-
export function getAutoSnapshotOptions(task: MetaTask):
|
|
4
|
+
export function getAutoSnapshotOptions<M extends SnapshotMeta<any> = SnapshotMeta<any>>(task: MetaTask): M | undefined {
|
|
5
5
|
if (!task) return
|
|
6
6
|
|
|
7
7
|
const list: any[] = []
|
|
@@ -6,6 +6,7 @@ import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapsh
|
|
|
6
6
|
export type SnapshotMeta<M extends ComparisonMethod> = ToMatchImageSnapshotOptions<M> &
|
|
7
7
|
AutoSnapshotOptions & {
|
|
8
8
|
enable?: boolean | undefined
|
|
9
|
+
[key: string]: unknown
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
type Suite = { meta: Record<string, any>; suite?: Suite | undefined }
|
package/src/setup/create_vis.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import dedent from 'dedent'
|
|
2
2
|
import { afterEach, beforeAll } from 'vitest'
|
|
3
|
-
import { toTaskId } from '../client.ts'
|
|
3
|
+
import { type SnapshotMeta, toTaskId } from '../client.ts'
|
|
4
4
|
import { ctx } from '../client/ctx.ts'
|
|
5
5
|
import { shouldTakeSnapshot } from '../client/should_take_snapshot.ts'
|
|
6
6
|
import { getAutoSnapshotOptions } from '../client/snapshot_options.internal.js'
|
|
7
7
|
import type { SetupVisSuiteCommand } from '../server/commands/setup_vis_suite.ts'
|
|
8
8
|
|
|
9
|
-
export function createVis(commands: SetupVisSuiteCommand) {
|
|
9
|
+
export function createVis<M extends SnapshotMeta<any>>(commands: SetupVisSuiteCommand) {
|
|
10
10
|
let subjectDataTestId: string | undefined
|
|
11
11
|
/**
|
|
12
12
|
* Visual test configuration on the client side.
|
|
@@ -43,7 +43,7 @@ export function createVis(commands: SetupVisSuiteCommand) {
|
|
|
43
43
|
* })
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
|
-
theme(themes: Record<string, () =>
|
|
46
|
+
theme(themes: Record<string, (options: M) => Promise<boolean> | Promise<void> | boolean | void>) {
|
|
47
47
|
beforeAll(vis.beforeAll.setup)
|
|
48
48
|
afterEach(vis.afterEach.matchPerTheme(themes))
|
|
49
49
|
},
|
|
@@ -64,17 +64,19 @@ export function createVis(commands: SetupVisSuiteCommand) {
|
|
|
64
64
|
|
|
65
65
|
await test!.context.expect(getSubject(meta?.subjectDataTestId ?? subjectDataTestId)).toMatchImageSnapshot(meta)
|
|
66
66
|
},
|
|
67
|
-
matchPerTheme(themes: Record<string, () => Promise<void> | void>) {
|
|
67
|
+
matchPerTheme(themes: Record<string, (options: M) => Promise<boolean> | Promise<void> | boolean | void>) {
|
|
68
68
|
return async function matchImageSnapshot() {
|
|
69
69
|
const test = ctx.getCurrentTest()
|
|
70
70
|
if ((test?.result?.errors?.length ?? 0) > 0) return
|
|
71
71
|
|
|
72
|
-
const meta = getAutoSnapshotOptions(test)
|
|
72
|
+
const meta = getAutoSnapshotOptions<M>(test)
|
|
73
73
|
if (!shouldTakeSnapshot(meta)) return
|
|
74
74
|
const errors: any[] = []
|
|
75
75
|
for (const themeId in themes) {
|
|
76
76
|
try {
|
|
77
|
-
await new Promise((a) => setTimeout(
|
|
77
|
+
await new Promise((a) => setTimeout(a, 10))
|
|
78
|
+
const r = await themes[themeId]!(meta!)
|
|
79
|
+
if (r === false) continue
|
|
78
80
|
await test!.context
|
|
79
81
|
.expect(getSubject(meta?.subjectDataTestId ?? subjectDataTestId))
|
|
80
82
|
.toMatchImageSnapshot({
|