@chromatic-com/vitest 0.1.7 → 0.1.9

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.js CHANGED
@@ -235,8 +235,9 @@ 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
+ import { version as vitestVersion } from "vitest/node";
240
241
  import colors2 from "tinyrainbow";
241
242
 
242
243
  // ../shared/src/utils/logger.ts
@@ -897,12 +898,6 @@ async function writeTestResult(e2eTestInfo, domSnapshots, archive, chromaticStor
897
898
  return { storiesFile };
898
899
  }
899
900
 
900
- // ../shared/src/utils/analytics.ts
901
- import { Analytics } from "@segment/analytics-node";
902
- var analytics = new Analytics({
903
- writeKey: process.env.SEGMENT_WRITE_KEY || "cwWhBWso2gwzyenW29CnCp30Kjz4DogE"
904
- });
905
-
906
901
  // ../shared/src/constants.ts
907
902
  var DEFAULT_GLOBAL_RESOURCE_ARCHIVE_TIMEOUT_MS = 1e4;
908
903
 
@@ -1112,8 +1107,9 @@ function pluralize(count, text) {
1112
1107
 
1113
1108
  // src/node/webpack-stats-reporter.ts
1114
1109
  import { existsSync as existsSync3, readFileSync } from "node:fs";
1115
- import { mkdir, rm, writeFile as writeFile2 } from "node:fs/promises";
1110
+ import { mkdir, readdir, readFile, rm, writeFile as writeFile2 } from "node:fs/promises";
1116
1111
  import { dirname, relative as relative2, resolve as resolve2, sep } from "node:path";
1112
+ import { isCSSRequest } from "vitest/node";
1117
1113
  var REPORTER_NAME2 = "chromatic-webpack-stats-reporter";
1118
1114
  var VIRTUAL_MODULE_PREFIX = "\0";
1119
1115
  var WebpackStatsReporter = class _WebpackStatsReporter {
@@ -1129,11 +1125,12 @@ var WebpackStatsReporter = class _WebpackStatsReporter {
1129
1125
  static apply(ctx, pluginOptions) {
1130
1126
  const exists = ctx.config.reporters.some(isWebpackStatsReporter);
1131
1127
  if (!exists) {
1132
- const outputFile2 = resolve2(
1133
- ctx.config.root,
1134
- pluginOptions.outputDirectory,
1135
- "preview-stats.json"
1136
- );
1128
+ const filename = [
1129
+ "preview-stats",
1130
+ ctx.config.shard && `-${ctx.config.shard.index}-${ctx.config.shard.count}`,
1131
+ ".json"
1132
+ ].filter(Boolean).join("");
1133
+ const outputFile2 = resolve2(ctx.config.root, pluginOptions.outputDirectory, filename);
1137
1134
  ctx.config.reporters.push(new _WebpackStatsReporter(ctx, { outputFile: outputFile2 }));
1138
1135
  }
1139
1136
  }
@@ -1175,14 +1172,22 @@ var WebpackStatsReporter = class _WebpackStatsReporter {
1175
1172
  continue;
1176
1173
  }
1177
1174
  const id = this.normalize(testModule.moduleId);
1175
+ const additionalModules = [];
1178
1176
  statsMap.set(id, this.createStatsMapModule(id, [...storyFiles]));
1179
1177
  if (testModule.project.config.config) {
1180
- const configId = this.normalize(testModule.project.config.config);
1181
- const configEntry = statsMap.get(configId) || this.createStatsMapModule(configId, []);
1182
- configEntry.reasons.push({ moduleName: id });
1183
- statsMap.set(configId, configEntry);
1178
+ additionalModules.push({ id: testModule.project.config.config, isInModuleGraph: false });
1179
+ }
1180
+ for (const setupFile of testModule.project.config.setupFiles) {
1181
+ additionalModules.push({ id: setupFile, isInModuleGraph: true });
1184
1182
  }
1185
- this.addModule(testModule.moduleId, statsMap, vite.moduleGraph, vite.config.cacheDir);
1183
+ this.addModule(
1184
+ testModule.moduleId,
1185
+ statsMap,
1186
+ vite.moduleGraph,
1187
+ vite.config.cacheDir,
1188
+ /* @__PURE__ */ new Set(),
1189
+ additionalModules
1190
+ );
1186
1191
  }
1187
1192
  if (!existsSync3(dirname(this.options.outputFile))) {
1188
1193
  await mkdir(dirname(this.options.outputFile), { recursive: true });
@@ -1193,7 +1198,7 @@ var WebpackStatsReporter = class _WebpackStatsReporter {
1193
1198
  "utf-8"
1194
1199
  );
1195
1200
  }
1196
- addModule(id, statsMap, moduleGraph, cacheDir, visited = /* @__PURE__ */ new Set()) {
1201
+ addModule(id, statsMap, moduleGraph, cacheDir, visited, additionalModules = []) {
1197
1202
  if (visited.has(id)) {
1198
1203
  return;
1199
1204
  }
@@ -1204,10 +1209,13 @@ var WebpackStatsReporter = class _WebpackStatsReporter {
1204
1209
  `[chromatic] Could not find module for id ${id}. Excluding from TurboSnap.`
1205
1210
  );
1206
1211
  }
1207
- const importedModules = [];
1212
+ const importedModules = [...additionalModules];
1208
1213
  for (const imported of mod.importedModules) {
1209
1214
  if (imported.id == null) {
1210
1215
  if (imported.file) {
1216
+ if (mod.file && isCSSRequest(mod.file) && !isCSSRequest(imported.file)) {
1217
+ continue;
1218
+ }
1211
1219
  importedModules.push({ id: imported.file, isInModuleGraph: false });
1212
1220
  }
1213
1221
  continue;
@@ -1307,6 +1315,34 @@ function resolveMapSources(mod, map, cacheDir) {
1307
1315
  return [resolved];
1308
1316
  });
1309
1317
  }
1318
+ async function mergePreviewStats(options) {
1319
+ const outputDirectory = resolve2(options.root, options.outputDirectory);
1320
+ const merged = /* @__PURE__ */ new Map();
1321
+ for (const filename of await readdir(outputDirectory)) {
1322
+ if (filename.startsWith("preview-stats") && filename.endsWith(".json")) {
1323
+ const fullFilename = resolve2(outputDirectory, filename);
1324
+ const stats = JSON.parse(await readFile(fullFilename, "utf-8"));
1325
+ for (const mod of stats.modules) {
1326
+ const previous = merged.get(mod.id);
1327
+ if (previous) {
1328
+ for (const reason of mod.reasons) {
1329
+ if (!previous.reasons.some((r) => r.moduleName === reason.moduleName)) {
1330
+ previous.reasons.push(reason);
1331
+ }
1332
+ }
1333
+ } else {
1334
+ merged.set(mod.id, mod);
1335
+ }
1336
+ }
1337
+ await rm(fullFilename);
1338
+ }
1339
+ }
1340
+ await writeFile2(
1341
+ resolve2(outputDirectory, "preview-stats.json"),
1342
+ JSON.stringify({ modules: Array.from(merged.values()) }, null, 2),
1343
+ "utf-8"
1344
+ );
1345
+ }
1310
1346
 
1311
1347
  // src/node/commands.ts
1312
1348
  function createCommands(options) {
@@ -1315,13 +1351,6 @@ function createCommands(options) {
1315
1351
  const savedResults = /* @__PURE__ */ new Set();
1316
1352
  const snapshots = /* @__PURE__ */ new Map();
1317
1353
  return {
1318
- /**
1319
- * Get resolved options on the client side.
1320
- * All options must be serializable at this point.
1321
- */
1322
- async __chromatic_getOptions() {
1323
- return options;
1324
- },
1325
1354
  /**
1326
1355
  * Store a `@rrweb` generated DOM snapshot for the test.
1327
1356
  * Can be called multiple times during a single test case.
@@ -1558,29 +1587,39 @@ function chromaticPlugin(userOptions = {}) {
1558
1587
  );
1559
1588
  return {
1560
1589
  name: "vitest:chromatic",
1590
+ enforce: "pre",
1591
+ resolveId(id) {
1592
+ if (id === "vitest/suite" && !vitestVersion.startsWith("4.0")) {
1593
+ return "vitest";
1594
+ }
1595
+ },
1561
1596
  config() {
1562
1597
  return {
1563
1598
  optimizeDeps: {
1564
1599
  entries: [setupFile]
1565
1600
  },
1566
1601
  test: {
1602
+ provide: {
1603
+ __chromatic_options: options
1604
+ },
1567
1605
  browser: {
1568
1606
  commands: createCommands(options)
1569
1607
  }
1570
1608
  }
1571
1609
  };
1572
1610
  },
1573
- configureVitest(context) {
1611
+ async configureVitest(context) {
1574
1612
  const project = context.project;
1575
1613
  const browser = project.config.browser;
1576
1614
  const sequence = context.vitest.config.sequence;
1615
+ const isMergeReports = project.globalConfig.mergeReports;
1577
1616
  if (!browser.enabled || browser.name !== "chromium") {
1578
1617
  return clean();
1579
1618
  }
1580
1619
  if (options.reporter.enabled) {
1581
1620
  ChromaticReporter.apply(context.vitest, options);
1582
1621
  }
1583
- if (options.turboSnap) {
1622
+ if (options.turboSnap && !isMergeReports) {
1584
1623
  WebpackStatsReporter.apply(context.vitest, options);
1585
1624
  }
1586
1625
  if (sequence.hooks === "stack") {
@@ -1613,7 +1652,14 @@ function chromaticPlugin(userOptions = {}) {
1613
1652
  }
1614
1653
  }
1615
1654
  }
1616
- if (!project.globalConfig.mergeReports) {
1655
+ if (isMergeReports) {
1656
+ if (options.turboSnap) {
1657
+ await mergePreviewStats({
1658
+ root: project.vitest.config.root,
1659
+ outputDirectory: options.outputDirectory
1660
+ });
1661
+ }
1662
+ } else {
1617
1663
  clean();
1618
1664
  }
1619
1665
  project.onTestsRerun(async () => {
@@ -1622,8 +1668,13 @@ function chromaticPlugin(userOptions = {}) {
1622
1668
  });
1623
1669
  function clean() {
1624
1670
  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 });
1671
+ rmSync(resolve4(outputDirectory, "chromatic-archives"), { recursive: true, force: true });
1672
+ if (existsSync4(outputDirectory)) {
1673
+ for (const file of readdirSync(outputDirectory)) {
1674
+ if (file.startsWith("preview-stats") && file.endsWith(".json")) {
1675
+ rmSync(resolve4(outputDirectory, file), { force: true });
1676
+ }
1677
+ }
1627
1678
  }
1628
1679
  }
1629
1680
  }
package/dist/setupFile.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/browser/setupFile.ts
2
- import { afterEach, beforeEach } from "vitest";
2
+ import { afterEach, beforeEach, inject } from "vitest";
3
3
  import { commands as commands3 } from "vitest/browser";
4
4
 
5
5
  // src/browser/public/takeSnapshot.ts
@@ -117,7 +117,7 @@ Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabl
117
117
 
118
118
  // src/browser/setupFile.ts
119
119
  beforeEach(async ({ task }) => {
120
- const options = await commands3.__chromatic_getOptions();
120
+ const options = inject("__chromatic_options");
121
121
  if (options.tags) {
122
122
  const hasMatchingTag = task.tags?.some((tag) => options.tags?.includes(tag));
123
123
  if (!hasMatchingTag) {
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.9",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,7 +32,6 @@
32
32
  ],
33
33
  "dependencies": {
34
34
  "@chromaui/rrweb-snapshot": "2.0.0-alpha.19-noAbsolute",
35
- "@segment/analytics-node": "2.1.3",
36
35
  "storybook": "10.2.13",
37
36
  "tinyrainbow": "^3.1.0"
38
37
  },