@chromatic-com/vitest 0.1.0 → 0.1.2

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.js CHANGED
@@ -154,7 +154,7 @@ async function waitForIdleNetwork(timeout) {
154
154
  throw new TypeError(`waitForIdleNetwork() cannot be called in a test that is not registered for Chromatic plugin.
155
155
  Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`);
156
156
  }
157
- return await commands2.__chromatic_waitForIdleNetwork(test.id, timeout);
157
+ return await commands2.__chromatic_waitForIdleNetwork(timeout);
158
158
  }
159
159
  __name(waitForIdleNetwork, "waitForIdleNetwork");
160
160
  export {
package/dist/plugin.js CHANGED
@@ -3406,9 +3406,10 @@ var ResourceArchiver = class {
3406
3406
  * Currently active requests
3407
3407
  */
3408
3408
  pendingRequests = /* @__PURE__ */ new Set();
3409
- constructor(cdpClient, allowedDomains, httpCredentials) {
3409
+ constructor(cdpClient, allowedDomains, httpCredentials, firstUrl) {
3410
3410
  this.client = cdpClient;
3411
3411
  this.httpCredentials = httpCredentials;
3412
+ this.firstUrl = firstUrl;
3412
3413
  this.assetDomains = (allowedDomains || []).map((domain) => {
3413
3414
  if (domain.startsWith("http")) {
3414
3415
  return domain;
@@ -3518,12 +3519,25 @@ var ResourceArchiver = class {
3518
3519
  const contentTypeHeader = responseHeaders.find(({ name }) => name.toLowerCase() === "content-type");
3519
3520
  const isFirstRequest = requestUrl.toString() === this.firstUrl.toString();
3520
3521
  if (isRequestFromAllowedDomain && !isFirstRequest) {
3522
+ logger.log("Archiving request", {
3523
+ url: request.url,
3524
+ statusCode: responseStatusCode,
3525
+ statusText: responseStatusText,
3526
+ contentType: contentTypeHeader?.value
3527
+ });
3521
3528
  this.archive[request.url] = {
3522
3529
  statusCode: responseStatusCode,
3523
3530
  statusText: responseStatusText,
3524
3531
  body: Buffer.from(body, base64Encoded ? "base64" : "utf8"),
3525
3532
  contentType: contentTypeHeader?.value
3526
3533
  };
3534
+ } else {
3535
+ logger.log("Skipping archiving of request", {
3536
+ url: request.url,
3537
+ firstUrl: this.firstUrl.toString(),
3538
+ isFirstRequest,
3539
+ isRequestFromAllowedDomain
3540
+ });
3527
3541
  }
3528
3542
  await this.clientSend(request, "Fetch.continueRequest", {
3529
3543
  requestId
@@ -3533,6 +3547,7 @@ var ResourceArchiver = class {
3533
3547
 
3534
3548
  // ../shared/src/write-archive/index.ts
3535
3549
  import { join } from "node:path";
3550
+ import { existsSync as existsSync2 } from "node:fs";
3536
3551
 
3537
3552
  // ../shared/src/utils/filePaths.ts
3538
3553
  import { existsSync, mkdirSync } from "fs";
@@ -4025,10 +4040,13 @@ async function writeTestResult(e2eTestInfo, domSnapshots, archive, chromaticStor
4025
4040
  const archiveFile = new ArchiveFile(url, response, pageUrl);
4026
4041
  const origSrcPath = archiveFile.originalSrc();
4027
4042
  const fileSystemPath = archiveFile.toFileSystemPath();
4043
+ const responsePath = join(archiveDir, fileSystemPath);
4028
4044
  if (origSrcPath !== fileSystemPath) {
4029
4045
  sourceMap.set(origSrcPath, fileSystemPath);
4030
4046
  }
4031
- await outputFile(join(archiveDir, fileSystemPath), response.body);
4047
+ if (!existsSync2(responsePath)) {
4048
+ await outputFile(responsePath, response.body);
4049
+ }
4032
4050
  }));
4033
4051
  await Promise.all(Object.entries(domSnapshots).map(async ([name, domSnapshot]) => {
4034
4052
  const snapshot = new DOMSnapshot(domSnapshot);
@@ -4084,7 +4102,6 @@ var NetworkIdleTracker = class _NetworkIdleTracker {
4084
4102
  }
4085
4103
  static async create(cdp, idleTimeout) {
4086
4104
  const tracker = new _NetworkIdleTracker(cdp, idleTimeout);
4087
- await tracker.watch();
4088
4105
  return tracker;
4089
4106
  }
4090
4107
  async watch() {
@@ -4093,11 +4110,13 @@ var NetworkIdleTracker = class _NetworkIdleTracker {
4093
4110
  this.cdp.on("Network.loadingFailed", this.onComplete);
4094
4111
  await this.cdp.send("Network.enable");
4095
4112
  }
4096
- async off() {
4113
+ async unwatch() {
4097
4114
  this.cdp.off("Network.requestWillBeSent", this.onRequest);
4098
4115
  this.cdp.off("Network.loadingFinished", this.onComplete);
4099
4116
  this.cdp.off("Network.loadingFailed", this.onComplete);
4100
4117
  this.clearTimer();
4118
+ this.pending.clear();
4119
+ this.idleResolvers.splice(0).forEach((fn) => fn());
4101
4120
  await this.cdp.send("Network.disable");
4102
4121
  }
4103
4122
  waitForIdle(rejectTimeout) {
@@ -4298,22 +4317,33 @@ function createCommands(options) {
4298
4317
  * Wait for network to be idle, meaning no new network requests for at least `idleNetworkInterval` ms.
4299
4318
  * Use `timeout` argument to reject if network doesn't become idle within given time.
4300
4319
  */
4301
- async __chromatic_waitForIdleNetwork(_, id, timeout) {
4302
- const networkIdleTracker = networkIdleTrackers.get(id);
4303
- assert(networkIdleTracker, `No network idle tracker found for test ${id}`);
4320
+ async __chromatic_waitForIdleNetwork(context, timeout) {
4321
+ const networkIdleTracker = networkIdleTrackers.get(context.sessionId);
4322
+ assert(networkIdleTracker, `No network idle tracker found for session ${context.sessionId}`);
4304
4323
  await networkIdleTracker.waitForIdle(timeout);
4305
4324
  },
4306
4325
  /**
4307
4326
  * Start recording HTTP resources and network activity for given test.
4308
4327
  */
4309
- async __chromatic_interceptFetch(context, id) {
4310
- const cdp = await context.provider.getCDPSession?.(context.sessionId);
4311
- assert(cdp, `Unable to get CDP session for session ${context.sessionId}`);
4312
- const { contextOptions } = context.project.config.browser.provider?.options ?? {};
4313
- const resourceArchiver = new ResourceArchiver(cdp, options.assetDomains, contextOptions?.httpCredentials);
4314
- resourceArchivers.set(id, resourceArchiver);
4328
+ async __chromatic_interceptFetch(context) {
4329
+ let resourceArchiver = resourceArchivers.get(context.sessionId);
4330
+ let networkIdleTracker = networkIdleTrackers.get(context.sessionId);
4331
+ let cdp;
4332
+ if (!resourceArchiver || !networkIdleTracker) {
4333
+ cdp = await context.provider.getCDPSession?.(context.sessionId);
4334
+ assert(cdp, `Unable to get CDP session for session ${context.sessionId}`);
4335
+ }
4336
+ if (!resourceArchiver) {
4337
+ const { contextOptions } = context.project.config.browser.provider?.options ?? {};
4338
+ resourceArchiver = new ResourceArchiver(cdp, options.assetDomains, contextOptions?.httpCredentials, new URL(context.page.url()));
4339
+ resourceArchivers.set(context.sessionId, resourceArchiver);
4340
+ }
4341
+ if (!networkIdleTracker) {
4342
+ networkIdleTracker = await NetworkIdleTracker.create(cdp, options.idleNetworkInterval);
4343
+ networkIdleTrackers.set(context.sessionId, networkIdleTracker);
4344
+ }
4345
+ await networkIdleTracker.watch();
4315
4346
  await resourceArchiver.watch();
4316
- networkIdleTrackers.set(id, await NetworkIdleTracker.create(cdp, options.idleNetworkInterval));
4317
4347
  },
4318
4348
  /**
4319
4349
  * Write captured snapshots and network resources to disk.
@@ -4322,7 +4352,7 @@ function createCommands(options) {
4322
4352
  async __chromatic_writeTestResult(context, id, testOptions = {}) {
4323
4353
  const entity = context.project.vitest.state.getReportedEntityById(id);
4324
4354
  assert(entity?.type === "test", `Expected entity with id ${id} to be a test, found ${entity?.type}`);
4325
- const { archive, sessionSnapshots } = await onTestCleanup(id);
4355
+ const { archive, sessionSnapshots } = await onTestCleanup(context, id);
4326
4356
  assert(sessionSnapshots, `No snapshots found for test ${id}`);
4327
4357
  const snapshotBuffers = {};
4328
4358
  for (const [name, { snapshot, viewport, pseudoClassIds }] of sessionSnapshots) {
@@ -4362,13 +4392,16 @@ function createCommands(options) {
4362
4392
  /**
4363
4393
  * Clear test state without writing test results.
4364
4394
  */
4365
- async __chromatic_stopWithoutSnapshots(_, id) {
4366
- await onTestCleanup(id);
4395
+ async __chromatic_stopWithoutSnapshots(context, id) {
4396
+ await onTestCleanup(context, id);
4367
4397
  },
4368
4398
  /**
4369
4399
  * Reset all state of the plugin. Should be called between test runs.
4370
4400
  */
4371
4401
  async __chromatic_reset() {
4402
+ for (const resourceArchiver of resourceArchivers.values()) {
4403
+ resourceArchiver.archive = {};
4404
+ }
4372
4405
  resourceArchivers.clear();
4373
4406
  networkIdleTrackers.clear();
4374
4407
  snapshots.clear();
@@ -4381,14 +4414,13 @@ function createCommands(options) {
4381
4414
  return Object.fromEntries(snapshots.get(id) || []);
4382
4415
  }
4383
4416
  };
4384
- async function onTestCleanup(id) {
4385
- const resourceArchiver = resourceArchivers.get(id);
4417
+ async function onTestCleanup(context, id) {
4418
+ const resourceArchiver = resourceArchivers.get(context.sessionId);
4386
4419
  assert(resourceArchiver, `No resource archiver found for test ${id}`);
4387
- resourceArchivers.delete(id);
4420
+ const networkIdleTracker = networkIdleTrackers.get(context.sessionId);
4421
+ assert(networkIdleTracker, `No network idle tracker found for test ${id}`);
4388
4422
  await resourceArchiver.unwatch();
4389
- const networkIdleTracker = networkIdleTrackers.get(id);
4390
- networkIdleTrackers.delete(id);
4391
- await networkIdleTracker?.off();
4423
+ await networkIdleTracker.unwatch();
4392
4424
  const sessionSnapshots = snapshots.get(id);
4393
4425
  snapshots.delete(id);
4394
4426
  return {
package/dist/setupFile.js CHANGED
@@ -126,7 +126,7 @@ async function waitForIdleNetwork(timeout) {
126
126
  throw new TypeError(`waitForIdleNetwork() cannot be called in a test that is not registered for Chromatic plugin.
127
127
  Make sure ${test.file.projectName || "root"} project has chromaticPlugin() enabled.`);
128
128
  }
129
- return await commands2.__chromatic_waitForIdleNetwork(test.id, timeout);
129
+ return await commands2.__chromatic_waitForIdleNetwork(timeout);
130
130
  }
131
131
  __name(waitForIdleNetwork, "waitForIdleNetwork");
132
132
 
@@ -144,7 +144,8 @@ beforeEach(async ({ task }) => {
144
144
  task.meta.__chromatic_isTakeSnapshotCalled = false;
145
145
  task.meta.__chromatic_options ||= {};
146
146
  task.meta.__chromatic_options.disableAutoSnapshot ??= options.disableAutoSnapshot;
147
- await commands3.__chromatic_interceptFetch(task.id);
147
+ task.meta.__chromatic_options.resourceArchiveTimeout ??= options.resourceArchiveTimeout;
148
+ await commands3.__chromatic_interceptFetch();
148
149
  return /* @__PURE__ */ __name(async function beforeEachCleanup() {
149
150
  const { __chromatic_isTakeSnapshotCalled: isTakeSnapshotCalled, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
150
151
  if (!isRegistered) {
@@ -169,24 +170,22 @@ beforeEach(async ({ task }) => {
169
170
  __name(cleanup, "cleanup");
170
171
  });
171
172
  afterEach(async ({ task }) => {
172
- const { __chromatic_options: options, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
173
+ const { __chromatic_options: options, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered, __chromatic_options: testOptions } = task.meta;
173
174
  if (!isRegistered) {
174
175
  return;
175
176
  }
176
177
  if (pendingTakeSnapshots?.length) {
177
178
  throw new PendingSnapshotsError(pendingTakeSnapshots);
178
179
  }
179
- if (options.disableAutoSnapshot) {
180
- return;
180
+ if (testOptions.resourceArchiveTimeout !== 0) {
181
+ await document.fonts.ready;
182
+ await waitForIdleNetwork(testOptions.resourceArchiveTimeout);
181
183
  }
182
- const globals = await commands3.__chromatic_getOptions();
183
- const resourceArchiveTimeout = options.resourceArchiveTimeout ?? globals.resourceArchiveTimeout;
184
- if (resourceArchiveTimeout !== 0) {
185
- await waitForIdleNetwork(resourceArchiveTimeout);
184
+ if (!options.disableAutoSnapshot) {
185
+ await takeSnapshot(void 0, {
186
+ ignoreUnawaited: true
187
+ });
186
188
  }
187
- await takeSnapshot(void 0, {
188
- ignoreUnawaited: true
189
- });
190
189
  });
191
190
  var PendingSnapshotsError = class PendingSnapshotsError2 extends AggregateError {
192
191
  static {
@@ -1,4 +1,4 @@
1
- /** @type { import('@storybook/server-webpack5').StorybookConfig } */
1
+ /** @type {import('@storybook/server-webpack5').StorybookConfig} */
2
2
  declare const _default: {
3
3
  stories: string[];
4
4
  managerEntries: string[];
@@ -12,6 +12,9 @@ declare const _default: {
12
12
  renderer: string;
13
13
  };
14
14
  staticDirs: string[];
15
+ features: {
16
+ sidebarOnboardingChecklist: boolean;
17
+ };
15
18
  };
16
19
 
17
20
  export { _default as default };
@@ -51,7 +51,10 @@ var main_default = {
51
51
  },
52
52
  staticDirs: [
53
53
  path2.resolve(archivesDir(DEFAULT_OUTPUT_DIR), "archive")
54
- ]
54
+ ],
55
+ features: {
56
+ sidebarOnboardingChecklist: false
57
+ }
55
58
  };
56
59
  export {
57
60
  main_default as default
@@ -2,7 +2,13 @@
2
2
  import { addons } from "storybook/manager-api";
3
3
  addons.setConfig({
4
4
  sidebar: {
5
- // this ensures we use folders at the root-level instead of categories
5
+ // Ensures we use folders at the root-level instead of categories
6
6
  showRoots: false
7
+ },
8
+ layoutCustomisations: {
9
+ // Hide toolbar options that don't make sense in e2e setup
10
+ showToolbar: () => false,
11
+ // Hide bottom panel that's empty in e2e setup
12
+ showPanel: () => false
7
13
  }
8
14
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromatic-com/vitest",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",