@vitest/browser 4.0.0-beta.17 → 4.0.0-beta.18

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/context.d.ts CHANGED
@@ -40,7 +40,7 @@ export interface ScreenshotOptions {
40
40
  save?: boolean
41
41
  }
42
42
 
43
- export interface ScreenshotComparatorRegistry {
43
+ interface StandardScreenshotComparators {
44
44
  pixelmatch: {
45
45
  /**
46
46
  * The maximum number of pixels that are allowed to differ between the captured
@@ -136,6 +136,13 @@ export interface ScreenshotComparatorRegistry {
136
136
  }
137
137
  }
138
138
 
139
+ export interface ScreenshotComparatorRegistry extends StandardScreenshotComparators {}
140
+
141
+ export type NonStandardScreenshotComparators = Omit<
142
+ ScreenshotComparatorRegistry,
143
+ keyof StandardScreenshotComparators
144
+ >
145
+
139
146
  export interface ScreenshotMatcherOptions<
140
147
  ComparatorName extends keyof ScreenshotComparatorRegistry = keyof ScreenshotComparatorRegistry
141
148
  > {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,40 @@
1
1
  import { ResolvedConfig, BrowserCommand, BrowserServerFactory, BrowserProviderOption } from 'vitest/node';
2
+ import { ScreenshotMatcherOptions, NonStandardScreenshotComparators, ScreenshotComparatorRegistry } from '@vitest/browser/context';
3
+
4
+ interface BaseMetadata {
5
+ height: number;
6
+ width: number;
7
+ }
8
+ type TypedArray = Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike> | Uint8ClampedArray<ArrayBufferLike>;
9
+ type Promisable<T> = T | Promise<T>;
10
+ type Comparator<Options extends Record<string, unknown>> = (reference: {
11
+ metadata: BaseMetadata;
12
+ data: TypedArray;
13
+ }, actual: {
14
+ metadata: BaseMetadata;
15
+ data: TypedArray;
16
+ }, options: {
17
+ /**
18
+ * Allows the comparator to create a diff image.
19
+ *
20
+ * Note that the comparator might choose to ignore the flag, so a diff image is not guaranteed.
21
+ */
22
+ createDiff: boolean;
23
+ } & Options) => Promisable<{
24
+ pass: boolean;
25
+ diff: TypedArray | null;
26
+ message: string | null;
27
+ }>;
28
+ type CustomComparatorsToRegister = { [Key in keyof NonStandardScreenshotComparators] : Comparator<NonStandardScreenshotComparators[Key]> };
29
+ type CustomComparatorsRegistry = keyof CustomComparatorsToRegister extends never ? {
30
+ comparators?: Record<string, Comparator<Record<string, unknown>>>;
31
+ } : {
32
+ comparators: CustomComparatorsToRegister;
33
+ };
34
+ declare module "vitest/node" {
35
+ interface ToMatchScreenshotOptions extends Omit<ScreenshotMatcherOptions, "comparatorName" | "comparatorOptions">, CustomComparatorsRegistry {}
36
+ interface ToMatchScreenshotComparators extends ScreenshotComparatorRegistry {}
37
+ }
2
38
 
3
39
  declare enum DOM_KEY_LOCATION {
4
40
  STANDARD = 0,
@@ -34,3 +70,4 @@ declare function defineBrowserProvider<T extends object = object>(options: Omit<
34
70
  }): BrowserProviderOption;
35
71
 
36
72
  export { createBrowserServer, defineBrowserCommand, defineBrowserProvider, parseKeyDef, resolveScreenshotPath };
73
+ export type { CustomComparatorsRegistry };
package/dist/index.js CHANGED
@@ -18,7 +18,7 @@ import { PNG } from 'pngjs';
18
18
  import pm from 'pixelmatch';
19
19
  import { WebSocketServer } from 'ws';
20
20
 
21
- var version = "4.0.0-beta.17";
21
+ var version = "4.0.0-beta.18";
22
22
 
23
23
  const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
24
24
  function normalizeWindowsPath(input = "") {
@@ -2045,10 +2045,14 @@ const pixelmatch = (reference, actual, { createDiff,...options }) => {
2045
2045
  };
2046
2046
  };
2047
2047
 
2048
- const comparators = new Map(Object.entries({ pixelmatch }));
2049
- function getComparator(comparator) {
2050
- if (comparators.has(comparator)) {
2051
- return comparators.get(comparator);
2048
+ const comparators = { pixelmatch };
2049
+ function getComparator(comparator, context) {
2050
+ if (comparator in comparators) {
2051
+ return comparators[comparator];
2052
+ }
2053
+ const customComparators = context.project.config.browser.expect?.toMatchScreenshot?.comparators;
2054
+ if (customComparators && comparator in customComparators) {
2055
+ return customComparators[comparator];
2052
2056
  }
2053
2057
  throw new Error(`Unrecognized comparator ${comparator}`);
2054
2058
  }
@@ -2103,7 +2107,7 @@ function resolveOptions({ context, name, options, testName }) {
2103
2107
  };
2104
2108
  return {
2105
2109
  codec: getCodec(extension),
2106
- comparator: getComparator(resolvedOptions.comparatorName),
2110
+ comparator: getComparator(resolvedOptions.comparatorName, context),
2107
2111
  resolvedOptions,
2108
2112
  paths: {
2109
2113
  reference: resolvedOptions.resolveScreenshotPath(resolvePathData),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/browser",
3
3
  "type": "module",
4
- "version": "4.0.0-beta.17",
4
+ "version": "4.0.0-beta.18",
5
5
  "description": "Browser running for Vitest",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -36,8 +36,7 @@
36
36
  "default": "./dist/locators.js"
37
37
  },
38
38
  "./utils": {
39
- "types": "./utils.d.ts",
40
- "default": "./dist/utils.js"
39
+ "default": "./dummy.js"
41
40
  },
42
41
  "./package.json": "./package.json"
43
42
  },
@@ -52,7 +51,7 @@
52
51
  "providers"
53
52
  ],
54
53
  "peerDependencies": {
55
- "vitest": "4.0.0-beta.17"
54
+ "vitest": "4.0.0-beta.18"
56
55
  },
57
56
  "dependencies": {
58
57
  "magic-string": "^0.30.19",
@@ -61,8 +60,8 @@
61
60
  "sirv": "^3.0.2",
62
61
  "tinyrainbow": "^3.0.3",
63
62
  "ws": "^8.18.3",
64
- "@vitest/mocker": "4.0.0-beta.17",
65
- "@vitest/utils": "4.0.0-beta.17"
63
+ "@vitest/mocker": "4.0.0-beta.18",
64
+ "@vitest/utils": "4.0.0-beta.18"
66
65
  },
67
66
  "devDependencies": {
68
67
  "@testing-library/user-event": "^14.6.1",
@@ -73,8 +72,8 @@
73
72
  "ivya": "^1.7.0",
74
73
  "mime": "^4.1.0",
75
74
  "pathe": "^2.0.3",
76
- "vitest": "4.0.0-beta.17",
77
- "@vitest/runner": "4.0.0-beta.17"
75
+ "@vitest/runner": "4.0.0-beta.18",
76
+ "vitest": "4.0.0-beta.18"
78
77
  },
79
78
  "scripts": {
80
79
  "typecheck": "tsc -p ./src/client/tsconfig.json --noEmit",