@chromatic-com/vitest 0.0.7 → 0.0.8-24ca304-20260520092839

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/dist/index.d.ts CHANGED
@@ -3,15 +3,59 @@
3
3
  */
4
4
  declare function takeSnapshot(name?: string): Promise<void>;
5
5
 
6
+ declare global {
7
+ interface Window {
8
+ FontFace: typeof FontFace;
9
+ }
10
+ }
11
+
12
+ interface ChromaticConfig {
13
+ /** https://www.chromatic.com/docs/delay/ */
14
+ delay?: number;
15
+ /** https://www.chromatic.com/docs/threshold/#anti-aliasing */
16
+ diffIncludeAntiAliasing?: boolean;
17
+ /** https://www.chromatic.com/docs/threshold/#setting-the-threshold */
18
+ diffThreshold?: number;
19
+ /** Disable the capture that happens automatically at the end of a test when using the Chromatic test fixture */
20
+ disableAutoSnapshot?: boolean;
21
+ /** https://www.chromatic.com/docs/media-features/#test-high-contrast-color-schemes */
22
+ forcedColors?: string;
23
+ /** https://www.chromatic.com/docs/animations/#css-animations */
24
+ pauseAnimationAtEnd?: boolean;
25
+ /** https://www.chromatic.com/docs/media-features/#verify-reduced-motion-animations */
26
+ prefersReducedMotion?: string;
27
+ /**
28
+ * Specify a network timeout, in milliseconds. This is the maximum amount of time that
29
+ * each test will wait for the network to be idle while archiving resources.
30
+ */
31
+ resourceArchiveTimeout?: number;
32
+ /**
33
+ * Domains (besides where the test is being run from) that assets should be archived from
34
+ * (needed when, for example, CI environment can't access the archives later on)
35
+ * ex: www.some-domain.com
36
+ */
37
+ assetDomains?: string[];
38
+ /** Crop snapshots to the viewport size. */
39
+ cropToViewport?: boolean;
40
+ /** CSS selectors of elements to ignore when comparing snapshots. */
41
+ ignoreSelectors?: string[];
42
+ }
43
+
44
+ /**
45
+ * Options that can be set per test, suite or module via `configure()`
46
+ * - `assetDomains` is excluded as it can only be configured globally on the plugin
47
+ */
48
+ type ConfigureOptions = Omit<ChromaticConfig, 'assetDomains'>;
49
+
6
50
  /**
7
- * Disable automatic test snapshotting for specific scope.
51
+ * Configure options for the current scope.
8
52
  *
9
- * When called within `test()`, it disables snapshotting for that test:
53
+ * When called within `test()`, the configuration applies only to that test:
10
54
  * ```jsx
11
- * import { disableAutoSnapshot } from "@chromatic-com/vitest";
55
+ * import { configure } from "@chromatic-com/vitest";
12
56
  *
13
57
  * test("accordion", async () => { // ❌ Not snapshotted
14
- * disableAutoSnapshot(); // Scopes to this test only
58
+ * configure({ disableAutoSnapshot: true }); // Scopes to this test only
15
59
  * await render(<Accordion />);
16
60
  * });
17
61
  *
@@ -20,16 +64,16 @@ declare function takeSnapshot(name?: string): Promise<void>;
20
64
  * });
21
65
  * ```
22
66
  *
23
- * When called within `describe()`, it disables snapshotting for all tests and suites within that describe block.
67
+ * When called within `describe()`, the configuration applies to all tests and suites within that describe block.
24
68
  * ```
25
- * import { disableAutoSnapshot } from "@chromatic-com/vitest";
69
+ * import { configure } from "@chromatic-com/vitest";
26
70
  *
27
71
  * test("accordion", async () => { // ✅ Snapshotted automatically
28
72
  * await render(<Accordion />);
29
73
  * });
30
74
  *
31
75
  * describe("button", () => {
32
- * disableAutoSnapshot(); // Scopes to all tests (and nested suites) in this suite
76
+ * configure({ disableAutoSnapshot: true }); // Scopes to all tests (and nested suites) in this suite
33
77
  *
34
78
  * test("default", async () => { // ❌ Not snapshotted
35
79
  * await render(<Button />);
@@ -43,13 +87,13 @@ declare function takeSnapshot(name?: string): Promise<void>;
43
87
  * });
44
88
  * ```
45
89
  *
46
- * When called at the top level, it disables snapshotting for all tests in the file:
90
+ * When called at the top level, the configuration applies to all tests in the file:
47
91
  * ```jsx
48
92
  * import { describe, test } from "vitest";
49
- * import { disableAutoSnapshot } from "@chromatic-com/vitest";
93
+ * import { configure } from "@chromatic-com/vitest";
50
94
  *
51
95
  * // Scoped to whole test module (file), affects all tests and suites in this file
52
- * disableAutoSnapshot();
96
+ * configure({ disableAutoSnapshot: true });
53
97
  *
54
98
  * test("accordion", async () => { // ❌ Not snapshotted
55
99
  * await render(<Accordion />);
@@ -61,9 +105,8 @@ declare function takeSnapshot(name?: string): Promise<void>;
61
105
  * });
62
106
  * });
63
107
  * ```
64
- *
65
108
  */
66
- declare function disableAutoSnapshot(): void;
109
+ declare function configure(options: ConfigureOptions): void;
67
110
 
68
111
  /**
69
112
  * Wait for network to be idle, meaning no new network requests for at least `idleNetworkInterval` ms.
@@ -80,4 +123,4 @@ declare function disableAutoSnapshot(): void;
80
123
  */
81
124
  declare function waitForIdleNetwork(timeout: number): Promise<void>;
82
125
 
83
- export { disableAutoSnapshot, takeSnapshot, waitForIdleNetwork };
126
+ export { configure, takeSnapshot, waitForIdleNetwork };
package/dist/index.js CHANGED
@@ -108,22 +108,29 @@ async function toDataURL(url) {
108
108
  }
109
109
  __name(toDataURL, "toDataURL");
110
110
 
111
- // src/browser/public/autoSnapshot.ts
111
+ // src/browser/public/configure.ts
112
112
  import { beforeAll } from "vitest";
113
- function disableAutoSnapshot() {
113
+ function configure(options) {
114
114
  if (!isChromium()) {
115
115
  return;
116
116
  }
117
117
  const test = getCurrentTest();
118
118
  if (test) {
119
- test.meta.__chromatic_autoSnapshot = false;
119
+ test.meta.__chromatic_options = {
120
+ ...test.meta.__chromatic_options,
121
+ ...options
122
+ };
120
123
  return;
121
124
  }
122
125
  beforeAll(({}, suite) => {
123
126
  traverseTests(suite);
124
127
  function traverseTests(task) {
125
128
  if (task.type === "test") {
126
- task.meta.__chromatic_autoSnapshot = false;
129
+ const test2 = task;
130
+ test2.meta.__chromatic_options = {
131
+ ...test2.meta.__chromatic_options,
132
+ ...options
133
+ };
127
134
  return;
128
135
  }
129
136
  task.tasks.forEach(traverseTests);
@@ -131,7 +138,7 @@ function disableAutoSnapshot() {
131
138
  __name(traverseTests, "traverseTests");
132
139
  });
133
140
  }
134
- __name(disableAutoSnapshot, "disableAutoSnapshot");
141
+ __name(configure, "configure");
135
142
 
136
143
  // src/browser/public/waitForIdleNetwork.ts
137
144
  import { commands as commands2 } from "vitest/browser";
@@ -151,7 +158,7 @@ Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabl
151
158
  }
152
159
  __name(waitForIdleNetwork, "waitForIdleNetwork");
153
160
  export {
154
- disableAutoSnapshot,
161
+ configure,
155
162
  takeSnapshot,
156
163
  waitForIdleNetwork
157
164
  };
package/dist/plugin.js CHANGED
@@ -4175,7 +4175,7 @@ function createCommands(options) {
4175
4175
  * Write captured snapshots and network resources to disk.
4176
4176
  * Should be called only once per test as it also clears resources.
4177
4177
  */
4178
- async __chromatic_writeTestResult(context, id) {
4178
+ async __chromatic_writeTestResult(context, id, testOptions = {}) {
4179
4179
  const entity = context.project.vitest.state.getReportedEntityById(id);
4180
4180
  assert(entity?.type === "test", `Expected entity with id ${id} to be a test, found ${entity?.type}`);
4181
4181
  const { archive, sessionSnapshots } = await onTestCleanup(id);
@@ -4188,20 +4188,26 @@ function createCommands(options) {
4188
4188
  pseudoClassIds
4189
4189
  };
4190
4190
  }
4191
- await writeTestResult({
4192
- outputDir: resolve(context.project.vitest.config.root, options.outputDirectory),
4193
- pageUrl: context.page.url(),
4194
- titlePath: getNames(entity)
4195
- }, snapshotBuffers, archive, {
4196
- delay: options.delay,
4197
- diffIncludeAntiAliasing: options.diffIncludeAntiAliasing,
4198
- diffThreshold: options.diffThreshold,
4199
- forcedColors: options.forcedColors,
4200
- pauseAnimationAtEnd: options.pauseAnimationAtEnd,
4201
- prefersReducedMotion: options.prefersReducedMotion,
4202
- cropToViewport: options.cropToViewport,
4203
- ignoreSelectors: options.ignoreSelectors
4204
- });
4191
+ await writeTestResult(
4192
+ {
4193
+ outputDir: resolve(context.project.vitest.config.root, options.outputDirectory),
4194
+ pageUrl: context.page.url(),
4195
+ titlePath: getNames(entity)
4196
+ },
4197
+ snapshotBuffers,
4198
+ archive,
4199
+ // Remove undefined options with stringify/parse
4200
+ JSON.parse(JSON.stringify({
4201
+ delay: testOptions.delay ?? options.delay,
4202
+ diffIncludeAntiAliasing: testOptions.diffIncludeAntiAliasing ?? options.diffIncludeAntiAliasing,
4203
+ diffThreshold: testOptions.diffThreshold ?? options.diffThreshold,
4204
+ forcedColors: testOptions.forcedColors ?? options.forcedColors,
4205
+ pauseAnimationAtEnd: testOptions.pauseAnimationAtEnd ?? options.pauseAnimationAtEnd,
4206
+ prefersReducedMotion: testOptions.prefersReducedMotion ?? options.prefersReducedMotion,
4207
+ cropToViewport: testOptions.cropToViewport ?? options.cropToViewport,
4208
+ ignoreSelectors: testOptions.ignoreSelectors ?? options.ignoreSelectors
4209
+ }))
4210
+ );
4205
4211
  },
4206
4212
  /**
4207
4213
  * Clear test state without writing test results.
package/dist/setupFile.js CHANGED
@@ -142,7 +142,7 @@ beforeEach(async ({ task }) => {
142
142
  }
143
143
  task.meta.__chromatic_isRegistered = true;
144
144
  task.meta.__chromatic_isTakeSnapshotCalled = false;
145
- task.meta.__chromatic_autoSnapshot ??= !options.disableAutoSnapshot;
145
+ task.meta.__chromatic_options ||= {};
146
146
  await commands3.__chromatic_interceptFetch(task.id);
147
147
  return /* @__PURE__ */ __name(async function beforeEachCleanup() {
148
148
  const { __chromatic_isTakeSnapshotCalled: isTakeSnapshotCalled, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
@@ -156,11 +156,11 @@ beforeEach(async ({ task }) => {
156
156
  await commands3.__chromatic_stopWithoutSnapshots(task.id);
157
157
  return cleanup();
158
158
  }
159
- await commands3.__chromatic_writeTestResult(task.id);
159
+ await commands3.__chromatic_writeTestResult(task.id, task.meta.__chromatic_options);
160
160
  return cleanup();
161
161
  }, "beforeEachCleanup");
162
162
  function cleanup() {
163
- task.meta.__chromatic_autoSnapshot = void 0;
163
+ task.meta.__chromatic_options = void 0;
164
164
  task.meta.__chromatic_isTakeSnapshotCalled = void 0;
165
165
  task.meta.__chromatic_isRegistered = void 0;
166
166
  task.meta.__chromatic_pendingTakeSnapshots = void 0;
@@ -168,19 +168,20 @@ beforeEach(async ({ task }) => {
168
168
  __name(cleanup, "cleanup");
169
169
  });
170
170
  afterEach(async ({ task }) => {
171
- const { __chromatic_autoSnapshot: autoSnapshot, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
171
+ const { __chromatic_options: options, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
172
172
  if (!isRegistered) {
173
173
  return;
174
174
  }
175
175
  if (pendingTakeSnapshots?.length) {
176
176
  throw new PendingSnapshotsError(pendingTakeSnapshots);
177
177
  }
178
- if (!autoSnapshot) {
178
+ if (options.disableAutoSnapshot) {
179
179
  return;
180
180
  }
181
- const options = await commands3.__chromatic_getOptions();
182
- if (options.resourceArchiveTimeout !== 0) {
183
- await waitForIdleNetwork(options.resourceArchiveTimeout);
181
+ const globals = await commands3.__chromatic_getOptions();
182
+ const resourceArchiveTimeout = options.resourceArchiveTimeout ?? globals.resourceArchiveTimeout;
183
+ if (resourceArchiveTimeout !== 0) {
184
+ await waitForIdleNetwork(resourceArchiveTimeout);
184
185
  }
185
186
  await takeSnapshot(void 0, {
186
187
  ignoreUnawaited: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromatic-com/vitest",
3
- "version": "0.0.7",
3
+ "version": "0.0.8-24ca304-20260520092839",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",