[snap tester] watch mode: ignore changes from test updates
A bit of a hack - We currently trigger test runs when we detect changes in the test fixtures directory. This trigger is also hit when we run `snap` in update mode, since updating performs file writes. This PR will ignore subscription changes (callbacks) that trigger within 5 seconds of the last update. It seems difficult to be more granular with a timestamp, since `@parcel/watcher` doesn't give us the file change timestamp and (from my understanding), other promises and tasks can be queued to run between the update and callback.
Mofei Zhang committed
Apr 25, 2023 at 14:15 UTC
bf518c2b2cb09025c28c07c2686ee71585ace071
1 file changed
+16
-5
compiler/forget/packages/snap/src/runner.ts
+16
-5
@@ -329,6 +329,15 @@ export async function main(opts: RunnerOptions): Promise<void> {
329
// safe to use a cached compiler version
330
let compilerVersion = 0;
331
let isCompilerValid = false;
332
+ let lastUpdate = -1;
333
+
334
+ function isRealUpdate(): boolean {
335
+ // Try to ignore changes that occurred as a result of our explicitly updating
336
+ // fixtures in update().
337
+ // Currently keeps a timestamp of last known changes, and ignore events that occurred
338
+ // around that timestamp.
339
+ return performance.now() - lastUpdate > 5000;
340
+ }
341
342
function onStart() {
343
// Notify the user when compilation starts but don't clear the screen yet
@@ -349,6 +358,9 @@ export async function main(opts: RunnerOptions): Promise<void> {
358
report(results);
359
}
360
const end = performance.now();
361
+ if (mode === Mode.Update) {
362
+ lastUpdate = end;
363
+ }
364
console.log(`Completed in ${Math.floor(end - start)} ms`);
365
} else {
366
console.error(
@@ -378,9 +390,6 @@ export async function main(opts: RunnerOptions): Promise<void> {
390
});
391
392
// Watch the fixtures directory for changes
381
- // TODO: ignore changes that occurred as a result of our explicitly updating
382
- // fixtures in update() - maybe keep a timestamp of last known changes, and
383
- // ignore events that occurred prior to that timestamp.
393
const fileSubscription = watcher.subscribe(
394
FIXTURES_PATH,
395
async (err, _events) => {
@@ -388,8 +397,10 @@ export async function main(opts: RunnerOptions): Promise<void> {
397
console.error(err);
398
process.exit(1);
399
}
391
- // Fixtures changed, re-run tests
392
- onChange({ mode: Mode.Test });
400
+ if (isRealUpdate()) {
401
+ // Fixtures changed, re-run tests
402
+ onChange({ mode: Mode.Test });
403
+ }
404
}
405
);
406