@chromatic-com/vitest 0.0.9 → 0.0.10

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/plugin.d.ts CHANGED
@@ -64,6 +64,25 @@ interface Options extends ChromaticConfig {
64
64
  * @default 100
65
65
  */
66
66
  idleNetworkInterval?: number;
67
+ /**
68
+ * Logger used to report status of captured archives.
69
+ *
70
+ * @default true
71
+ */
72
+ reporter?: boolean | {
73
+ /**
74
+ * Whether the reporter is enabled.
75
+ *
76
+ * @default true
77
+ */
78
+ enabled?: boolean;
79
+ /**
80
+ * Log information about captured archives during test run.
81
+ *
82
+ * @default true
83
+ */
84
+ verbose?: boolean;
85
+ };
67
86
  }
68
87
 
69
88
  /**
package/dist/plugin.js CHANGED
@@ -3369,8 +3369,8 @@ var require_srcset = __commonJS({
3369
3369
 
3370
3370
  // src/node/plugin.ts
3371
3371
  import { rmSync } from "node:fs";
3372
- import { resolve as resolve2 } from "node:path";
3373
- import colors from "tinyrainbow";
3372
+ import { resolve as resolve3 } from "node:path";
3373
+ import colors2 from "tinyrainbow";
3374
3374
 
3375
3375
  // ../shared/src/utils/logger.ts
3376
3376
  var logger = process.env.LOG ? console : {
@@ -4053,7 +4053,7 @@ var DEFAULT_GLOBAL_RESOURCE_ARCHIVE_TIMEOUT_MS = 1e4;
4053
4053
 
4054
4054
  // src/node/commands.ts
4055
4055
  import assert from "node:assert";
4056
- import { resolve } from "node:path";
4056
+ import { resolve as resolve2 } from "node:path";
4057
4057
 
4058
4058
  // src/node/NetworkIdleTracker.ts
4059
4059
  var NetworkIdleTimeoutError = class NetworkIdleTimeoutError2 extends Error {
@@ -4097,9 +4097,9 @@ var NetworkIdleTracker = class _NetworkIdleTracker {
4097
4097
  if (this.pending.size === 0 && this.idleTimer === null) {
4098
4098
  return Promise.resolve();
4099
4099
  }
4100
- return new Promise((resolve3, reject) => {
4100
+ return new Promise((resolve4, reject) => {
4101
4101
  const timeout = setTimeout(() => reject(new NetworkIdleTimeoutError("Timed out waiting for network to be idle")), rejectTimeout);
4102
- this.idleResolvers.push(resolve3, () => clearTimeout(timeout));
4102
+ this.idleResolvers.push(resolve4, () => clearTimeout(timeout));
4103
4103
  });
4104
4104
  }
4105
4105
  onComplete = (params) => {
@@ -4127,6 +4127,128 @@ var NetworkIdleTracker = class _NetworkIdleTracker {
4127
4127
  }
4128
4128
  };
4129
4129
 
4130
+ // src/node/reporter.ts
4131
+ import { relative, resolve } from "node:path";
4132
+ import colors from "tinyrainbow";
4133
+
4134
+ // src/constants.ts
4135
+ var DEFAULT_OUTPUT_DIR = ".vitest/chromatic";
4136
+
4137
+ // src/node/reporter.ts
4138
+ var REPORTER_NAME = "chromatic-reporter";
4139
+ var ChromaticReporter = class _ChromaticReporter {
4140
+ static {
4141
+ __name(this, "ChromaticReporter");
4142
+ }
4143
+ ctx;
4144
+ options;
4145
+ snapshotCountPerEntity;
4146
+ name = REPORTER_NAME;
4147
+ constructor(ctx, options, snapshotCountPerEntity = /* @__PURE__ */ new Map()) {
4148
+ this.ctx = ctx;
4149
+ this.options = options;
4150
+ this.snapshotCountPerEntity = snapshotCountPerEntity;
4151
+ }
4152
+ /**
4153
+ * Add `ChromaticReporter` to Vitest reporters unless it's already included.
4154
+ */
4155
+ static apply(ctx, pluginOptions) {
4156
+ const options = {
4157
+ verbose: pluginOptions.reporter.verbose,
4158
+ outputDirectory: pluginOptions.outputDirectory,
4159
+ builtInReporter: "default"
4160
+ };
4161
+ for (const reporter of ctx.config.reporters) {
4162
+ if (isChromaticReporter(reporter)) {
4163
+ return;
4164
+ }
4165
+ if (!Array.isArray(reporter)) {
4166
+ continue;
4167
+ }
4168
+ const reporterName = reporter[0];
4169
+ if (reporterName === "tree" || reporterName === "default" || reporterName === "verbose") {
4170
+ options.builtInReporter = reporterName;
4171
+ }
4172
+ }
4173
+ ctx.config.reporters.push(new _ChromaticReporter(ctx, options));
4174
+ }
4175
+ /**
4176
+ * Custom reporter life-cycle called when `takeSnapshot()` is called.
4177
+ */
4178
+ static onSnapshot(ctx, test) {
4179
+ const reporter = ctx.config.reporters.find(isChromaticReporter);
4180
+ reporter?._onSnapshot(test);
4181
+ }
4182
+ onTestCaseResult(testCase) {
4183
+ if (this.options.builtInReporter !== "verbose") {
4184
+ return;
4185
+ }
4186
+ this.logSnapshots({
4187
+ count: this.snapshotCountPerEntity.get(testCase.id) ?? 0,
4188
+ indentation: 3,
4189
+ projectName: testCase.project.name
4190
+ });
4191
+ }
4192
+ onTestModuleEnd(testModule) {
4193
+ if (this.options.builtInReporter !== "default" && this.options.builtInReporter !== "tree") {
4194
+ return;
4195
+ }
4196
+ this.logSnapshots({
4197
+ count: this.snapshotCountPerEntity.get(testModule.id) ?? 0,
4198
+ indentation: this.options.builtInReporter === "default" ? 3 : 2,
4199
+ projectName: this.options.builtInReporter === "default" && testModule.project.name
4200
+ });
4201
+ }
4202
+ onTestRunEnd() {
4203
+ const snapshotCount = [
4204
+ ...this.snapshotCountPerEntity.values()
4205
+ ].reduce((a, b) => a + b, 0);
4206
+ this.snapshotCountPerEntity.clear();
4207
+ if (snapshotCount === 0) {
4208
+ return;
4209
+ }
4210
+ const output = resolve(this.ctx.config.root, this.options.outputDirectory);
4211
+ const separator = colors.dim("\u2500".repeat(this.ctx.logger.getColumns()));
4212
+ let uploadCommand = "chromatic --vitest --project-token=<TOKEN>";
4213
+ if (this.options.outputDirectory !== DEFAULT_OUTPUT_DIR || this.ctx.config.root !== process.cwd()) {
4214
+ uploadCommand = `CHROMATIC_ARCHIVE_LOCATION=${relative(process.cwd(), output)} ${uploadCommand}`;
4215
+ }
4216
+ this.ctx.logger.log(`${separator}`, `
4217
+ ${colors.inverse(" Chromatic Visual Regression ")}`, `
4218
+
4219
+ ${colors.green("\u2713")} ${snapshotCount} ${pluralize(snapshotCount, "archive")} captured`, `
4220
+
4221
+ Archives saved in ${colors.dim(output)}`, `
4222
+ To upload archives into Chromatic run ${colors.green(uploadCommand)}`, `
4223
+
4224
+ ${separator}
4225
+ `);
4226
+ }
4227
+ logSnapshots(options) {
4228
+ if (!this.options.verbose || options.count === 0) {
4229
+ return;
4230
+ }
4231
+ const snapshots = `(${options.count} ${pluralize(options.count, "archive")} captured)`;
4232
+ if (options.projectName) {
4233
+ options.indentation += options.projectName.length + 2;
4234
+ }
4235
+ this.ctx.logger.log(`${" ".repeat(options.indentation)} ${colors.dim(snapshots)}`);
4236
+ }
4237
+ _onSnapshot(test) {
4238
+ const key = this.options.builtInReporter === "verbose" ? test.id : test.module.id;
4239
+ const previousCount = this.snapshotCountPerEntity.get(key) ?? 0;
4240
+ this.snapshotCountPerEntity.set(key, previousCount + 1);
4241
+ }
4242
+ };
4243
+ function isChromaticReporter(reporter) {
4244
+ return "name" in reporter && reporter.name === REPORTER_NAME;
4245
+ }
4246
+ __name(isChromaticReporter, "isChromaticReporter");
4247
+ function pluralize(count, text) {
4248
+ return count === 1 ? text : `${text}s`;
4249
+ }
4250
+ __name(pluralize, "pluralize");
4251
+
4130
4252
  // src/node/commands.ts
4131
4253
  function createCommands(options) {
4132
4254
  const resourceArchivers = /* @__PURE__ */ new Map();
@@ -4145,6 +4267,8 @@ function createCommands(options) {
4145
4267
  * Can be called multiple times during a single test case.
4146
4268
  */
4147
4269
  async __chromatic_uploadDOMSnapshot(context, id, snapshot, pseudoClassIds, name) {
4270
+ const entity = context.project.vitest.state.getReportedEntityById(id);
4271
+ assert(entity?.type === "test", `Expected entity with id ${id} to be a test, found ${entity?.type}`);
4148
4272
  let sessionSnapshots = snapshots.get(id);
4149
4273
  if (!sessionSnapshots) {
4150
4274
  sessionSnapshots = /* @__PURE__ */ new Map();
@@ -4161,6 +4285,7 @@ function createCommands(options) {
4161
4285
  viewport,
4162
4286
  pseudoClassIds
4163
4287
  });
4288
+ ChromaticReporter.onSnapshot(context.project.vitest, entity);
4164
4289
  },
4165
4290
  /**
4166
4291
  * Wait for network to be idle, meaning no new network requests for at least `idleNetworkInterval` ms.
@@ -4202,7 +4327,7 @@ function createCommands(options) {
4202
4327
  };
4203
4328
  }
4204
4329
  await writeTestResult({
4205
- outputDir: resolve(context.project.vitest.config.root, options.outputDirectory),
4330
+ outputDir: resolve2(context.project.vitest.config.root, options.outputDirectory),
4206
4331
  pageUrl: context.page.url(),
4207
4332
  titlePath: testOptions.title ? [
4208
4333
  testOptions.title
@@ -4284,9 +4409,6 @@ function getSnapshotPrefix(test) {
4284
4409
  }
4285
4410
  __name(getSnapshotPrefix, "getSnapshotPrefix");
4286
4411
 
4287
- // src/constants.ts
4288
- var DEFAULT_OUTPUT_DIR = ".vitest/chromatic";
4289
-
4290
4412
  // src/node/plugin.ts
4291
4413
  var DEFAULT_TAG_DESCRIPTION = "Visual Regression Tests for `@chromatic-com/vitest`";
4292
4414
  function chromaticPlugin(userOptions = {}) {
@@ -4296,10 +4418,11 @@ function chromaticPlugin(userOptions = {}) {
4296
4418
  outputDirectory: DEFAULT_OUTPUT_DIR,
4297
4419
  resourceArchiveTimeout: DEFAULT_GLOBAL_RESOURCE_ARCHIVE_TIMEOUT_MS,
4298
4420
  idleNetworkInterval: 100,
4299
- ...userOptions
4421
+ ...userOptions,
4422
+ reporter: resolveReporterOptions(userOptions.reporter)
4300
4423
  };
4301
4424
  const isDist = import.meta.url.includes("dist/plugin.js");
4302
- const setupFile = resolve2(import.meta.dirname, isDist ? "./setupFile.js" : "../browser/setupFile.ts");
4425
+ const setupFile = resolve3(import.meta.dirname, isDist ? "./setupFile.js" : "../browser/setupFile.ts");
4303
4426
  return {
4304
4427
  name: "vitest:chromatic",
4305
4428
  config() {
@@ -4323,16 +4446,19 @@ function chromaticPlugin(userOptions = {}) {
4323
4446
  if (!browser.enabled || browser.name !== "chromium") {
4324
4447
  return clean();
4325
4448
  }
4449
+ if (options.reporter.enabled) {
4450
+ ChromaticReporter.apply(context.vitest, options);
4451
+ }
4326
4452
  if (sequence.hooks === "stack") {
4327
4453
  project.config.setupFiles.push(setupFile);
4328
4454
  } else if (sequence.hooks === "list") {
4329
4455
  project.config.setupFiles.unshift(setupFile);
4330
4456
  } else {
4331
4457
  project.config.setupFiles.push(setupFile);
4332
- context.vitest.logger.warn(colors.bgYellow(colors.black(" chromatic ")), colors.yellow(`Using { sequence.hooks: 'parallel' } may cause unstable snapshots. Please set 'sequence.hooks' to 'list' or 'stack' to ensure reliable snapshot ordering.`));
4458
+ context.vitest.logger.warn(colors2.bgYellow(colors2.black(" chromatic ")), colors2.yellow(`Using { sequence.hooks: 'parallel' } may cause unstable snapshots. Please set 'sequence.hooks' to 'list' or 'stack' to ensure reliable snapshot ordering.`));
4333
4459
  }
4334
4460
  if (options.tags && context.vitest.version.startsWith("4.0")) {
4335
- context.vitest.logger.warn(colors.bgYellow(colors.black(" chromatic ")), colors.yellow(`Tags cannot be used with Vitest ${context.vitest.version}. Please upgrade to Vitest 4.1 or later to use this feature.`));
4461
+ context.vitest.logger.warn(colors2.bgYellow(colors2.black(" chromatic ")), colors2.yellow(`Tags cannot be used with Vitest ${context.vitest.version}. Please upgrade to Vitest 4.1 or later to use this feature.`));
4336
4462
  }
4337
4463
  if (options.tags) {
4338
4464
  project.config.tags ||= [];
@@ -4354,7 +4480,7 @@ function chromaticPlugin(userOptions = {}) {
4354
4480
  await project.browser?.triggerCommand("__chromatic_reset", {});
4355
4481
  });
4356
4482
  function clean() {
4357
- rmSync(resolve2(project.vitest.config.root, options.outputDirectory, "chromatic-archives"), {
4483
+ rmSync(resolve3(project.vitest.config.root, options.outputDirectory, "chromatic-archives"), {
4358
4484
  recursive: true,
4359
4485
  force: true
4360
4486
  });
@@ -4364,6 +4490,25 @@ function chromaticPlugin(userOptions = {}) {
4364
4490
  };
4365
4491
  }
4366
4492
  __name(chromaticPlugin, "chromaticPlugin");
4493
+ function resolveReporterOptions(reporter) {
4494
+ if (reporter == void 0 || reporter === true) {
4495
+ return {
4496
+ enabled: true,
4497
+ verbose: true
4498
+ };
4499
+ }
4500
+ if (reporter === false) {
4501
+ return {
4502
+ enabled: false,
4503
+ verbose: false
4504
+ };
4505
+ }
4506
+ return {
4507
+ enabled: reporter.enabled ?? true,
4508
+ verbose: reporter.verbose ?? true
4509
+ };
4510
+ }
4511
+ __name(resolveReporterOptions, "resolveReporterOptions");
4367
4512
  export {
4368
4513
  chromaticPlugin
4369
4514
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromatic-com/vitest",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",