@chromatic-com/vitest 0.1.7 → 0.1.8

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 (2) hide show
  1. package/dist/plugin.js +54 -12
  2. package/package.json +1 -1
package/dist/plugin.js CHANGED
@@ -235,7 +235,7 @@ var require_srcset = __commonJS({
235
235
  });
236
236
 
237
237
  // src/node/plugin.ts
238
- import { rmSync } from "node:fs";
238
+ import { existsSync as existsSync4, readdirSync, rmSync } from "node:fs";
239
239
  import { resolve as resolve4 } from "node:path";
240
240
  import colors2 from "tinyrainbow";
241
241
 
@@ -1112,7 +1112,7 @@ function pluralize(count, text) {
1112
1112
 
1113
1113
  // src/node/webpack-stats-reporter.ts
1114
1114
  import { existsSync as existsSync3, readFileSync } from "node:fs";
1115
- import { mkdir, rm, writeFile as writeFile2 } from "node:fs/promises";
1115
+ import { mkdir, readdir, readFile, rm, writeFile as writeFile2 } from "node:fs/promises";
1116
1116
  import { dirname, relative as relative2, resolve as resolve2, sep } from "node:path";
1117
1117
  var REPORTER_NAME2 = "chromatic-webpack-stats-reporter";
1118
1118
  var VIRTUAL_MODULE_PREFIX = "\0";
@@ -1129,11 +1129,12 @@ var WebpackStatsReporter = class _WebpackStatsReporter {
1129
1129
  static apply(ctx, pluginOptions) {
1130
1130
  const exists = ctx.config.reporters.some(isWebpackStatsReporter);
1131
1131
  if (!exists) {
1132
- const outputFile2 = resolve2(
1133
- ctx.config.root,
1134
- pluginOptions.outputDirectory,
1135
- "preview-stats.json"
1136
- );
1132
+ const filename = [
1133
+ "preview-stats",
1134
+ ctx.config.shard && `-${ctx.config.shard.index}-${ctx.config.shard.count}`,
1135
+ ".json"
1136
+ ].filter(Boolean).join("");
1137
+ const outputFile2 = resolve2(ctx.config.root, pluginOptions.outputDirectory, filename);
1137
1138
  ctx.config.reporters.push(new _WebpackStatsReporter(ctx, { outputFile: outputFile2 }));
1138
1139
  }
1139
1140
  }
@@ -1307,6 +1308,34 @@ function resolveMapSources(mod, map, cacheDir) {
1307
1308
  return [resolved];
1308
1309
  });
1309
1310
  }
1311
+ async function mergePreviewStats(options) {
1312
+ const outputDirectory = resolve2(options.root, options.outputDirectory);
1313
+ const merged = /* @__PURE__ */ new Map();
1314
+ for (const filename of await readdir(outputDirectory)) {
1315
+ if (filename.startsWith("preview-stats") && filename.endsWith(".json")) {
1316
+ const fullFilename = resolve2(outputDirectory, filename);
1317
+ const stats = JSON.parse(await readFile(fullFilename, "utf-8"));
1318
+ for (const mod of stats.modules) {
1319
+ const previous = merged.get(mod.id);
1320
+ if (previous) {
1321
+ for (const reason of mod.reasons) {
1322
+ if (!previous.reasons.some((r) => r.moduleName === reason.moduleName)) {
1323
+ previous.reasons.push(reason);
1324
+ }
1325
+ }
1326
+ } else {
1327
+ merged.set(mod.id, mod);
1328
+ }
1329
+ }
1330
+ await rm(fullFilename);
1331
+ }
1332
+ }
1333
+ await writeFile2(
1334
+ resolve2(outputDirectory, "preview-stats.json"),
1335
+ JSON.stringify({ modules: Array.from(merged.values()) }, null, 2),
1336
+ "utf-8"
1337
+ );
1338
+ }
1310
1339
 
1311
1340
  // src/node/commands.ts
1312
1341
  function createCommands(options) {
@@ -1570,17 +1599,18 @@ function chromaticPlugin(userOptions = {}) {
1570
1599
  }
1571
1600
  };
1572
1601
  },
1573
- configureVitest(context) {
1602
+ async configureVitest(context) {
1574
1603
  const project = context.project;
1575
1604
  const browser = project.config.browser;
1576
1605
  const sequence = context.vitest.config.sequence;
1606
+ const isMergeReports = project.globalConfig.mergeReports;
1577
1607
  if (!browser.enabled || browser.name !== "chromium") {
1578
1608
  return clean();
1579
1609
  }
1580
1610
  if (options.reporter.enabled) {
1581
1611
  ChromaticReporter.apply(context.vitest, options);
1582
1612
  }
1583
- if (options.turboSnap) {
1613
+ if (options.turboSnap && !isMergeReports) {
1584
1614
  WebpackStatsReporter.apply(context.vitest, options);
1585
1615
  }
1586
1616
  if (sequence.hooks === "stack") {
@@ -1613,7 +1643,14 @@ function chromaticPlugin(userOptions = {}) {
1613
1643
  }
1614
1644
  }
1615
1645
  }
1616
- if (!project.globalConfig.mergeReports) {
1646
+ if (isMergeReports) {
1647
+ if (options.turboSnap) {
1648
+ await mergePreviewStats({
1649
+ root: project.vitest.config.root,
1650
+ outputDirectory: options.outputDirectory
1651
+ });
1652
+ }
1653
+ } else {
1617
1654
  clean();
1618
1655
  }
1619
1656
  project.onTestsRerun(async () => {
@@ -1622,8 +1659,13 @@ function chromaticPlugin(userOptions = {}) {
1622
1659
  });
1623
1660
  function clean() {
1624
1661
  const outputDirectory = resolve4(project.vitest.config.root, options.outputDirectory);
1625
- for (const output of ["chromatic-archives", "preview-stats.json"]) {
1626
- rmSync(resolve4(outputDirectory, output), { recursive: true, force: true });
1662
+ rmSync(resolve4(outputDirectory, "chromatic-archives"), { recursive: true, force: true });
1663
+ if (existsSync4(outputDirectory)) {
1664
+ for (const file of readdirSync(outputDirectory)) {
1665
+ if (file.startsWith("preview-stats") && file.endsWith(".json")) {
1666
+ rmSync(resolve4(outputDirectory, file), { force: true });
1667
+ }
1668
+ }
1627
1669
  }
1628
1670
  }
1629
1671
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromatic-com/vitest",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",