vitest-plugin-vis 2.4.4 → 3.0.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.
Files changed (264) hide show
  1. package/esm/client/actions/has_image_snapshot_action.d.ts +4 -0
  2. package/esm/client/actions/has_image_snapshot_action.d.ts.map +1 -0
  3. package/esm/client/actions/has_image_snapshot_action.js +6 -0
  4. package/esm/client/actions/load_image_snapshot_results_action.d.ts +3 -0
  5. package/esm/client/actions/load_image_snapshot_results_action.d.ts.map +1 -0
  6. package/esm/client/actions/load_image_snapshot_results_action.js +3 -0
  7. package/esm/client/actions/match_image_snapshot_action.d.ts +6 -0
  8. package/esm/client/actions/match_image_snapshot_action.d.ts.map +1 -0
  9. package/esm/client/actions/match_image_snapshot_action.js +20 -0
  10. package/esm/client/actions/match_page_image_snapshot_action.d.ts +6 -0
  11. package/esm/client/actions/match_page_image_snapshot_action.d.ts.map +1 -0
  12. package/esm/client/actions/match_page_image_snapshot_action.js +9 -0
  13. package/esm/client/auto_snapshot_options.d.ts +29 -0
  14. package/esm/client/auto_snapshot_options.d.ts.map +1 -0
  15. package/esm/client/auto_snapshot_options.js +49 -0
  16. package/esm/client/compare_image_snapshot.d.ts +6 -0
  17. package/esm/client/compare_image_snapshot.d.ts.map +1 -0
  18. package/esm/client/compare_image_snapshot.js +52 -0
  19. package/esm/client/{image_snapshot_matcher.logic.d.ts → compare_image_snapshot.logic.d.ts} +1 -1
  20. package/esm/client/compare_image_snapshot.logic.d.ts.map +1 -0
  21. package/esm/client/expect/to_match_image_snapshot.d.ts.map +1 -1
  22. package/esm/client/expect/to_match_image_snapshot.js +4 -3
  23. package/esm/client/image_resizer.d.ts.map +1 -1
  24. package/esm/client/image_snapshot_options.d.ts +2 -0
  25. package/esm/client/image_snapshot_options.d.ts.map +1 -0
  26. package/esm/client/image_snapshot_options.js +6 -0
  27. package/esm/client/page/augment.d.ts +2 -1
  28. package/esm/client/page/augment.d.ts.map +1 -1
  29. package/esm/client/page/extend.js +2 -1
  30. package/esm/client/page/has_image_snapshot.d.ts +6 -6
  31. package/esm/client/page/has_image_snapshot.d.ts.map +1 -1
  32. package/esm/client/page/has_image_snapshot.js +2 -11
  33. package/esm/client/page/to_match_image_snapshot.d.ts +8 -0
  34. package/esm/client/page/to_match_image_snapshot.d.ts.map +1 -0
  35. package/esm/client/page/to_match_image_snapshot.js +15 -0
  36. package/esm/client/setup/create_vis.d.ts +96 -0
  37. package/esm/client/setup/create_vis.d.ts.map +1 -0
  38. package/esm/{setup → client/setup}/create_vis.js +43 -31
  39. package/esm/client/setup/vis.ctx.d.ts +12 -0
  40. package/esm/client/setup/vis.ctx.d.ts.map +1 -0
  41. package/esm/client/setup/vis.ctx.js +32 -0
  42. package/esm/client/setup/vis.d.ts +5 -0
  43. package/esm/client/setup/vis.d.ts.map +1 -0
  44. package/esm/client/should_take_snapshot.d.ts +2 -9
  45. package/esm/client/should_take_snapshot.d.ts.map +1 -1
  46. package/esm/client/should_take_snapshot.js +1 -11
  47. package/esm/client/snapshot_meta.d.ts +6 -0
  48. package/esm/client/snapshot_meta.d.ts.map +1 -0
  49. package/esm/client/task_id.d.ts +0 -3
  50. package/esm/client/task_id.d.ts.map +1 -1
  51. package/esm/client/task_id.js +0 -3
  52. package/esm/client/vitest_suite_proxy.d.ts +2 -9
  53. package/esm/client/vitest_suite_proxy.d.ts.map +1 -1
  54. package/esm/client-api.d.ts +11 -0
  55. package/esm/client-api.d.ts.map +1 -0
  56. package/esm/client-api.js +8 -0
  57. package/esm/commands.d.ts +0 -1
  58. package/esm/commands.d.ts.map +1 -1
  59. package/esm/commands.js +0 -1
  60. package/esm/config/types.d.ts +22 -22
  61. package/esm/config/types.d.ts.map +1 -1
  62. package/esm/config/vis.d.ts +4 -3
  63. package/esm/config/vis.d.ts.map +1 -1
  64. package/esm/config/vis.js +3 -4
  65. package/esm/index.d.ts +1 -1
  66. package/esm/index.d.ts.map +1 -1
  67. package/esm/index.js +1 -1
  68. package/esm/presets/auto.d.ts +1 -2
  69. package/esm/presets/auto.d.ts.map +1 -1
  70. package/esm/presets/auto.js +8 -4
  71. package/esm/presets/enable.d.ts +1 -2
  72. package/esm/presets/enable.d.ts.map +1 -1
  73. package/esm/presets/enable.js +4 -4
  74. package/esm/presets/manual.d.ts +1 -2
  75. package/esm/presets/manual.d.ts.map +1 -1
  76. package/esm/presets/manual.js +3 -4
  77. package/esm/server/browser_provider/playwright.d.ts.map +1 -1
  78. package/esm/server/browser_provider/playwright.js +9 -3
  79. package/esm/server/browser_provider/types.d.ts +5 -1
  80. package/esm/server/browser_provider/types.d.ts.map +1 -1
  81. package/esm/server/browser_provider/webdriverio.d.ts.map +1 -1
  82. package/esm/server/browser_provider/webdriverio.js +7 -4
  83. package/esm/server/commands/augment.d.ts +11 -0
  84. package/esm/server/commands/augment.d.ts.map +1 -0
  85. package/esm/server/commands/augment.js +1 -0
  86. package/esm/server/commands/commands.d.ts +9 -0
  87. package/esm/server/commands/commands.d.ts.map +1 -0
  88. package/esm/server/commands/commands.js +14 -0
  89. package/esm/server/commands/has_image_snapshot.d.ts +2 -6
  90. package/esm/server/commands/has_image_snapshot.d.ts.map +1 -1
  91. package/esm/server/commands/has_image_snapshot.js +3 -3
  92. package/esm/server/commands/image_snapshot_next_index.d.ts +1 -1
  93. package/esm/server/commands/image_snapshot_next_index.d.ts.map +1 -1
  94. package/esm/server/commands/image_snapshot_next_index.js +2 -2
  95. package/esm/server/commands/load_image_snapshot_results.d.ts +21 -0
  96. package/esm/server/commands/load_image_snapshot_results.d.ts.map +1 -0
  97. package/esm/server/commands/load_image_snapshot_results.js +6 -0
  98. package/esm/server/commands/prepare_image_snapshot_comparison.d.ts +8 -10
  99. package/esm/server/commands/prepare_image_snapshot_comparison.d.ts.map +1 -1
  100. package/esm/server/commands/prepare_image_snapshot_comparison.js +11 -9
  101. package/esm/server/commands/prepare_page_image_snapshot_comparison.d.ts +35 -0
  102. package/esm/server/commands/prepare_page_image_snapshot_comparison.d.ts.map +1 -0
  103. package/esm/server/commands/prepare_page_image_snapshot_comparison.js +27 -0
  104. package/esm/server/commands/setup_vis_suite.d.ts +3 -3
  105. package/esm/server/commands/setup_vis_suite.d.ts.map +1 -1
  106. package/esm/server/commands/setup_vis_suite.js +2 -3
  107. package/esm/server/commands/types.d.ts +3 -0
  108. package/esm/server/commands/types.d.ts.map +1 -0
  109. package/esm/server/commands/types.js +1 -0
  110. package/esm/server/externals/file.d.ts +10 -0
  111. package/esm/server/externals/file.d.ts.map +1 -0
  112. package/esm/server/{file.js → externals/file.js} +12 -4
  113. package/esm/server/{commands/browser_command_context.d.ts → project.d.ts} +8 -3
  114. package/esm/server/project.d.ts.map +1 -0
  115. package/esm/server/{commands/browser_command_context.js → project.js} +2 -2
  116. package/esm/server/snapshot.d.ts +4 -5
  117. package/esm/server/snapshot.d.ts.map +1 -1
  118. package/esm/server/snapshot.js +15 -13
  119. package/esm/server/snapshot_path.d.ts +3 -3
  120. package/esm/server/snapshot_path.d.ts.map +1 -1
  121. package/esm/server/snapshot_path.js +12 -11
  122. package/esm/server/snapshot_writer.d.ts +13 -0
  123. package/esm/server/snapshot_writer.d.ts.map +1 -0
  124. package/esm/server/snapshot_writer.js +15 -0
  125. package/esm/server/suite.d.ts +53 -0
  126. package/esm/server/suite.d.ts.map +1 -0
  127. package/esm/server/suite.js +73 -0
  128. package/esm/server/vis_options.d.ts +18 -0
  129. package/esm/server/vis_options.d.ts.map +1 -0
  130. package/esm/server/vis_options.js +14 -0
  131. package/esm/server/vis_server_context.d.ts +50 -0
  132. package/esm/server/vis_server_context.d.ts.map +1 -0
  133. package/esm/server/{vis_context.ctx.d.ts → vis_server_context.deps.d.ts} +2 -2
  134. package/esm/server/vis_server_context.deps.d.ts.map +1 -0
  135. package/esm/server/{vis_context.ctx.js → vis_server_context.deps.js} +1 -1
  136. package/esm/server/vis_server_context.js +2 -0
  137. package/esm/server/{vis_context.logic.d.ts → vis_server_context.logic.d.ts} +12 -32
  138. package/esm/server/vis_server_context.logic.d.ts.map +1 -0
  139. package/esm/server/vis_server_context.logic.js +79 -0
  140. package/esm/server/{vis_context.types.d.ts → vis_server_context.types.d.ts} +6 -6
  141. package/esm/server/vis_server_context.types.d.ts.map +1 -0
  142. package/esm/server/vis_server_context.types.js +1 -0
  143. package/esm/server-utils.d.ts +3 -0
  144. package/esm/server-utils.d.ts.map +1 -0
  145. package/esm/server-utils.js +2 -0
  146. package/esm/setup.d.ts +2 -2
  147. package/esm/setup.d.ts.map +1 -1
  148. package/esm/setup.js +2 -2
  149. package/esm/shared/asserts.d.ts +2 -0
  150. package/esm/shared/asserts.d.ts.map +1 -0
  151. package/esm/shared/asserts.js +5 -0
  152. package/esm/shared/types.d.ts +22 -21
  153. package/esm/shared/types.d.ts.map +1 -1
  154. package/esm/testing/stubBrowserCommandContext.d.ts +1 -1
  155. package/esm/testing/stubBrowserCommandContext.d.ts.map +1 -1
  156. package/esm/testing/stubBrowserCommandContext.js +3 -13
  157. package/esm/testing/stubSuite.d.ts +10 -0
  158. package/esm/testing/stubSuite.d.ts.map +1 -0
  159. package/esm/testing/stubSuite.js +27 -0
  160. package/esm/testing/stubUserConfig.d.ts +7 -0
  161. package/esm/testing/stubUserConfig.d.ts.map +1 -0
  162. package/esm/testing/stubUserConfig.js +4 -0
  163. package/package.json +29 -28
  164. package/readme.md +409 -110
  165. package/src/client/actions/has_image_snapshot_action.ts +14 -0
  166. package/src/client/actions/load_image_snapshot_results_action.ts +5 -0
  167. package/src/client/actions/match_image_snapshot_action.ts +32 -0
  168. package/src/client/actions/match_page_image_snapshot_action.ts +20 -0
  169. package/src/client/auto_snapshot_options.ts +71 -0
  170. package/src/client/compare_image_snapshot.ts +84 -0
  171. package/src/client/expect/to_match_image_snapshot.ts +4 -3
  172. package/src/client/image_snapshot_options.ts +6 -0
  173. package/src/client/page/augment.ts +2 -1
  174. package/src/client/page/extend.ts +2 -1
  175. package/src/client/page/has_image_snapshot.ts +8 -21
  176. package/src/client/page/to_match_image_snapshot.ts +27 -0
  177. package/src/client/setup/create_vis.ts +227 -0
  178. package/src/client/setup/vis.ctx.ts +33 -0
  179. package/src/client/should_take_snapshot.ts +3 -13
  180. package/src/client/snapshot_meta.ts +7 -0
  181. package/src/client/task_id.ts +0 -3
  182. package/src/client/vitest_suite_proxy.ts +2 -5
  183. package/src/client-api.ts +11 -0
  184. package/src/commands.ts +0 -1
  185. package/src/config/types.ts +32 -27
  186. package/src/config/vis.ts +3 -5
  187. package/src/index.ts +1 -1
  188. package/src/presets/auto.ts +8 -4
  189. package/src/presets/enable.ts +4 -4
  190. package/src/presets/manual.ts +3 -4
  191. package/src/server/browser_provider/playwright.ts +9 -3
  192. package/src/server/browser_provider/types.ts +12 -1
  193. package/src/server/browser_provider/webdriverio.ts +7 -4
  194. package/src/server/commands/augment.ts +16 -0
  195. package/src/server/commands/commands.ts +15 -0
  196. package/src/server/commands/has_image_snapshot.ts +8 -6
  197. package/src/server/commands/image_snapshot_next_index.ts +5 -3
  198. package/src/server/commands/load_image_snapshot_results.ts +32 -0
  199. package/src/server/commands/prepare_image_snapshot_comparison.ts +20 -14
  200. package/src/server/commands/prepare_page_image_snapshot_comparison.ts +75 -0
  201. package/src/server/commands/setup_vis_suite.ts +17 -7
  202. package/src/server/commands/types.ts +9 -0
  203. package/src/server/externals/file.ts +21 -0
  204. package/src/server/{commands/browser_command_context.ts → project.ts} +2 -4
  205. package/src/server/snapshot.ts +23 -14
  206. package/src/server/snapshot_path.ts +15 -13
  207. package/src/server/snapshot_writer.ts +22 -0
  208. package/src/server/suite.ts +96 -0
  209. package/src/server/vis_options.ts +27 -0
  210. package/src/server/{vis_context.ctx.ts → vis_server_context.deps.ts} +1 -1
  211. package/src/server/vis_server_context.logic.ts +112 -0
  212. package/src/server/vis_server_context.ts +3 -0
  213. package/src/server/{vis_context.types.ts → vis_server_context.types.ts} +5 -5
  214. package/src/server-utils.ts +2 -0
  215. package/src/setup.ts +2 -2
  216. package/src/shared/asserts.ts +5 -0
  217. package/src/shared/types.ts +25 -23
  218. package/src/testing/stubBrowserCommandContext.ts +4 -14
  219. package/src/testing/stubSuite.ts +38 -0
  220. package/src/testing/stubUserConfig.ts +6 -0
  221. package/esm/client/image_snapshot_matcher.d.ts +0 -11
  222. package/esm/client/image_snapshot_matcher.d.ts.map +0 -1
  223. package/esm/client/image_snapshot_matcher.js +0 -80
  224. package/esm/client/image_snapshot_matcher.logic.d.ts.map +0 -1
  225. package/esm/client/snapshot_options.d.ts +0 -33
  226. package/esm/client/snapshot_options.d.ts.map +0 -1
  227. package/esm/client/snapshot_options.js +0 -37
  228. package/esm/client.d.ts +0 -8
  229. package/esm/client.d.ts.map +0 -1
  230. package/esm/client.js +0 -5
  231. package/esm/server/commands/browser_command_context.d.ts.map +0 -1
  232. package/esm/server/commands/extend.d.ts +0 -17
  233. package/esm/server/commands/extend.d.ts.map +0 -1
  234. package/esm/server/commands/extend.js +0 -12
  235. package/esm/server/commands/match_image_snapshot.d.ts +0 -18
  236. package/esm/server/commands/match_image_snapshot.d.ts.map +0 -1
  237. package/esm/server/commands/match_image_snapshot.js +0 -81
  238. package/esm/server/file.d.ts +0 -7
  239. package/esm/server/file.d.ts.map +0 -1
  240. package/esm/server/vis_context.ctx.d.ts.map +0 -1
  241. package/esm/server/vis_context.d.ts +0 -56
  242. package/esm/server/vis_context.d.ts.map +0 -1
  243. package/esm/server/vis_context.js +0 -2
  244. package/esm/server/vis_context.logic.d.ts.map +0 -1
  245. package/esm/server/vis_context.logic.js +0 -137
  246. package/esm/server/vis_context.types.d.ts.map +0 -1
  247. package/esm/setup/create_vis.d.ts +0 -53
  248. package/esm/setup/create_vis.d.ts.map +0 -1
  249. package/esm/setup/vis.d.ts +0 -5
  250. package/esm/setup/vis.d.ts.map +0 -1
  251. package/src/client/image_snapshot_matcher.ts +0 -121
  252. package/src/client/snapshot_options.ts +0 -82
  253. package/src/client.ts +0 -7
  254. package/src/server/commands/extend.ts +0 -25
  255. package/src/server/commands/match_image_snapshot.ts +0 -128
  256. package/src/server/file.ts +0 -13
  257. package/src/server/vis_context.logic.ts +0 -200
  258. package/src/server/vis_context.ts +0 -3
  259. package/src/setup/create_vis.ts +0 -154
  260. /package/esm/client/{image_snapshot_matcher.logic.js → compare_image_snapshot.logic.js} +0 -0
  261. /package/esm/{setup → client/setup}/vis.js +0 -0
  262. /package/esm/{server/vis_context.types.js → client/snapshot_meta.js} +0 -0
  263. /package/src/client/{image_snapshot_matcher.logic.ts → compare_image_snapshot.logic.ts} +0 -0
  264. /package/src/{setup → client/setup}/vis.ts +0 -0
@@ -1,31 +1,40 @@
1
1
  import type {
2
- AutoSnapshotOptions,
3
2
  ComparisonMethod,
4
- ImageSnapshotCompareOptions,
5
- ImageSnapshotIdOptions,
3
+ FailureThresholdOptions,
4
+ ImageSnapshotKeyOptions,
5
+ ImageSnapshotSubjectOptions,
6
6
  ImageSnapshotTimeoutOptions,
7
+ PixelComparisonOptions,
8
+ PixelDiffOptions,
9
+ SsimComparisonOptions,
10
+ SsimDiffOptions,
7
11
  } from '../shared/types.ts'
8
12
 
9
- export type { ComparisonMethod }
13
+ export type { ComparisonMethod, PixelComparisonOptions, PixelDiffOptions, SsimComparisonOptions, SsimDiffOptions }
10
14
 
11
- export type VisOptions<M extends ComparisonMethod = 'pixel'> = ImageSnapshotTimeoutOptions &
12
- ImageSnapshotIdOptions &
13
- ImageSnapshotCompareOptions<M> &
14
- AutoSnapshotOptions & {
15
+ export type VisOptions<M extends ComparisonMethod = 'pixel'> = (M extends 'ssim'
16
+ ? {
17
+ comparisonMethod: M
18
+ /**
19
+ * Custom options passed to 'ssim'
20
+ */
21
+ diffOptions?: Partial<SsimDiffOptions> | undefined
22
+ }
23
+ : {
24
+ comparisonMethod?: M | undefined
25
+ /**
26
+ * Custom options passed to 'pixelmatch'
27
+ */
28
+ diffOptions?: PixelDiffOptions | undefined
29
+ }) &
30
+ FailureThresholdOptions &
31
+ ImageSnapshotTimeoutOptions &
32
+ ImageSnapshotKeyOptions &
33
+ ImageSnapshotSubjectOptions & {
15
34
  /**
16
35
  * Loads the `test.setupFiles` of the specified preset.
17
36
  */
18
37
  preset?: 'enable' | 'manual' | 'auto' | 'none' | undefined
19
- /**
20
- * The name of the subdirectory that the baseline snapshots get saved in.
21
- *
22
- * <snapshotRootDir>/<platform>/<snapshotSubpath>/<snapshotId>.png
23
- *
24
- * Default: undefined
25
- *
26
- * @deprecated Use `snapshotRootDir` instead.
27
- */
28
- platform?: string | undefined
29
38
  /**
30
39
  * The snapshot folder relative to the root of the project.
31
40
  *
@@ -45,12 +54,6 @@ export type VisOptions<M extends ComparisonMethod = 'pixel'> = ImageSnapshotTime
45
54
  /**
46
55
  * Customize the snapshot subpath.
47
56
  *
48
- * The snapshot subpath is used along with `snapshotRootDir` to determine the folder of the snapshots:
49
- *
50
- * - baseline: `<snapshotRootDir>/baselines/<snapshotSubpath>/<snapshotId>.png`
51
- * - result: `<snapshotRootDir>/results/<snapshotSubpath>/<snapshotId>.png`
52
- * - diff: `<snapshotRootDir>/diffs/<snapshotSubpath>/<snapshotId>.png`
53
- *
54
57
  * Typically, you place your test files either in a dedicated `tests` folder or in the `src` folder along with your source code.
55
58
  * By default, the snapshot subpath is the test file path with that folder removed to reduces nesting of the snapshot folders.
56
59
  *
@@ -59,8 +62,10 @@ export type VisOptions<M extends ComparisonMethod = 'pixel'> = ImageSnapshotTime
59
62
  * and they might have files with the same name and create conflicting snapshots,
60
63
  * you can set this to customize the snapshot subpath.
61
64
  *
62
- * @param subPath - The path of the test file to be used as the snapshot sub path.
63
- * If you want to keep the full path, you can simply return it.
65
+ * @param options - Options for customizing the snapshot subpath.
66
+ * @property subPath - The path of the test file to be used as the snapshot subpath.
67
+ * To retain the full path, simply return it.
68
+ * @returns The customized snapshot subpath.
64
69
  */
65
- customizeSnapshotSubpath?: (subPath: string) => string
70
+ snapshotSubpath?: ((options: { subpath: string }) => string) | undefined
66
71
  }
package/src/config/vis.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Plugin } from 'vite'
2
- import { commands } from '../server/commands/extend.ts'
3
- import { visContext } from '../server/vis_context.ts'
2
+ import { commands } from '../server/commands/commands.ts'
3
+ import { setVisOption } from '../server/vis_options.ts'
4
4
  import { NAME } from '../shared/constants.ts'
5
5
  import type { ComparisonMethod } from '../shared/types.ts'
6
6
  import type { VisOptions } from './types.ts'
@@ -15,9 +15,7 @@ export function vis<M extends ComparisonMethod = 'pixel'>(options: VisOptions<M>
15
15
  return {
16
16
  name: NAME,
17
17
  config(userConfig) {
18
- const projectName = userConfig?.test?.name
19
-
20
- visContext.setOptions(projectName, options)
18
+ setVisOption(userConfig, options)
21
19
  const preset = options?.preset
22
20
  return {
23
21
  test: {
package/src/index.ts CHANGED
@@ -2,5 +2,5 @@
2
2
  import './client/expect/augment.ts'
3
3
  import './client/page/augment.ts'
4
4
 
5
- export { setAutoSnapshotOptions, type MetaTask } from './client/snapshot_options.ts'
5
+ export { setAutoSnapshotOptions, type MetaTask } from './client/auto_snapshot_options.ts'
6
6
  export type * from './shared/types.ts'
@@ -1,5 +1,9 @@
1
- import { vis } from '../setup/vis.ts'
2
- import '../client/expect/extend.ts'
3
- import '../client/page/extend.ts'
1
+ import { afterEach, beforeAll } from 'vitest'
2
+ import { setAutoSnapshotOptions } from '../index.ts'
3
+ import { vis } from '../setup.ts'
4
4
 
5
- vis.presets.auto()
5
+ beforeAll(async () => {
6
+ await vis.beforeAll.setup()
7
+ setAutoSnapshotOptions(true)
8
+ })
9
+ afterEach(() => vis.afterEach.matchImageSnapshot())
@@ -1,5 +1,5 @@
1
- import { vis } from '../setup/vis.ts'
2
- import '../client/expect/extend.ts'
3
- import '../client/page/extend.ts'
1
+ import { afterEach, beforeAll } from 'vitest'
2
+ import { vis } from '../setup.ts'
4
3
 
5
- vis.presets.enable()
4
+ beforeAll(() => vis.beforeAll.setup())
5
+ afterEach(() => vis.afterEach.matchImageSnapshot())
@@ -1,5 +1,4 @@
1
- import { vis } from '../setup/vis.ts'
2
- import '../client/expect/extend.ts'
3
- import '../client/page/extend.ts'
1
+ import { beforeAll } from 'vitest'
2
+ import { vis } from '../setup.ts'
4
3
 
5
- vis.presets.manual()
4
+ beforeAll(() => vis.beforeAll.setup())
@@ -1,13 +1,19 @@
1
+ import { resolve } from 'pathe'
1
2
  import type { BrowserCommandContext } from 'vitest/node'
2
3
  import type { BrowserApi } from './types.ts'
3
4
 
4
5
  export function playwright(context: BrowserCommandContext): BrowserApi {
5
6
  return {
6
- async takeScreenshot(filePath, selector, options) {
7
- // The `Locator` type from `vitest` has less props than the `Locator` in `playwright`
7
+ async takeScreenshot(projectRoot, relativeFilePath, selector, options) {
8
8
  const subject = context.iframe.locator(selector)
9
9
  return subject.screenshot({
10
- path: filePath,
10
+ path: resolve(projectRoot, relativeFilePath),
11
+ ...options,
12
+ })
13
+ },
14
+ async takePageScreenshot(projectRoot, relativeFilePath, options) {
15
+ return context.page.screenshot({
16
+ path: resolve(projectRoot, relativeFilePath),
11
17
  ...options,
12
18
  })
13
19
  },
@@ -1,6 +1,7 @@
1
1
  export interface BrowserApi {
2
2
  takeScreenshot(
3
- filePath: string,
3
+ projectRoot: string,
4
+ relativeFilePath: string,
4
5
  selector: string,
5
6
  options:
6
7
  | {
@@ -8,4 +9,14 @@ export interface BrowserApi {
8
9
  }
9
10
  | undefined,
10
11
  ): Promise<Buffer>
12
+ takePageScreenshot(
13
+ projectRoot: string,
14
+ relativeFilePath: string,
15
+ options:
16
+ | {
17
+ fullPage?: boolean | undefined
18
+ timeout?: number | undefined
19
+ }
20
+ | undefined,
21
+ ): Promise<Buffer>
11
22
  }
@@ -2,11 +2,14 @@ import type { BrowserCommandContext } from 'vitest/node'
2
2
  import type { BrowserApi } from './types.ts'
3
3
 
4
4
  export function webdriverio(context: BrowserCommandContext): BrowserApi {
5
- const page = (context.provider as any).browser!
5
+ const browser = context.browser
6
6
  return {
7
- async takeScreenshot(filePath, selector) {
8
- const element = await page.$(`${selector}`)
9
- return element.saveScreenshot(filePath)
7
+ async takeScreenshot(_projectRoot, relativeFilePath, selector) {
8
+ const element = await browser.$(`${selector}`)
9
+ return element.saveScreenshot(relativeFilePath)
10
+ },
11
+ async takePageScreenshot(_projectRoot, relativeFilePath, options) {
12
+ return browser.saveScreenshot(relativeFilePath, options)
10
13
  },
11
14
  }
12
15
  }
@@ -0,0 +1,16 @@
1
+ import type { HasImageSnapshotCommand } from './has_image_snapshot.ts'
2
+ import type { ImageSnapshotNextIndexCommand } from './image_snapshot_next_index.ts'
3
+ import type { LoadImageSnapshotResultsCommand } from './load_image_snapshot_results.ts'
4
+ import type { PrepareImageSnapshotComparisonCommand } from './prepare_image_snapshot_comparison.ts'
5
+ import type { PreparePageImageSnapshotComparisonCommand } from './prepare_page_image_snapshot_comparison.ts'
6
+ import type { SetupVisSuiteCommand } from './setup_vis_suite.ts'
7
+
8
+ declare module '@vitest/browser/context' {
9
+ interface BrowserCommands
10
+ extends SetupVisSuiteCommand,
11
+ ImageSnapshotNextIndexCommand,
12
+ HasImageSnapshotCommand,
13
+ PreparePageImageSnapshotComparisonCommand,
14
+ PrepareImageSnapshotComparisonCommand,
15
+ LoadImageSnapshotResultsCommand {}
16
+ }
@@ -0,0 +1,15 @@
1
+ import { hasImageSnapshot } from './has_image_snapshot.ts'
2
+ import { imageSnapshotNextIndex } from './image_snapshot_next_index.ts'
3
+ import { loadImageSnapshotResults } from './load_image_snapshot_results.ts'
4
+ import { prepareImageSnapshotComparison } from './prepare_image_snapshot_comparison.ts'
5
+ import { preparePageImageSnapshotComparison } from './prepare_page_image_snapshot_comparison.ts'
6
+ import { setupVisSuite } from './setup_vis_suite.ts'
7
+
8
+ export const commands = {
9
+ setupVisSuite,
10
+ imageSnapshotNextIndex,
11
+ hasImageSnapshot,
12
+ preparePageImageSnapshotComparison,
13
+ prepareImageSnapshotComparison,
14
+ loadImageSnapshotResults,
15
+ }
@@ -1,15 +1,17 @@
1
1
  import type { BrowserCommand } from 'vitest/node'
2
- import { visContext } from '../vis_context.ts'
2
+ import { visServerContext } from '../vis_server_context.ts'
3
3
  import { assertTestPathDefined } from './_assertions.ts'
4
4
 
5
5
  export interface HasImageSnapshotCommand {
6
- hasImageSnapshot(taskId: string, snapshotId: string | undefined, isAutoSnapshot: boolean): Promise<boolean>
6
+ hasImageSnapshot(taskId: string, snapshotKey: string | undefined): Promise<boolean>
7
7
  }
8
8
 
9
- export const hasImageSnapshot: BrowserCommand<
10
- [taskId: string, snapshotId: string | undefined, isAutoSnapshot: boolean]
11
- > = async (context, taskId, snapshotId, isAutoSnapshot) => {
9
+ export const hasImageSnapshot: BrowserCommand<Parameters<HasImageSnapshotCommand['hasImageSnapshot']>> = async (
10
+ context,
11
+ taskId,
12
+ snapshotKey,
13
+ ) => {
12
14
  assertTestPathDefined(context, 'hasImageSnapshot')
13
15
 
14
- return visContext.hasImageSnapshot(context as any, taskId, snapshotId, isAutoSnapshot)
16
+ return visServerContext.hasImageSnapshot(context as any, taskId, snapshotKey)
15
17
  }
@@ -1,5 +1,5 @@
1
1
  import type { BrowserCommand } from 'vitest/node'
2
- import { visContext } from '../vis_context.ts'
2
+ import { visServerContext } from '../vis_server_context.ts'
3
3
  import { assertTestPathDefined } from './_assertions.ts'
4
4
 
5
5
  export interface ImageSnapshotNextIndexCommand {
@@ -9,8 +9,10 @@ export interface ImageSnapshotNextIndexCommand {
9
9
  imageSnapshotNextIndex(taskId: string): Promise<number>
10
10
  }
11
11
 
12
- export const imageSnapshotNextIndex: BrowserCommand<[taskId: string]> = async (context, taskId) => {
12
+ export const imageSnapshotNextIndex: BrowserCommand<
13
+ Parameters<ImageSnapshotNextIndexCommand['imageSnapshotNextIndex']>
14
+ > = async (context, taskId) => {
13
15
  assertTestPathDefined(context, 'imageSnapshotNextIndex')
14
16
 
15
- return visContext.getTaskCount(context, taskId)
17
+ return visServerContext.getTaskCount(context, taskId)
16
18
  }
@@ -0,0 +1,32 @@
1
+ import type { BrowserCommand } from 'vitest/node'
2
+ import { visServerContext } from '../vis_server_context.ts'
3
+ import { assertTestPathDefined } from './_assertions.ts'
4
+
5
+ export interface ImageSnapshotResult {
6
+ filename: string
7
+ /**
8
+ * The base64 encoded image of the baseline snapshot.
9
+ */
10
+ baseline?: string | undefined
11
+ /**
12
+ * The base64 encoded image of the result snapshot.
13
+ */
14
+ result?: string | undefined
15
+ /**
16
+ * The base64 encoded image of the diff snapshot.
17
+ */
18
+ diff?: string | undefined
19
+ }
20
+
21
+ export interface LoadImageSnapshotResultsCommand {
22
+ loadImageSnapshotResults(taskId: string): Promise<ImageSnapshotResult[]>
23
+ }
24
+
25
+ export const loadImageSnapshotResults: BrowserCommand<[taskId: string]> = async (
26
+ context,
27
+ taskId,
28
+ ): Promise<ImageSnapshotResult[]> => {
29
+ assertTestPathDefined(context, 'loadImageSnapshotResults')
30
+
31
+ return visServerContext.getSnapshotResults(context, taskId)
32
+ }
@@ -7,13 +7,15 @@ import type {
7
7
  PixelComparisonOptions,
8
8
  SsimComparisonOptions,
9
9
  } from '../../shared/types.ts'
10
- import { file } from '../file.ts'
11
- import { takeSnapshot, takeSnapshotByBrowser, writeSnapshot } from '../snapshot.ts'
12
- import { visContext } from '../vis_context.ts'
10
+ import { file } from '../externals/file.ts'
11
+ import { getProjectRoot } from '../project.ts'
12
+ import { takeSnapshot, takeSnapshotByBrowser } from '../snapshot.ts'
13
+ import { snapshotWriter } from '../snapshot_writer.ts'
14
+ import { visServerContext } from '../vis_server_context.ts'
13
15
  import { assertTestPathDefined } from './_assertions.ts'
14
- import type { MatchImageSnapshotOptions } from './match_image_snapshot.ts'
16
+ import type { MatchImageSnapshotOptions } from './types.ts'
15
17
 
16
- type ImageSnapshotComparisonInfo = {
18
+ export type ImageSnapshotComparisonInfo = {
17
19
  /**
18
20
  * Path to the project root.
19
21
  */
@@ -38,40 +40,44 @@ type ImageSnapshotComparisonInfo = {
38
40
  * Base64 encoded result image.
39
41
  */
40
42
  result: string
43
+ /**
44
+ * Base64 encoded diff image.
45
+ */
46
+ diff?: string | undefined
41
47
  } & ImageSnapshotTimeoutOptions &
42
48
  FailureThresholdOptions &
43
49
  (SsimComparisonOptions | PixelComparisonOptions)
44
50
 
45
51
  export interface PrepareImageSnapshotComparisonCommand {
46
52
  prepareImageSnapshotComparison: (
47
- taskId: string | undefined,
53
+ taskId: string,
48
54
  subject: string,
49
- isAutoSnapshot: boolean,
50
55
  options?: MatchImageSnapshotOptions | undefined,
51
56
  ) => Promise<ImageSnapshotComparisonInfo | undefined>
52
57
  }
53
58
 
54
59
  export const prepareImageSnapshotComparison: BrowserCommand<
55
- [taskId: string, snapshotId: string, isAutoSnapshot: boolean, options?: MatchImageSnapshotOptions | undefined]
56
- > = async (context, taskId, subject, isAutoSnapshot, options) => {
60
+ Parameters<PrepareImageSnapshotComparisonCommand['prepareImageSnapshotComparison']>
61
+ > = async (context, taskId, subject, options): Promise<ImageSnapshotComparisonInfo | undefined> => {
57
62
  assertTestPathDefined(context, 'prepareImageSnapshotComparison')
58
63
  // vitest:browser passes in `null` when not defined
59
64
  if (!options) options = {}
60
65
  options.timeout = options.timeout ?? 30000
61
66
 
62
- const projectRoot = context.project.config.root
63
- const info = await visContext.getSnapshotInfo(context as any, taskId, isAutoSnapshot, options)
67
+ const projectRoot = getProjectRoot(context)
68
+ const info = await visServerContext.getSnapshotInfo(context, taskId, options)
69
+
64
70
  const baselineBuffer = await file.tryReadFile(resolve(projectRoot, info.baselinePath))
65
71
  if (!baselineBuffer) {
66
72
  if (isBase64String(subject)) {
67
- await writeSnapshot(resolve(projectRoot, info.baselinePath), subject)
73
+ await snapshotWriter.writeBase64(resolve(projectRoot, info.baselinePath), subject)
68
74
  } else {
69
- await takeSnapshotByBrowser(context, resolve(projectRoot, info.baselinePath), subject, options)
75
+ await takeSnapshotByBrowser(context, projectRoot, info.baselinePath, subject, options)
70
76
  }
71
77
  return
72
78
  }
73
79
 
74
- const resultBuffer = await takeSnapshot(context, resolve(projectRoot, info.resultPath), subject, options)
80
+ const resultBuffer = await takeSnapshot(context, projectRoot, info.resultPath, subject, options)
75
81
  return {
76
82
  ...info,
77
83
  projectRoot,
@@ -0,0 +1,75 @@
1
+ import { resolve } from 'pathe'
2
+ import type { BrowserCommand } from 'vitest/node'
3
+ import type {
4
+ FailureThresholdOptions,
5
+ ImageSnapshotTimeoutOptions,
6
+ PixelComparisonOptions,
7
+ SsimComparisonOptions,
8
+ } from '../../shared/types.ts'
9
+ import { file } from '../externals/file.ts'
10
+ import { getProjectRoot } from '../project.ts'
11
+ import { takePageSnapshot } from '../snapshot.ts'
12
+ import { visServerContext } from '../vis_server_context.ts'
13
+ import { assertTestPathDefined } from './_assertions.ts'
14
+ import type { MatchImageSnapshotOptions } from './types.ts'
15
+
16
+ type ImageSnapshotComparisonInfo = {
17
+ /**
18
+ * Path to the project root.
19
+ */
20
+ projectRoot: string
21
+ /**
22
+ * Path to the baseline image relative to the project root.
23
+ */
24
+ baselinePath: string
25
+ /**
26
+ * Path to the result image relative to the project root.
27
+ */
28
+ resultPath: string
29
+ /**
30
+ * Path to the diff image relative to the project root.
31
+ */
32
+ diffPath: string
33
+ /**
34
+ * Base64 encoded baseline image.
35
+ */
36
+ baseline: string
37
+ /**
38
+ * Base64 encoded result image.
39
+ */
40
+ result: string
41
+ } & ImageSnapshotTimeoutOptions &
42
+ FailureThresholdOptions &
43
+ (SsimComparisonOptions | PixelComparisonOptions)
44
+
45
+ export interface PreparePageImageSnapshotComparisonCommand {
46
+ preparePageImageSnapshotComparison: (
47
+ taskId: string,
48
+ options?: MatchImageSnapshotOptions | undefined,
49
+ ) => Promise<ImageSnapshotComparisonInfo | undefined>
50
+ }
51
+
52
+ export const preparePageImageSnapshotComparison: BrowserCommand<
53
+ Parameters<PreparePageImageSnapshotComparisonCommand['preparePageImageSnapshotComparison']>
54
+ > = async (context, taskId, options) => {
55
+ assertTestPathDefined(context, 'preparePageImageSnapshotComparison')
56
+ // vitest:browser passes in `null` when not defined
57
+ if (!options) options = {}
58
+ options.timeout = options.timeout ?? 30000
59
+
60
+ const projectRoot = getProjectRoot(context)
61
+ const info = await visServerContext.getSnapshotInfo(context, taskId, options)
62
+ const baselineBuffer = await file.tryReadFile(resolve(projectRoot, info.baselinePath))
63
+ if (!baselineBuffer) {
64
+ await takePageSnapshot(context, projectRoot, info.baselinePath, options)
65
+ return
66
+ }
67
+
68
+ const resultBuffer = await takePageSnapshot(context, projectRoot, info.resultPath, options)
69
+ return {
70
+ ...info,
71
+ projectRoot,
72
+ baseline: baselineBuffer.toString('base64'),
73
+ result: resultBuffer.toString('base64'),
74
+ }
75
+ }
@@ -1,5 +1,7 @@
1
+ import type { Pick } from 'type-plus'
1
2
  import type { BrowserCommand } from 'vitest/node'
2
- import { visContext } from '../vis_context.ts'
3
+ import type { VisOptions } from '../../config/types.ts'
4
+ import { setupSuite } from '../suite.ts'
3
5
  import { assertTestPathDefined } from './_assertions.ts'
4
6
 
5
7
  export interface SetupVisSuiteCommand {
@@ -10,13 +12,21 @@ export interface SetupVisSuiteCommand {
10
12
  *
11
13
  * In subsequent runs, it will only remove the results and diffs directory of the current suite.
12
14
  */
13
- setupVisSuite: () => Promise<{ subjectDataTestId: string | undefined }>
15
+ setupVisSuite: () => Promise<
16
+ Pick<
17
+ VisOptions,
18
+ | 'comparisonMethod'
19
+ | 'diffOptions'
20
+ | 'failureThreshold'
21
+ | 'failureThresholdType'
22
+ | 'snapshotKey'
23
+ | 'subject'
24
+ | 'timeout'
25
+ >
26
+ >
14
27
  }
15
28
 
16
- export const setupVisSuite: BrowserCommand<[]> = async (
17
- context,
18
- ): Promise<{ subjectDataTestId: string | undefined }> => {
29
+ export const setupVisSuite: BrowserCommand<[]> = async (context): Promise<{ subject: string | undefined }> => {
19
30
  assertTestPathDefined(context, 'setupVisSuite')
20
- // using props not currently listed in the types
21
- return visContext.setupSuite(context as any)
31
+ return setupSuite(context)
22
32
  }
@@ -0,0 +1,9 @@
1
+ import type {
2
+ ImageSnapshotCompareOptions,
3
+ ImageSnapshotKeyOptions,
4
+ ImageSnapshotTimeoutOptions,
5
+ } from '../../shared/types.ts'
6
+
7
+ export type MatchImageSnapshotOptions = ImageSnapshotTimeoutOptions &
8
+ ImageSnapshotCompareOptions<any> &
9
+ ImageSnapshotKeyOptions
@@ -0,0 +1,21 @@
1
+ import { glob, type GlobOptionsWithFileTypesUnset } from 'glob'
2
+ import { mkdirp } from 'mkdirp'
3
+ import { readFile, stat, writeFile } from 'node:fs/promises'
4
+
5
+ export const file = {
6
+ existFile(filePath: string) {
7
+ return stat(filePath)
8
+ .then((s) => s.isFile())
9
+ .catch(() => false)
10
+ },
11
+ glob(pattern: string | string[], options?: GlobOptionsWithFileTypesUnset | undefined) {
12
+ return glob(pattern, options)
13
+ },
14
+ mkdirp: mkdirp as (path: string) => Promise<string | undefined>,
15
+ async tryReadFile(filePath: string | undefined): Promise<Buffer | undefined> {
16
+ if (!filePath) return undefined
17
+
18
+ return readFile(filePath).catch(() => undefined)
19
+ },
20
+ writeFile,
21
+ }
@@ -1,9 +1,7 @@
1
- import type { BrowserCommandContext } from 'vitest/node'
2
-
3
1
  export function getProjectName(context: { project: { vite: { config: { test?: { name?: string | undefined } } } } }) {
4
2
  return context.project.vite.config.test?.name
5
3
  }
6
4
 
7
- export function getBrowserInstanceProjectName(context: BrowserCommandContext) {
8
- return context.project.config
5
+ export function getProjectRoot(context: { project: { config: { root: string } } }) {
6
+ return context.project.config.root
9
7
  }
@@ -1,42 +1,51 @@
1
1
  import { mkdirp } from 'mkdirp'
2
- import { dirname } from 'pathe'
2
+ import { dirname, resolve } from 'pathe'
3
3
  import type { BrowserCommandContext } from 'vitest/node'
4
- import type { ImageSnapshotTimeoutOptions } from '../client.ts'
5
4
  import { isBase64String } from '../shared/base64.ts'
5
+ import type { ImageSnapshotTimeoutOptions, PageImageSnapshotOptions } from '../shared/types.ts'
6
6
  import { browserApi } from './browser_provider/browser_api.ts'
7
- import { file } from './file.js'
7
+ import { snapshotWriter } from './snapshot_writer.ts'
8
8
 
9
9
  export async function takeSnapshot(
10
10
  context: BrowserCommandContext,
11
- filePath: string,
11
+ projectRoot: string,
12
+ relativeFilePath: string,
12
13
  subject: string,
13
14
  options: ImageSnapshotTimeoutOptions | undefined,
14
15
  ) {
15
16
  if (isBase64String(subject)) {
16
- await writeSnapshot(filePath, subject)
17
+ await snapshotWriter.writeBase64(resolve(projectRoot, relativeFilePath), subject)
17
18
  return Buffer.from(subject, 'base64')
18
19
  }
19
- return takeSnapshotByBrowser(context, filePath, subject, options)
20
+ return takeSnapshotByBrowser(context, projectRoot, relativeFilePath, subject, options)
20
21
  }
21
22
 
22
23
  export async function takeSnapshotByBrowser(
23
24
  context: BrowserCommandContext,
24
- filePath: string,
25
+ projectRoot: string,
26
+ relativeFilePath: string,
25
27
  subject: string,
26
28
  options: ImageSnapshotTimeoutOptions | undefined,
27
29
  ) {
30
+ const filePath = resolve(projectRoot, relativeFilePath)
28
31
  await mkdirp(dirname(filePath))
29
32
  const browser = browserApi(context)
30
- return browser.takeScreenshot(filePath, subject ?? 'body', {
33
+ return browser.takeScreenshot(projectRoot, relativeFilePath, subject ?? 'body', {
31
34
  timeout: options?.timeout,
32
35
  })
33
36
  }
34
37
 
35
- export async function writeSnapshot(filePath: string, subject: string) {
36
- await mkdirp(dirname(filePath))
37
- await file.writeFile(filePath, subject, { encoding: 'base64' })
38
- }
39
- export async function writeSnapshotBuffer(filePath: string, subject: Buffer) {
38
+ export async function takePageSnapshot(
39
+ context: BrowserCommandContext,
40
+ projectRoot: string,
41
+ relativeFilePath: string,
42
+ options: (PageImageSnapshotOptions & ImageSnapshotTimeoutOptions) | undefined,
43
+ ) {
44
+ const filePath = resolve(projectRoot, relativeFilePath)
40
45
  await mkdirp(dirname(filePath))
41
- await file.writeFile(filePath, subject)
46
+ const browser = browserApi(context)
47
+ return browser.takePageScreenshot(projectRoot, relativeFilePath, {
48
+ timeout: options?.timeout,
49
+ fullPage: options?.fullPage,
50
+ })
42
51
  }