@chromatic-com/vitest 0.1.0 → 0.1.1

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,7 +4110,7 @@ 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);
@@ -4298,22 +4315,33 @@ function createCommands(options) {
4298
4315
  * Wait for network to be idle, meaning no new network requests for at least `idleNetworkInterval` ms.
4299
4316
  * Use `timeout` argument to reject if network doesn't become idle within given time.
4300
4317
  */
4301
- async __chromatic_waitForIdleNetwork(_, id, timeout) {
4302
- const networkIdleTracker = networkIdleTrackers.get(id);
4303
- assert(networkIdleTracker, `No network idle tracker found for test ${id}`);
4318
+ async __chromatic_waitForIdleNetwork(context, timeout) {
4319
+ const networkIdleTracker = networkIdleTrackers.get(context.sessionId);
4320
+ assert(networkIdleTracker, `No network idle tracker found for session ${context.sessionId}`);
4304
4321
  await networkIdleTracker.waitForIdle(timeout);
4305
4322
  },
4306
4323
  /**
4307
4324
  * Start recording HTTP resources and network activity for given test.
4308
4325
  */
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);
4326
+ async __chromatic_interceptFetch(context) {
4327
+ let resourceArchiver = resourceArchivers.get(context.sessionId);
4328
+ let networkIdleTracker = networkIdleTrackers.get(context.sessionId);
4329
+ let cdp;
4330
+ if (!resourceArchiver || !networkIdleTracker) {
4331
+ cdp = await context.provider.getCDPSession?.(context.sessionId);
4332
+ assert(cdp, `Unable to get CDP session for session ${context.sessionId}`);
4333
+ }
4334
+ if (!resourceArchiver) {
4335
+ const { contextOptions } = context.project.config.browser.provider?.options ?? {};
4336
+ resourceArchiver = new ResourceArchiver(cdp, options.assetDomains, contextOptions?.httpCredentials, new URL(context.page.url()));
4337
+ resourceArchivers.set(context.sessionId, resourceArchiver);
4338
+ }
4339
+ if (!networkIdleTracker) {
4340
+ networkIdleTracker = await NetworkIdleTracker.create(cdp, options.idleNetworkInterval);
4341
+ networkIdleTrackers.set(context.sessionId, networkIdleTracker);
4342
+ }
4343
+ await networkIdleTracker.watch();
4315
4344
  await resourceArchiver.watch();
4316
- networkIdleTrackers.set(id, await NetworkIdleTracker.create(cdp, options.idleNetworkInterval));
4317
4345
  },
4318
4346
  /**
4319
4347
  * Write captured snapshots and network resources to disk.
@@ -4322,7 +4350,7 @@ function createCommands(options) {
4322
4350
  async __chromatic_writeTestResult(context, id, testOptions = {}) {
4323
4351
  const entity = context.project.vitest.state.getReportedEntityById(id);
4324
4352
  assert(entity?.type === "test", `Expected entity with id ${id} to be a test, found ${entity?.type}`);
4325
- const { archive, sessionSnapshots } = await onTestCleanup(id);
4353
+ const { archive, sessionSnapshots } = await onTestCleanup(context, id);
4326
4354
  assert(sessionSnapshots, `No snapshots found for test ${id}`);
4327
4355
  const snapshotBuffers = {};
4328
4356
  for (const [name, { snapshot, viewport, pseudoClassIds }] of sessionSnapshots) {
@@ -4362,13 +4390,16 @@ function createCommands(options) {
4362
4390
  /**
4363
4391
  * Clear test state without writing test results.
4364
4392
  */
4365
- async __chromatic_stopWithoutSnapshots(_, id) {
4366
- await onTestCleanup(id);
4393
+ async __chromatic_stopWithoutSnapshots(context, id) {
4394
+ await onTestCleanup(context, id);
4367
4395
  },
4368
4396
  /**
4369
4397
  * Reset all state of the plugin. Should be called between test runs.
4370
4398
  */
4371
4399
  async __chromatic_reset() {
4400
+ for (const resourceArchiver of resourceArchivers.values()) {
4401
+ resourceArchiver.archive = {};
4402
+ }
4372
4403
  resourceArchivers.clear();
4373
4404
  networkIdleTrackers.clear();
4374
4405
  snapshots.clear();
@@ -4381,14 +4412,13 @@ function createCommands(options) {
4381
4412
  return Object.fromEntries(snapshots.get(id) || []);
4382
4413
  }
4383
4414
  };
4384
- async function onTestCleanup(id) {
4385
- const resourceArchiver = resourceArchivers.get(id);
4415
+ async function onTestCleanup(context, id) {
4416
+ const resourceArchiver = resourceArchivers.get(context.sessionId);
4386
4417
  assert(resourceArchiver, `No resource archiver found for test ${id}`);
4387
- resourceArchivers.delete(id);
4418
+ const networkIdleTracker = networkIdleTrackers.get(context.sessionId);
4419
+ assert(networkIdleTracker, `No network idle tracker found for test ${id}`);
4388
4420
  await resourceArchiver.unwatch();
4389
- const networkIdleTracker = networkIdleTrackers.get(id);
4390
- networkIdleTrackers.delete(id);
4391
- await networkIdleTracker?.off();
4421
+ await networkIdleTracker.unwatch();
4392
4422
  const sessionSnapshots = snapshots.get(id);
4393
4423
  snapshots.delete(id);
4394
4424
  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,7 @@ 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
+ await commands3.__chromatic_interceptFetch();
148
148
  return /* @__PURE__ */ __name(async function beforeEachCleanup() {
149
149
  const { __chromatic_isTakeSnapshotCalled: isTakeSnapshotCalled, __chromatic_pendingTakeSnapshots: pendingTakeSnapshots, __chromatic_isRegistered: isRegistered } = task.meta;
150
150
  if (!isRegistered) {
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.1",
4
4
  "description": "Chromatic Visual Regression Testing for Vitest",
5
5
  "repository": {
6
6
  "type": "git",