vitest-plugin-vis 1.3.2 → 1.4.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/expect/to_match_image_snapshot.types.d.ts +4 -4
- package/esm/client/expect/to_match_image_snapshot.types.d.ts.map +1 -1
- package/esm/client/image_snapshot_matcher.d.ts +1 -1
- package/esm/client/image_snapshot_matcher.d.ts.map +1 -1
- package/esm/client/image_snapshot_matcher.logic.d.ts +1 -1
- package/esm/client/image_snapshot_matcher.logic.d.ts.map +1 -1
- package/esm/client/should_take_snapshot.d.ts +1 -1
- package/esm/client/should_take_snapshot.d.ts.map +1 -1
- package/esm/client/snapshot_options.d.ts +4 -3
- 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/config/types.d.ts +4 -2
- package/esm/config/types.d.ts.map +1 -1
- package/esm/config/vis.d.ts +2 -1
- package/esm/config/vis.d.ts.map +1 -1
- package/esm/server/commands/match_image_snapshot.d.ts +2 -2
- package/esm/server/commands/match_image_snapshot.d.ts.map +1 -1
- package/esm/server/snapshot_path.d.ts +2 -2
- package/esm/server/snapshot_path.d.ts.map +1 -1
- package/esm/server/vis_context.d.ts +2 -2
- package/esm/server/vis_context.d.ts.map +1 -1
- package/esm/server/vis_context.logic.d.ts +4 -4
- package/esm/server/vis_context.logic.d.ts.map +1 -1
- package/esm/shared/compare_image.d.ts +2 -2
- package/esm/shared/compare_image.d.ts.map +1 -1
- package/esm/shared/compare_image.js +36 -4
- package/esm/shared/types.d.ts +23 -2
- package/esm/shared/types.d.ts.map +1 -1
- package/package.json +2 -1
- package/readme.md +5 -3
- package/src/client/expect/to_match_image_snapshot.types.ts +11 -11
- package/src/client/image_snapshot_matcher.logic.ts +1 -1
- package/src/client/image_snapshot_matcher.ts +2 -2
- package/src/client/should_take_snapshot.ts +1 -1
- package/src/client/snapshot_options.internal.ts +1 -1
- package/src/client/snapshot_options.ts +14 -10
- package/src/config/types.ts +4 -1
- package/src/config/vis.ts +2 -1
- package/src/server/commands/match_image_snapshot.ts +8 -9
- package/src/server/snapshot_path.ts +2 -2
- package/src/server/vis_context.logic.ts +5 -5
- package/src/shared/compare_image.ts +55 -6
- package/src/shared/types.ts +42 -18
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type { ImageSnapshotCompareOptions, ImageSnapshotIdOptions, ImageSnapshotTimeoutOptions } from '../../shared/types.ts';
|
|
1
|
+
import type { ComparisonMethod, ImageSnapshotCompareOptions, ImageSnapshotIdOptions, ImageSnapshotTimeoutOptions } from '../../shared/types.ts';
|
|
2
2
|
export interface ImageSnapshotMatcher {
|
|
3
|
-
toMatchImageSnapshot(options?: ToMatchImageSnapshotOptions | undefined): Promise<void>;
|
|
3
|
+
toMatchImageSnapshot<M extends ComparisonMethod>(options?: ToMatchImageSnapshotOptions<M> | undefined): Promise<void>;
|
|
4
4
|
}
|
|
5
|
-
export
|
|
5
|
+
export type ToMatchImageSnapshotOptions<M extends ComparisonMethod = 'pixel'> = ImageSnapshotTimeoutOptions & ImageSnapshotIdOptions & ImageSnapshotCompareOptions<M> & {
|
|
6
6
|
/**
|
|
7
7
|
* Expect the matcher to fail.
|
|
8
8
|
* If it passes, it will throw an error with details.
|
|
9
9
|
*/
|
|
10
10
|
expectToFail?: boolean | undefined;
|
|
11
|
-
}
|
|
11
|
+
};
|
|
12
12
|
//# sourceMappingURL=to_match_image_snapshot.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to_match_image_snapshot.types.d.ts","sourceRoot":"","sources":["../../../src/client/expect/to_match_image_snapshot.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,2BAA2B,EAC3B,sBAAsB,EACtB,2BAA2B,EAC3B,MAAM,uBAAuB,CAAA;AAE9B,MAAM,WAAW,oBAAoB;IACpC,oBAAoB,CAAC,OAAO,CAAC,EAAE,2BAA2B,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"to_match_image_snapshot.types.d.ts","sourceRoot":"","sources":["../../../src/client/expect/to_match_image_snapshot.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,gBAAgB,EAChB,2BAA2B,EAC3B,sBAAsB,EACtB,2BAA2B,EAC3B,MAAM,uBAAuB,CAAA;AAE9B,MAAM,WAAW,oBAAoB;IACpC,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,EAAE,OAAO,CAAC,EAAE,2BAA2B,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACrH;AAED,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,gBAAgB,GAAG,OAAO,IAAI,2BAA2B,GAC1G,sBAAsB,GACtB,2BAA2B,CAAC,CAAC,CAAC,GAAG;IAChC;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAClC,CAAA"}
|
|
@@ -3,7 +3,7 @@ import type { ImageSnapshotNextIndexCommand } from '../commands.ts';
|
|
|
3
3
|
import type { PrepareImageSnapshotComparisonCommand } from '../server/commands/prepare_image_snapshot_comparison.ts';
|
|
4
4
|
import type { WriteImageSnapshotCommand } from '../server/commands/write_image_snapshot.ts';
|
|
5
5
|
import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapshot.types.ts';
|
|
6
|
-
export declare function imageSnapshotMatcher(commands: PrepareImageSnapshotComparisonCommand & WriteImageSnapshotCommand & ImageSnapshotNextIndexCommand): (test: Task, subject: any, options?: ToMatchImageSnapshotOptions) => Promise<void>;
|
|
6
|
+
export declare function imageSnapshotMatcher(commands: PrepareImageSnapshotComparisonCommand & WriteImageSnapshotCommand & ImageSnapshotNextIndexCommand): (test: Task, subject: any, options?: ToMatchImageSnapshotOptions<any>) => Promise<void>;
|
|
7
7
|
/**
|
|
8
8
|
* @deprecated internalized. Use `matchImageSnapshot` directly instead.
|
|
9
9
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image_snapshot_matcher.d.ts","sourceRoot":"","sources":["../../src/client/image_snapshot_matcher.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAA;AACnE,OAAO,KAAK,EAAE,qCAAqC,EAAE,MAAM,yDAAyD,CAAA;AACpH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAA;AAI3F,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAO5F,wBAAgB,oBAAoB,CACnC,QAAQ,EAAE,qCAAqC,GAAG,yBAAyB,GAAG,6BAA6B,UAE5D,IAAI,WAAW,GAAG,YAAY,2BAA2B,
|
|
1
|
+
{"version":3,"file":"image_snapshot_matcher.d.ts","sourceRoot":"","sources":["../../src/client/image_snapshot_matcher.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAA;AACnE,OAAO,KAAK,EAAE,qCAAqC,EAAE,MAAM,yDAAyD,CAAA;AACpH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAA;AAI3F,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAO5F,wBAAgB,oBAAoB,CACnC,QAAQ,EAAE,qCAAqC,GAAG,yBAAyB,GAAG,6BAA6B,UAE5D,IAAI,WAAW,GAAG,YAAY,2BAA2B,CAAC,GAAG,CAAC,mBA8D7G;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,GAAG,OAOrD"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapshot.types.ts';
|
|
2
|
-
export declare function prettifyOptions(options: ToMatchImageSnapshotOptions | undefined): string;
|
|
2
|
+
export declare function prettifyOptions(options: ToMatchImageSnapshotOptions<any> | undefined): string;
|
|
3
3
|
//# sourceMappingURL=image_snapshot_matcher.logic.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image_snapshot_matcher.logic.d.ts","sourceRoot":"","sources":["../../src/client/image_snapshot_matcher.logic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAE5F,wBAAgB,eAAe,CAAC,OAAO,EAAE,2BAA2B,GAAG,SAAS,
|
|
1
|
+
{"version":3,"file":"image_snapshot_matcher.logic.d.ts","sourceRoot":"","sources":["../../src/client/image_snapshot_matcher.logic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAE5F,wBAAgB,eAAe,CAAC,OAAO,EAAE,2BAA2B,CAAC,GAAG,CAAC,GAAG,SAAS,UAUpF"}
|
|
@@ -6,5 +6,5 @@ import type { SnapshotMeta } from './snapshot_options.ts';
|
|
|
6
6
|
* no `snapshot` tag: false
|
|
7
7
|
* disabled by `!snapshot` tag: false
|
|
8
8
|
*/
|
|
9
|
-
export declare function shouldTakeSnapshot(meta?: SnapshotMeta | undefined): boolean;
|
|
9
|
+
export declare function shouldTakeSnapshot(meta?: SnapshotMeta<any> | undefined): boolean;
|
|
10
10
|
//# sourceMappingURL=should_take_snapshot.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"should_take_snapshot.d.ts","sourceRoot":"","sources":["../../src/client/should_take_snapshot.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAEzD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,CAAC,EAAE,YAAY,GAAG,SAAS,
|
|
1
|
+
{"version":3,"file":"should_take_snapshot.d.ts","sourceRoot":"","sources":["../../src/client/should_take_snapshot.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAEzD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,GAAG,SAAS,WAGtE"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { ComparisonMethod } from '../shared/types.ts';
|
|
1
2
|
import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapshot.types.ts';
|
|
2
|
-
export type SnapshotMeta = ToMatchImageSnapshotOptions & {
|
|
3
|
+
export type SnapshotMeta<M extends ComparisonMethod> = ToMatchImageSnapshotOptions<M> & {
|
|
3
4
|
enable?: boolean | undefined;
|
|
4
5
|
};
|
|
5
6
|
type Suite = {
|
|
@@ -28,7 +29,7 @@ export type MetaTask = {
|
|
|
28
29
|
* @param task Optional. Suite or task to set the options.
|
|
29
30
|
* If not provided, it will set the options for the current test.
|
|
30
31
|
*/
|
|
31
|
-
export declare function setAutoSnapshotOptions(task: MetaTask, meta: SnapshotMeta | boolean): void;
|
|
32
|
-
export declare function setAutoSnapshotOptions(meta: SnapshotMeta | boolean): void;
|
|
32
|
+
export declare function setAutoSnapshotOptions<M extends ComparisonMethod>(task: MetaTask, meta: SnapshotMeta<M> | boolean): void;
|
|
33
|
+
export declare function setAutoSnapshotOptions<M extends ComparisonMethod>(meta: SnapshotMeta<M> | boolean): void;
|
|
33
34
|
export {};
|
|
34
35
|
//# sourceMappingURL=snapshot_options.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapshot_options.d.ts","sourceRoot":"","sources":["../../src/client/snapshot_options.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"snapshot_options.d.ts","sourceRoot":"","sources":["../../src/client/snapshot_options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAE1D,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2CAA2C,CAAA;AAE5F,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,gBAAgB,IAAI,2BAA2B,CAAC,CAAC,CAAC,GAAG;IAAE,MAAM,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,CAAA;AAExH,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): SnapshotMeta | undefined;
|
|
2
|
+
export declare function getAutoSnapshotOptions(task: MetaTask): SnapshotMeta<any> | 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,IAAI,EAAE,QAAQ,GAAG,YAAY,GAAG,SAAS,
|
|
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,IAAI,EAAE,QAAQ,GAAG,YAAY,CAAC,GAAG,CAAC,GAAG,SAAS,CAkBpF"}
|
package/esm/config/types.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { ToMatchImageSnapshotOptions } from '../client/expect/to_match_image_snapshot.types.ts';
|
|
2
|
-
|
|
2
|
+
import type { ComparisonMethod } from '../shared/types.ts';
|
|
3
|
+
export type { ComparisonMethod };
|
|
4
|
+
export type VisOptions<M extends ComparisonMethod = 'pixel'> = ToMatchImageSnapshotOptions<M> & {
|
|
3
5
|
/**
|
|
4
6
|
* Loads the `test.setupFiles` of the specified preset.
|
|
5
7
|
*/
|
|
@@ -31,5 +33,5 @@ export interface VisOptions extends ToMatchImageSnapshotOptions {
|
|
|
31
33
|
* If you want to keep the full path, you can simply return it.
|
|
32
34
|
*/
|
|
33
35
|
customizeSnapshotSubpath?: (subPath: string) => string;
|
|
34
|
-
}
|
|
36
|
+
};
|
|
35
37
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAA;AACpG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAE1D,YAAY,EAAE,gBAAgB,EAAE,CAAA;AAEhC,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,gBAAgB,GAAG,OAAO,IAAI,2BAA2B,CAAC,CAAC,CAAC,GAAG;IAC/F;;OAEG;IACH,MAAM,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;IAC/C;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,wBAAwB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAA;CACtD,CAAA"}
|
package/esm/config/vis.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ComparisonMethod } from '../shared/types.ts';
|
|
1
2
|
import type { VisOptions } from './types.ts';
|
|
2
3
|
/**
|
|
3
4
|
* Create a Vite plugin for visual testing.
|
|
@@ -5,7 +6,7 @@ import type { VisOptions } from './types.ts';
|
|
|
5
6
|
* If options are not provided, the plugin will use the default options,
|
|
6
7
|
* which enables the `auto` preset.
|
|
7
8
|
*/
|
|
8
|
-
export declare function vis(options?: VisOptions): {
|
|
9
|
+
export declare function vis<M extends ComparisonMethod = 'pixel'>(options?: VisOptions<M>): {
|
|
9
10
|
name: string;
|
|
10
11
|
config(this: void): {
|
|
11
12
|
test: {
|
package/esm/config/vis.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vis.d.ts","sourceRoot":"","sources":["../../src/config/vis.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,OAAO,GAAE,
|
|
1
|
+
{"version":3,"file":"vis.d.ts","sourceRoot":"","sources":["../../src/config/vis.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,gBAAgB,GAAG,OAAO,EAAE,OAAO,GAAE,UAAU,CAAC,CAAC,CAA6B;;;;;sBASxE,MAAM;;;;;;;;;;;;;EAQzC"}
|
|
@@ -3,12 +3,12 @@ import type { ImageSnapshotCompareOptions, ImageSnapshotIdOptions, ImageSnapshot
|
|
|
3
3
|
export interface MatchImageSnapshotCommand {
|
|
4
4
|
matchImageSnapshot: (taskId: string | undefined, subject: string, options?: MatchImageSnapshotOptions | undefined) => Promise<void>;
|
|
5
5
|
}
|
|
6
|
-
export
|
|
6
|
+
export type MatchImageSnapshotOptions = ImageSnapshotTimeoutOptions & ImageSnapshotIdOptions & ImageSnapshotCompareOptions<any> & {
|
|
7
7
|
/**
|
|
8
8
|
* The snapshot file id calculated on the client side.
|
|
9
9
|
*/
|
|
10
10
|
snapshotFileId?: string | undefined;
|
|
11
|
-
}
|
|
11
|
+
};
|
|
12
12
|
export declare const matchImageSnapshot: BrowserCommand<[
|
|
13
13
|
taskId: string,
|
|
14
14
|
subject: string,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"match_image_snapshot.d.ts","sourceRoot":"","sources":["../../../src/server/commands/match_image_snapshot.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAKjD,OAAO,KAAK,EACX,2BAA2B,EAC3B,sBAAsB,EACtB,2BAA2B,EAC3B,MAAM,uBAAuB,CAAA;AAM9B,MAAM,WAAW,yBAAyB;IACzC,kBAAkB,EAAE,CACnB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS,KAC3C,OAAO,CAAC,IAAI,CAAC,CAAA;CAClB;AAED,MAAM,
|
|
1
|
+
{"version":3,"file":"match_image_snapshot.d.ts","sourceRoot":"","sources":["../../../src/server/commands/match_image_snapshot.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAKjD,OAAO,KAAK,EACX,2BAA2B,EAC3B,sBAAsB,EACtB,2BAA2B,EAC3B,MAAM,uBAAuB,CAAA;AAM9B,MAAM,WAAW,yBAAyB;IACzC,kBAAkB,EAAE,CACnB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS,KAC3C,OAAO,CAAC,IAAI,CAAC,CAAA;CAClB;AAED,MAAM,MAAM,yBAAyB,GAAG,2BAA2B,GAClE,sBAAsB,GACtB,2BAA2B,CAAC,GAAG,CAAC,GAAG;IAClC;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACnC,CAAA;AAEF,eAAO,MAAM,kBAAkB,EAAE,cAAc,CAC9C;IAAC,MAAM,EAAE,MAAM;IAAE,OAAO,EAAE,MAAM;IAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,SAAS;CAAC,CAuElF,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { VisOptions } from '../config/types.ts';
|
|
2
|
-
export declare function resolveSnapshotRootDir(options: VisOptions | undefined): string;
|
|
3
|
-
export declare function getSnapshotSubpath(suiteName: string, options: VisOptions): string;
|
|
2
|
+
export declare function resolveSnapshotRootDir(options: Pick<VisOptions, 'snapshotRootDir'> | undefined): string;
|
|
3
|
+
export declare function getSnapshotSubpath(suiteName: string, options: Pick<VisOptions, 'customizeSnapshotSubpath'>): string;
|
|
4
4
|
//# sourceMappingURL=snapshot_path.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snapshot_path.d.ts","sourceRoot":"","sources":["../../src/server/snapshot_path.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAIpD,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,UAAU,GAAG,SAAS,
|
|
1
|
+
{"version":3,"file":"snapshot_path.d.ts","sourceRoot":"","sources":["../../src/server/snapshot_path.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAIpD,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,iBAAiB,CAAC,GAAG,SAAS,UAE9F;AAED,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,0BAA0B,CAAC,UAG1G"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const visContext: {
|
|
2
|
-
setOptions(options?: import("../config.ts").VisOptions): void;
|
|
3
|
-
__test__getOptions(): import("../config.ts").VisOptions
|
|
2
|
+
setOptions<M extends "pixel" | "ssim">(options?: import("../config.ts").VisOptions<M>): void;
|
|
3
|
+
__test__getOptions(): import("../config.ts").VisOptions<any>;
|
|
4
4
|
__test__reset(): void;
|
|
5
5
|
__test__getState(): import("./vis_context.types.ts").VisState;
|
|
6
6
|
setupSuite(context: import("./vis_context.logic.ts").PartialBrowserCommandContext): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vis_context.d.ts","sourceRoot":"","sources":["../../src/server/vis_context.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU;;;;;;;
|
|
1
|
+
{"version":3,"file":"vis_context.d.ts","sourceRoot":"","sources":["../../src/server/vis_context.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU;;;;;;;sBAC0kD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;CADtjD,CAAA"}
|
|
@@ -14,8 +14,8 @@ export type PartialBrowserCommandContext = {
|
|
|
14
14
|
testPath: string;
|
|
15
15
|
};
|
|
16
16
|
export declare function createVisContext(): {
|
|
17
|
-
setOptions(options?: VisOptions): void;
|
|
18
|
-
__test__getOptions(): VisOptions
|
|
17
|
+
setOptions<M extends "pixel" | "ssim">(options?: VisOptions<M>): void;
|
|
18
|
+
__test__getOptions(): VisOptions<any>;
|
|
19
19
|
__test__reset(): void;
|
|
20
20
|
__test__getState(): VisState;
|
|
21
21
|
/**
|
|
@@ -50,7 +50,7 @@ export declare function createVisContext(): {
|
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
52
|
};
|
|
53
|
-
export declare function createSuite(state: VisState, testPath: string, options: VisOptions): {
|
|
53
|
+
export declare function createSuite(state: VisState, testPath: string, options: Pick<VisOptions, 'customizeSnapshotSubpath'>): {
|
|
54
54
|
suiteId: string;
|
|
55
55
|
suite: {
|
|
56
56
|
baselineDir: string;
|
|
@@ -59,5 +59,5 @@ export declare function createSuite(state: VisState, testPath: string, options:
|
|
|
59
59
|
tasks: {};
|
|
60
60
|
};
|
|
61
61
|
};
|
|
62
|
-
export declare function getSuiteId(state: VisState, testPath: string, options: VisOptions): string;
|
|
62
|
+
export declare function getSuiteId(state: VisState, testPath: string, options: Pick<VisOptions, 'customizeSnapshotSubpath'>): string;
|
|
63
63
|
//# sourceMappingURL=vis_context.logic.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vis_context.logic.d.ts","sourceRoot":"","sources":["../../src/server/vis_context.logic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAKpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AAEtD,MAAM,MAAM,4BAA4B,GAAG;IAC1C,OAAO,EAAE;QACR,MAAM,EAAE;YACP,IAAI,EAAE,MAAM,CAAA;YACZ,eAAe,EAAE;gBAChB,cAAc,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAA;aACtC,CAAA;YACD,WAAW,EAAE,MAAM,CAAA;YACnB,WAAW,EAAE,MAAM,CAAA;SACnB,CAAA;KACD,CAAA;IACD,QAAQ,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,wBAAgB,gBAAgB;
|
|
1
|
+
{"version":3,"file":"vis_context.logic.d.ts","sourceRoot":"","sources":["../../src/server/vis_context.logic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAKpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AAEtD,MAAM,MAAM,4BAA4B,GAAG;IAC1C,OAAO,EAAE;QACR,MAAM,EAAE;YACP,IAAI,EAAE,MAAM,CAAA;YACZ,eAAe,EAAE;gBAChB,cAAc,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAA;aACtC,CAAA;YACD,WAAW,EAAE,MAAM,CAAA;YACnB,WAAW,EAAE,MAAM,CAAA;SACnB,CAAA;KACD,CAAA;IACD,QAAQ,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,wBAAgB,gBAAgB;eAMnB,CAAC,SAAS,OAAO,GAAG,MAAM,YAAW,UAAU,CAAC,CAAC,CAAC;;;;IAY7D;;;;OAIG;wBACuB,4BAA4B;8BAY5B,MAAM,QAAQ,MAAM,YAAY;QAAE,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE;;;;;2BAiB1E,MAAM,UAAU,MAAM;+BAGlB,MAAM,UAAU,MAAM,kBAAkB,MAAM,GAAG,SAAS;8BAI3D;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE;YAAE,KAAK,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,kBAAkB,MAAM,GAAG,SAAS;2BAMlF,MAAM,UAAU,MAAM;;;;;;;;;;EAe9C;AAuBD,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,0BAA0B,CAAC;;;;;;;;EAWnH;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,0BAA0B,CAAC,UAElH"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ImageSnapshotCompareOptions } from './types.ts';
|
|
2
|
-
export declare function compareImage(img1: Uint8ClampedArray | Buffer, img2: Uint8ClampedArray | Buffer, output: Uint8ClampedArray | Buffer, width: number, height: number,
|
|
1
|
+
import type { ComparisonMethod, ImageSnapshotCompareOptions } from './types.ts';
|
|
2
|
+
export declare function compareImage<M extends ComparisonMethod>(img1: Uint8ClampedArray | Buffer, img2: Uint8ClampedArray | Buffer, output: Uint8ClampedArray | Buffer, width: number, height: number, options?: ImageSnapshotCompareOptions<M>): {
|
|
3
3
|
pass: boolean;
|
|
4
4
|
diffAmount: number;
|
|
5
5
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare_image.d.ts","sourceRoot":"","sources":["../../src/shared/compare_image.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compare_image.d.ts","sourceRoot":"","sources":["../../src/shared/compare_image.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAA;AAE/E,wBAAgB,YAAY,CAAC,CAAC,SAAS,gBAAgB,EACtD,IAAI,EAAE,iBAAiB,GAAG,MAAM,EAChC,IAAI,EAAE,iBAAiB,GAAG,MAAM,EAChC,MAAM,EAAE,iBAAiB,GAAG,MAAM,EAClC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,2BAA2B,CAAC,CAAC,CAAa;;;EAenD"}
|
|
@@ -1,10 +1,42 @@
|
|
|
1
1
|
import pixelmatch from 'pixelmatch';
|
|
2
|
+
import { ssim } from 'ssim.js';
|
|
2
3
|
import { convertThresholdUnit } from "./convert_threshold_unit.js";
|
|
3
|
-
export function compareImage(img1, img2, output, width, height,
|
|
4
|
-
const pixelDiff =
|
|
5
|
-
|
|
4
|
+
export function compareImage(img1, img2, output, width, height, options = {}) {
|
|
5
|
+
const pixelDiff = options.comparisonMethod === 'ssim'
|
|
6
|
+
? compareWithSsim(img1, img2, output, width, height, options.diffOptions)
|
|
7
|
+
: pixelmatch(img1, img2, output, width, height, options.diffOptions);
|
|
8
|
+
const diffAmount = convertThresholdUnit({ failureThresholdType: options.failureThresholdType ?? 'pixel', width, height }, pixelDiff);
|
|
6
9
|
return {
|
|
7
|
-
pass: diffAmount <= failureThreshold,
|
|
10
|
+
pass: diffAmount <= (options.failureThreshold ?? 0),
|
|
8
11
|
diffAmount,
|
|
9
12
|
};
|
|
10
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* @author This code is based on `jest-image-snapshot` implementation.
|
|
16
|
+
*/
|
|
17
|
+
function compareWithSsim(img1, img2, output, width, height, diffOptions) {
|
|
18
|
+
const newImage = { data: img1, width: width, height: height };
|
|
19
|
+
const baselineImage = { data: img2, width: width, height: height };
|
|
20
|
+
const { ssim_map, mssim } = ssim(newImage, baselineImage, {
|
|
21
|
+
ssim: 'bezkrovny',
|
|
22
|
+
...diffOptions,
|
|
23
|
+
});
|
|
24
|
+
// Converts the SSIM value to different pixels based on image width and height
|
|
25
|
+
// conforms to how pixelmatch works.
|
|
26
|
+
const diffPixels = Math.round((1 - mssim) * width * height);
|
|
27
|
+
const diffRgbaPixels = new DataView(output.buffer, output.byteOffset);
|
|
28
|
+
for (let ln = 0; ln !== height; ++ln) {
|
|
29
|
+
for (let pos = 0; pos !== width; ++pos) {
|
|
30
|
+
const rPos = ln * width + pos;
|
|
31
|
+
// initial value is transparent. We'll add in the SSIM offset.
|
|
32
|
+
// red (ff) green (00) blue (00) alpha (00)
|
|
33
|
+
const diffValue = 0xff000000 +
|
|
34
|
+
Math.floor(0xff *
|
|
35
|
+
(1 -
|
|
36
|
+
ssim_map.data[ssim_map.width * Math.round((ssim_map.height * ln) / height) +
|
|
37
|
+
Math.round((ssim_map.width * pos) / width)]));
|
|
38
|
+
diffRgbaPixels.setUint32(rPos * 4, diffValue);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return diffPixels;
|
|
42
|
+
}
|
package/esm/shared/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PixelmatchOptions } from 'pixelmatch';
|
|
2
|
+
import type { Options as SsimOptions } from 'ssim.js';
|
|
2
3
|
export interface ImageSnapshotTimeoutOptions {
|
|
3
4
|
/**
|
|
4
5
|
* Timeout for taking the snapshot.
|
|
@@ -16,7 +17,27 @@ export interface ImageSnapshotIdOptions {
|
|
|
16
17
|
*/
|
|
17
18
|
customizeSnapshotId?: (id: string, index: number) => string;
|
|
18
19
|
}
|
|
19
|
-
export
|
|
20
|
+
export type ComparisonMethod = 'pixel' | 'ssim';
|
|
21
|
+
export type ImageSnapshotCompareOptions<M extends ComparisonMethod = 'pixel'> = M extends 'ssim' ? {
|
|
22
|
+
comparisonMethod: M;
|
|
23
|
+
/**
|
|
24
|
+
* Custom options passed to 'ssim'
|
|
25
|
+
*/
|
|
26
|
+
diffOptions?: Partial<SsimOptions> | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Failure threshold should measure in `pixel` or `percent`.
|
|
29
|
+
*
|
|
30
|
+
* Default is `pixel`.
|
|
31
|
+
*/
|
|
32
|
+
failureThresholdType?: 'pixel' | 'percent' | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Failure tolerance threshold.
|
|
35
|
+
*
|
|
36
|
+
* Default is `0`.
|
|
37
|
+
*/
|
|
38
|
+
failureThreshold?: number | undefined;
|
|
39
|
+
} : {
|
|
40
|
+
comparisonMethod?: M | undefined;
|
|
20
41
|
/**
|
|
21
42
|
* Custom options passed to 'pixelmatch'
|
|
22
43
|
*/
|
|
@@ -33,5 +54,5 @@ export interface ImageSnapshotCompareOptions {
|
|
|
33
54
|
* Default is `0`.
|
|
34
55
|
*/
|
|
35
56
|
failureThreshold?: number | undefined;
|
|
36
|
-
}
|
|
57
|
+
};
|
|
37
58
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,KAAK,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,SAAS,CAAA;AAErD,MAAM,WAAW,2BAA2B;IAC3C;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC5B;AAED,MAAM,WAAW,sBAAsB;IACtC;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAA;CAC3D;AAED,MAAM,MAAM,gBAAgB,GAAG,OAAO,GAAG,MAAM,CAAA;AAE/C,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,gBAAgB,GAAG,OAAO,IAAI,CAAC,SAAS,MAAM,GAC7F;IACA,gBAAgB,EAAE,CAAC,CAAA;IACnB;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,SAAS,CAAA;IAC9C;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,CAAA;IACtD;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACrC,GACA;IACA,gBAAgB,CAAC,EAAE,CAAC,GAAG,SAAS,CAAA;IAChC;;OAEG;IACH,WAAW,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAA;IAC3C;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,CAAA;IACtD;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACrC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-plugin-vis",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Vitest visual testing plugin",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"image-snapshot",
|
|
@@ -68,6 +68,7 @@
|
|
|
68
68
|
"pixelmatch": "^6.0.0",
|
|
69
69
|
"pngjs": "^7.0.0",
|
|
70
70
|
"rimraf": "^6.0.1",
|
|
71
|
+
"ssim.js": "^3.5.0",
|
|
71
72
|
"type-plus": "^8.0.0-beta.6"
|
|
72
73
|
},
|
|
73
74
|
"devDependencies": {
|
package/readme.md
CHANGED
|
@@ -41,6 +41,7 @@ export default defineConfig({
|
|
|
41
41
|
|
|
42
42
|
This default configuration will:
|
|
43
43
|
|
|
44
|
+
- Use `pixelmatch` as the image comparison method.
|
|
44
45
|
- Use the `auto` preset, taking image snapshot at the end on each rendering test.
|
|
45
46
|
- Set config to compare image snapshot with a failure threshold of `0 pixels`.
|
|
46
47
|
- Local (non-CI) image snapshots are saved in the `<root>/__vis__/local` directory.
|
|
@@ -59,6 +60,7 @@ export default defineConfig({
|
|
|
59
60
|
plugins: [
|
|
60
61
|
vis({
|
|
61
62
|
preset: 'auto',
|
|
63
|
+
comparisonMethod: 'pixel',
|
|
62
64
|
snapshotRootDir: '__vis__',
|
|
63
65
|
customizeSnapshotSubpath: (subpath) => trimCommonFolder(subpath),
|
|
64
66
|
customizeSnapshotId: (id, index) => `${id}-${index}`,
|
|
@@ -181,8 +183,10 @@ setAutoSnapshotOptions(true /* or false */)
|
|
|
181
183
|
// detailed options
|
|
182
184
|
setAutoSnapshotOptions({
|
|
183
185
|
enable: true,
|
|
186
|
+
comparisonMethod: 'pixel',
|
|
184
187
|
customizeSnapshotId: (id, index) => `${id}-custom-${index}`,
|
|
185
|
-
|
|
188
|
+
// pixelmatch or ssim.js options, depending on `comparisonMethod`.
|
|
189
|
+
diffOptions: { threshold: 0.01 },
|
|
186
190
|
failureThreshold: 0.01,
|
|
187
191
|
failureThresholdType: 'percent',
|
|
188
192
|
timeout: 15000
|
|
@@ -296,8 +300,6 @@ It is recommended to run your tests serially to avoid flakiness.
|
|
|
296
300
|
|
|
297
301
|
Some features in [`jest-image-snapshot`][jest-image-snapshot] are not implemented in [`vitest-plugin-vis`][vitest-plugin-vis] yet.
|
|
298
302
|
This is because through our own usage, we do not found a good use case for them.
|
|
299
|
-
For example, the `ssim` comparison method is not implemented in [`vitest-plugin-vis`][vitest-plugin-vis],
|
|
300
|
-
because we found that figuring out what is the delta using `ssim` is quite difficult.
|
|
301
303
|
|
|
302
304
|
If you have a good use case for these features, please open an issue or PR.
|
|
303
305
|
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
ComparisonMethod,
|
|
2
3
|
ImageSnapshotCompareOptions,
|
|
3
4
|
ImageSnapshotIdOptions,
|
|
4
5
|
ImageSnapshotTimeoutOptions,
|
|
5
6
|
} from '../../shared/types.ts'
|
|
6
7
|
|
|
7
8
|
export interface ImageSnapshotMatcher {
|
|
8
|
-
toMatchImageSnapshot(options?: ToMatchImageSnapshotOptions | undefined): Promise<void>
|
|
9
|
+
toMatchImageSnapshot<M extends ComparisonMethod>(options?: ToMatchImageSnapshotOptions<M> | undefined): Promise<void>
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
12
|
+
export type ToMatchImageSnapshotOptions<M extends ComparisonMethod = 'pixel'> = ImageSnapshotTimeoutOptions &
|
|
13
|
+
ImageSnapshotIdOptions &
|
|
14
|
+
ImageSnapshotCompareOptions<M> & {
|
|
15
|
+
/**
|
|
16
|
+
* Expect the matcher to fail.
|
|
17
|
+
* If it passes, it will throw an error with details.
|
|
18
|
+
*/
|
|
19
|
+
expectToFail?: boolean | undefined
|
|
20
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapshot.types.ts'
|
|
2
2
|
|
|
3
|
-
export function prettifyOptions(options: ToMatchImageSnapshotOptions | undefined) {
|
|
3
|
+
export function prettifyOptions(options: ToMatchImageSnapshotOptions<any> | undefined) {
|
|
4
4
|
if (!options) return 'none'
|
|
5
5
|
|
|
6
6
|
return [
|
|
@@ -17,7 +17,7 @@ import { server } from './vitest_browser_context_proxy.ts'
|
|
|
17
17
|
export function imageSnapshotMatcher(
|
|
18
18
|
commands: PrepareImageSnapshotComparisonCommand & WriteImageSnapshotCommand & ImageSnapshotNextIndexCommand,
|
|
19
19
|
) {
|
|
20
|
-
return async function matchImageSnapshot(test: Task, subject: any, options?: ToMatchImageSnapshotOptions) {
|
|
20
|
+
return async function matchImageSnapshot(test: Task, subject: any, options?: ToMatchImageSnapshotOptions<any>) {
|
|
21
21
|
const taskId = toTaskId(test)
|
|
22
22
|
const info = await commands.prepareImageSnapshotComparison(
|
|
23
23
|
taskId,
|
|
@@ -101,7 +101,7 @@ async function writeSnapshot(commands: WriteImageSnapshotCommand, path: string,
|
|
|
101
101
|
async function parseImageSnapshotOptions(
|
|
102
102
|
commands: ImageSnapshotNextIndexCommand,
|
|
103
103
|
taskId: string,
|
|
104
|
-
options: ToMatchImageSnapshotOptions
|
|
104
|
+
options: ToMatchImageSnapshotOptions<any>,
|
|
105
105
|
) {
|
|
106
106
|
const index = await commands.imageSnapshotNextIndex(taskId)
|
|
107
107
|
const { customizeSnapshotId, ...rest } = options
|
|
@@ -9,7 +9,7 @@ import type { SnapshotMeta } from './snapshot_options.ts'
|
|
|
9
9
|
* no `snapshot` tag: false
|
|
10
10
|
* disabled by `!snapshot` tag: false
|
|
11
11
|
*/
|
|
12
|
-
export function shouldTakeSnapshot(meta?: SnapshotMeta | undefined) {
|
|
12
|
+
export function shouldTakeSnapshot(meta?: SnapshotMeta<any> | undefined) {
|
|
13
13
|
const m = meta ?? getAutoSnapshotOptions(ctx.getCurrentTest())
|
|
14
14
|
return document.body.childElementCount > 0 && m?.enable !== false
|
|
15
15
|
}
|
|
@@ -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): SnapshotMeta | undefined {
|
|
4
|
+
export function getAutoSnapshotOptions(task: MetaTask): SnapshotMeta<any> | undefined {
|
|
5
5
|
if (!task) return
|
|
6
6
|
|
|
7
7
|
const list: any[] = []
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { NAME } from '../shared/constants.ts'
|
|
2
|
+
import type { ComparisonMethod } from '../shared/types.ts'
|
|
2
3
|
import { ctx } from './ctx.ts'
|
|
3
4
|
import type { ToMatchImageSnapshotOptions } from './expect/to_match_image_snapshot.types.ts'
|
|
4
5
|
|
|
5
|
-
export type SnapshotMeta = ToMatchImageSnapshotOptions & { enable?: boolean | undefined }
|
|
6
|
+
export type SnapshotMeta<M extends ComparisonMethod> = ToMatchImageSnapshotOptions<M> & { enable?: boolean | undefined }
|
|
6
7
|
|
|
7
8
|
type Suite = { meta: Record<string, any>; suite?: Suite | undefined }
|
|
8
9
|
|
|
@@ -29,10 +30,13 @@ export type MetaTask =
|
|
|
29
30
|
* @param task Optional. Suite or task to set the options.
|
|
30
31
|
* If not provided, it will set the options for the current test.
|
|
31
32
|
*/
|
|
32
|
-
export function setAutoSnapshotOptions
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
export function setAutoSnapshotOptions<M extends ComparisonMethod>(
|
|
34
|
+
task: MetaTask,
|
|
35
|
+
meta: SnapshotMeta<M> | boolean,
|
|
36
|
+
): void
|
|
37
|
+
export function setAutoSnapshotOptions<M extends ComparisonMethod>(meta: SnapshotMeta<M> | boolean): void
|
|
38
|
+
export function setAutoSnapshotOptions<M extends ComparisonMethod>(
|
|
39
|
+
...args: [task: MetaTask, meta: SnapshotMeta<M> | boolean] | [meta: SnapshotMeta<M> | boolean]
|
|
36
40
|
): void {
|
|
37
41
|
const [task, meta] = parseArgs(args)
|
|
38
42
|
if (task)
|
|
@@ -42,12 +46,12 @@ export function setAutoSnapshotOptions(
|
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
function parseArgs(
|
|
46
|
-
args: [task: MetaTask, meta: SnapshotMeta | boolean] | [meta: boolean | SnapshotMeta],
|
|
47
|
-
): [MetaTask | undefined, SnapshotMeta] {
|
|
49
|
+
function parseArgs<M extends ComparisonMethod>(
|
|
50
|
+
args: [task: MetaTask, meta: SnapshotMeta<M> | boolean] | [meta: boolean | SnapshotMeta<M>],
|
|
51
|
+
): [MetaTask | undefined, SnapshotMeta<M>] {
|
|
48
52
|
return args.length === 1 ? [ctx.getCurrentTest(), parseMeta(args[0])] : [args[0], parseMeta(args[1])]
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
function parseMeta(meta: boolean | SnapshotMeta): SnapshotMeta {
|
|
52
|
-
return typeof meta === 'boolean' ? { enable: meta } : { enable: true, ...meta }
|
|
55
|
+
function parseMeta<M extends ComparisonMethod>(meta: boolean | SnapshotMeta<M>): SnapshotMeta<M> {
|
|
56
|
+
return typeof meta === 'boolean' ? ({ enable: meta } as any) : { enable: true, ...meta }
|
|
53
57
|
}
|
package/src/config/types.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { ToMatchImageSnapshotOptions } from '../client/expect/to_match_image_snapshot.types.ts'
|
|
2
|
+
import type { ComparisonMethod } from '../shared/types.ts'
|
|
2
3
|
|
|
3
|
-
export
|
|
4
|
+
export type { ComparisonMethod }
|
|
5
|
+
|
|
6
|
+
export type VisOptions<M extends ComparisonMethod = 'pixel'> = ToMatchImageSnapshotOptions<M> & {
|
|
4
7
|
/**
|
|
5
8
|
* Loads the `test.setupFiles` of the specified preset.
|
|
6
9
|
*/
|
package/src/config/vis.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Plugin } from 'vite'
|
|
|
2
2
|
import { commands } from '../server/commands/extend.ts'
|
|
3
3
|
import { visContext } from '../server/vis_context.ts'
|
|
4
4
|
import { NAME } from '../shared/constants.ts'
|
|
5
|
+
import type { ComparisonMethod } from '../shared/types.ts'
|
|
5
6
|
import type { VisOptions } from './types.ts'
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -10,7 +11,7 @@ import type { VisOptions } from './types.ts'
|
|
|
10
11
|
* If options are not provided, the plugin will use the default options,
|
|
11
12
|
* which enables the `auto` preset.
|
|
12
13
|
*/
|
|
13
|
-
export function vis(options: VisOptions = { preset: 'auto' }) {
|
|
14
|
+
export function vis<M extends ComparisonMethod = 'pixel'>(options: VisOptions<M> = { preset: 'auto' } as any) {
|
|
14
15
|
visContext.setOptions(options)
|
|
15
16
|
const preset = options?.preset
|
|
16
17
|
return {
|
|
@@ -25,15 +25,14 @@ export interface MatchImageSnapshotCommand {
|
|
|
25
25
|
) => Promise<void>
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
28
|
+
export type MatchImageSnapshotOptions = ImageSnapshotTimeoutOptions &
|
|
29
|
+
ImageSnapshotIdOptions &
|
|
30
|
+
ImageSnapshotCompareOptions<any> & {
|
|
31
|
+
/**
|
|
32
|
+
* The snapshot file id calculated on the client side.
|
|
33
|
+
*/
|
|
34
|
+
snapshotFileId?: string | undefined
|
|
35
|
+
}
|
|
37
36
|
|
|
38
37
|
export const matchImageSnapshot: BrowserCommand<
|
|
39
38
|
[taskId: string, subject: string, options?: MatchImageSnapshotOptions | undefined]
|
|
@@ -2,11 +2,11 @@ import type { VisOptions } from '../config/types.ts'
|
|
|
2
2
|
import { SNAPSHOT_ROOT_DIR } from '../shared/constants.ts'
|
|
3
3
|
import { trimCommonFolder } from '../shared/trim_common_folder.ts'
|
|
4
4
|
|
|
5
|
-
export function resolveSnapshotRootDir(options: VisOptions | undefined) {
|
|
5
|
+
export function resolveSnapshotRootDir(options: Pick<VisOptions, 'snapshotRootDir'> | undefined) {
|
|
6
6
|
return options?.snapshotRootDir ?? SNAPSHOT_ROOT_DIR
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export function getSnapshotSubpath(suiteName: string, options: VisOptions) {
|
|
9
|
+
export function getSnapshotSubpath(suiteName: string, options: Pick<VisOptions, 'customizeSnapshotSubpath'>) {
|
|
10
10
|
const customizeSnapshotSubpath = options.customizeSnapshotSubpath ?? trimCommonFolder
|
|
11
11
|
return customizeSnapshotSubpath(suiteName)
|
|
12
12
|
}
|
|
@@ -21,12 +21,12 @@ export type PartialBrowserCommandContext = {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export function createVisContext() {
|
|
24
|
-
let visOptions: VisOptions = {}
|
|
24
|
+
let visOptions: VisOptions<any> = {}
|
|
25
25
|
let state: VisState
|
|
26
26
|
let globalStateReady: Promise<VisState>
|
|
27
27
|
|
|
28
28
|
const context = {
|
|
29
|
-
setOptions(options: VisOptions = {}) {
|
|
29
|
+
setOptions<M extends 'pixel' | 'ssim'>(options: VisOptions<M> = {} as any) {
|
|
30
30
|
visOptions = options
|
|
31
31
|
},
|
|
32
32
|
__test__getOptions() {
|
|
@@ -102,7 +102,7 @@ export function createVisContext() {
|
|
|
102
102
|
return context
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
async function setupState(suite: PartialBrowserCommandContext, visOptions: VisOptions) {
|
|
105
|
+
async function setupState(suite: PartialBrowserCommandContext, visOptions: Pick<VisOptions, 'snapshotRootDir'>) {
|
|
106
106
|
const snapshotRootDir = resolveSnapshotRootDir(visOptions)
|
|
107
107
|
const projectPath = suite.project.config.root
|
|
108
108
|
const platform = ctx.getSnapshotPlatform()
|
|
@@ -123,7 +123,7 @@ async function setupState(suite: PartialBrowserCommandContext, visOptions: VisOp
|
|
|
123
123
|
return state
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
export function createSuite(state: VisState, testPath: string, options: VisOptions) {
|
|
126
|
+
export function createSuite(state: VisState, testPath: string, options: Pick<VisOptions, 'customizeSnapshotSubpath'>) {
|
|
127
127
|
const suiteId = getSuiteId(state, testPath, options)
|
|
128
128
|
return {
|
|
129
129
|
suiteId,
|
|
@@ -136,6 +136,6 @@ export function createSuite(state: VisState, testPath: string, options: VisOptio
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
export function getSuiteId(state: VisState, testPath: string, options: VisOptions) {
|
|
139
|
+
export function getSuiteId(state: VisState, testPath: string, options: Pick<VisOptions, 'customizeSnapshotSubpath'>) {
|
|
140
140
|
return getSnapshotSubpath(relative(state.projectPath, testPath), options)
|
|
141
141
|
}
|
|
@@ -1,20 +1,69 @@
|
|
|
1
1
|
import pixelmatch from 'pixelmatch'
|
|
2
|
+
import { type Options, ssim } from 'ssim.js'
|
|
2
3
|
import { convertThresholdUnit } from './convert_threshold_unit.ts'
|
|
3
|
-
import type { ImageSnapshotCompareOptions } from './types.ts'
|
|
4
|
+
import type { ComparisonMethod, ImageSnapshotCompareOptions } from './types.ts'
|
|
4
5
|
|
|
5
|
-
export function compareImage(
|
|
6
|
+
export function compareImage<M extends ComparisonMethod>(
|
|
6
7
|
img1: Uint8ClampedArray | Buffer,
|
|
7
8
|
img2: Uint8ClampedArray | Buffer,
|
|
8
9
|
output: Uint8ClampedArray | Buffer,
|
|
9
10
|
width: number,
|
|
10
11
|
height: number,
|
|
11
|
-
|
|
12
|
+
options: ImageSnapshotCompareOptions<M> = {} as any,
|
|
12
13
|
) {
|
|
13
|
-
const pixelDiff =
|
|
14
|
-
|
|
14
|
+
const pixelDiff =
|
|
15
|
+
options.comparisonMethod === 'ssim'
|
|
16
|
+
? compareWithSsim(img1, img2, output, width, height, options.diffOptions)
|
|
17
|
+
: pixelmatch(img1, img2, output, width, height, options.diffOptions)
|
|
18
|
+
const diffAmount = convertThresholdUnit(
|
|
19
|
+
{ failureThresholdType: options.failureThresholdType ?? 'pixel', width, height },
|
|
20
|
+
pixelDiff,
|
|
21
|
+
)
|
|
15
22
|
|
|
16
23
|
return {
|
|
17
|
-
pass: diffAmount <= failureThreshold,
|
|
24
|
+
pass: diffAmount <= (options.failureThreshold ?? 0),
|
|
18
25
|
diffAmount,
|
|
19
26
|
}
|
|
20
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @author This code is based on `jest-image-snapshot` implementation.
|
|
31
|
+
*/
|
|
32
|
+
function compareWithSsim(
|
|
33
|
+
img1: Uint8ClampedArray | Buffer,
|
|
34
|
+
img2: Uint8ClampedArray | Buffer,
|
|
35
|
+
output: Uint8ClampedArray | Buffer,
|
|
36
|
+
width: number,
|
|
37
|
+
height: number,
|
|
38
|
+
diffOptions?: Partial<Options>,
|
|
39
|
+
) {
|
|
40
|
+
const newImage = { data: img1, width: width, height: height }
|
|
41
|
+
const baselineImage = { data: img2, width: width, height: height }
|
|
42
|
+
const { ssim_map, mssim } = ssim(newImage as any, baselineImage as any, {
|
|
43
|
+
ssim: 'bezkrovny',
|
|
44
|
+
...diffOptions,
|
|
45
|
+
})
|
|
46
|
+
// Converts the SSIM value to different pixels based on image width and height
|
|
47
|
+
// conforms to how pixelmatch works.
|
|
48
|
+
const diffPixels = Math.round((1 - mssim) * width * height)
|
|
49
|
+
const diffRgbaPixels = new DataView(output.buffer, output.byteOffset)
|
|
50
|
+
for (let ln = 0; ln !== height; ++ln) {
|
|
51
|
+
for (let pos = 0; pos !== width; ++pos) {
|
|
52
|
+
const rPos = ln * width + pos
|
|
53
|
+
// initial value is transparent. We'll add in the SSIM offset.
|
|
54
|
+
// red (ff) green (00) blue (00) alpha (00)
|
|
55
|
+
const diffValue =
|
|
56
|
+
0xff000000 +
|
|
57
|
+
Math.floor(
|
|
58
|
+
0xff *
|
|
59
|
+
(1 -
|
|
60
|
+
ssim_map.data[
|
|
61
|
+
ssim_map.width * Math.round((ssim_map.height * ln) / height) +
|
|
62
|
+
Math.round((ssim_map.width * pos) / width)
|
|
63
|
+
]!),
|
|
64
|
+
)
|
|
65
|
+
diffRgbaPixels.setUint32(rPos * 4, diffValue)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return diffPixels
|
|
69
|
+
}
|
package/src/shared/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PixelmatchOptions } from 'pixelmatch'
|
|
2
|
+
import type { Options as SsimOptions } from 'ssim.js'
|
|
2
3
|
|
|
3
4
|
export interface ImageSnapshotTimeoutOptions {
|
|
4
5
|
/**
|
|
@@ -19,21 +20,44 @@ export interface ImageSnapshotIdOptions {
|
|
|
19
20
|
customizeSnapshotId?: (id: string, index: number) => string
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
export type ComparisonMethod = 'pixel' | 'ssim'
|
|
24
|
+
|
|
25
|
+
export type ImageSnapshotCompareOptions<M extends ComparisonMethod = 'pixel'> = M extends 'ssim'
|
|
26
|
+
? {
|
|
27
|
+
comparisonMethod: M
|
|
28
|
+
/**
|
|
29
|
+
* Custom options passed to 'ssim'
|
|
30
|
+
*/
|
|
31
|
+
diffOptions?: Partial<SsimOptions> | undefined
|
|
32
|
+
/**
|
|
33
|
+
* Failure threshold should measure in `pixel` or `percent`.
|
|
34
|
+
*
|
|
35
|
+
* Default is `pixel`.
|
|
36
|
+
*/
|
|
37
|
+
failureThresholdType?: 'pixel' | 'percent' | undefined
|
|
38
|
+
/**
|
|
39
|
+
* Failure tolerance threshold.
|
|
40
|
+
*
|
|
41
|
+
* Default is `0`.
|
|
42
|
+
*/
|
|
43
|
+
failureThreshold?: number | undefined
|
|
44
|
+
}
|
|
45
|
+
: {
|
|
46
|
+
comparisonMethod?: M | undefined
|
|
47
|
+
/**
|
|
48
|
+
* Custom options passed to 'pixelmatch'
|
|
49
|
+
*/
|
|
50
|
+
diffOptions?: PixelmatchOptions | undefined
|
|
51
|
+
/**
|
|
52
|
+
* Failure threshold should measure in `pixel` or `percent`.
|
|
53
|
+
*
|
|
54
|
+
* Default is `pixel`.
|
|
55
|
+
*/
|
|
56
|
+
failureThresholdType?: 'pixel' | 'percent' | undefined
|
|
57
|
+
/**
|
|
58
|
+
* Failure tolerance threshold.
|
|
59
|
+
*
|
|
60
|
+
* Default is `0`.
|
|
61
|
+
*/
|
|
62
|
+
failureThreshold?: number | undefined
|
|
63
|
+
}
|