Add snapshot update count
MODIFY
packages/@romejs/core/common/bridges/TestWorkerBridge.ts
packages/@romejs/core/common/bridges/TestWorkerBridge.ts
@@ -40,6 +40,17 @@ export default class TestWorkerBridge extends Bridge {
direction: 'server->client',
});
+ snapshotUpdated = this.createEvent<
+ {
+ filename: string;
+ event: 'delete' | 'update' | 'create';
+ },
+ void
+ >({
+ name: 'snapshotUpdated',
+ direction: 'server->client',
+ });
+
runTest = this.createEvent<number, void>({
name: 'runTest',
direction: 'server->client',

MODIFY
packages/@romejs/core/master/testing/TestRunner.ts
packages/@romejs/core/master/testing/TestRunner.ts
@@ -103,9 +103,12 @@ export default class TestRunner {
this.coverageCollector = new CoverageCollector();
this.progress = {
- total: 0,
- started: 0,
- finished: 0,
+ totalTests: 0,
+ startedTests: 0,
+ finishedTests: 0,
+ updatedSnapshots: 0,
+ deletedSnapshots: 0,
+ createdSnapshots: 0,
};
this.runningTests = new Map();
@@ -153,9 +156,12 @@ export default class TestRunner {
>;
progress: {
- total: number;
- started: number;
- finished: number;
+ totalTests: number;
+ startedTests: number;
+ finishedTests: number;
+ updatedSnapshots: number;
+ deletedSnapshots: number;
+ createdSnapshots: number;
};
async runWorker({bridge, process, inspector}: TestWorkerContainer) {
@@ -363,7 +369,7 @@ export default class TestRunner {
teardown();
- this.printTestResults();
+ this.throwPrinter();
}
async handleWorkerTimeout(
@@ -480,7 +486,7 @@ export default class TestRunner {
ref: TestRef,
timeoutMs: undefined | number,
) {
- this.progress.started++;
+ this.progress.startedTests++;
let timeout = undefined;
if (timeoutMs !== undefined) {
@@ -508,7 +514,7 @@ export default class TestRunner {
}
data;
- this.progress.total++;
+ this.progress.totalTests++;
}
onTestFinished(ref: TestRef) {
@@ -523,7 +529,7 @@ export default class TestRunner {
}
this.runningTests.delete(key);
- this.progress.finished++;
+ this.progress.finishedTests++;
}
setupProgress(): () => void {
@@ -585,11 +591,30 @@ export default class TestRunner {
}
});
+ bridge.snapshotUpdated.subscribe(({event}) => {
+ switch (event) {
+ case 'create': {
+ this.progress.createdSnapshots++;
+ break;
+ }
+
+ case 'update': {
+ this.progress.updatedSnapshots++;
+ break;
+ }
+
+ case 'delete': {
+ this.progress.deletedSnapshots++;
+ break;
+ }
+ }
+ });
+
bridge.testsFound.subscribe((tests) => {
for (const {ref, isSkipped} of tests) {
this.onTestFound(ref, isSkipped);
}
- progress.setTotal(this.progress.total);
+ progress.setTotal(this.progress.totalTests);
});
bridge.testStart.subscribe((data) => {
@@ -630,8 +655,14 @@ export default class TestRunner {
};
}
- printCoverageReport() {
+ printCoverageReport(isError: boolean) {
const {reporter, master, coverageCollector} = this;
+
+ if (isError && this.options.showAllCoverage) {
+ // Only show coverage for errors when --show-all-coverage has been specified
+ return;
+ }
+
if (!this.options.coverage) {
return;
}
@@ -781,21 +812,6 @@ export default class TestRunner {
reporter.hr();
}
- printTestResults() {
- if (this.printer.hasDiagnostics()) {
- this.throwErrorDiagnosticsPrinter();
- } else {
- this.printTestSuccess();
- }
- }
-
- printTestSuccess() {
- this.printCoverageReport();
- this.reporter.success(
- `All <emphasis>${humanizeNumber(this.progress.total)}</emphasis> tests passed!`,
- );
- }
-
getSourceCode(filename: string): undefined | string {
const testSource = this.sources.get(filename);
if (testSource === undefined) {
@@ -805,15 +821,43 @@ export default class TestRunner {
}
}
- throwErrorDiagnosticsPrinter() {
+ throwPrinter() {
const {printer} = this;
- // Only show code coverage for errors when `--show-all-coverage` has been passed
- if (this.options.showAllCoverage) {
- printer.onBeforeFooterPrint(() => {
- this.printCoverageReport();
- });
+ printer.onBeforeFooterPrint((reporter, isError) => {
+ this.printCoverageReport(isError);
+
+ const {
+ createdSnapshots,
+ deletedSnapshots,
+ updatedSnapshots,
+ } = this.progress;
+ const snapshotLogParts: Array<string> = [];
+ if (createdSnapshots > 0) {
+ snapshotLogParts.push(
+ `<success><number emphasis>${createdSnapshots}</number> created</success>`,
+ );
+ }
+ if (updatedSnapshots > 0) {
+ snapshotLogParts.push(
+ `<success><number emphasis>${updatedSnapshots}</number> updated</success>`,
+ );
}
+ if (deletedSnapshots > 0) {
+ snapshotLogParts.push(
+ `<error><number emphasis>${deletedSnapshots}</number> deleted</error>`,
+ );
+ }
+ if (snapshotLogParts.length > 0) {
+ reporter.logAll(`Snapshots: ${snapshotLogParts.join(' ')}`);
+ }
+
+ if (!isError) {
+ reporter.success(
+ `All <emphasis>${humanizeNumber(this.progress.totalTests)}</emphasis> tests passed!`,
+ );
+ }
+ });
throw printer;
}

MODIFY
packages/@romejs/core/test-worker/SnapshotManager.ts
packages/@romejs/core/test-worker/SnapshotManager.ts
@@ -36,7 +36,7 @@ type SnapshotEntry = {
};
type Snapshot = {
- exists: boolean;
+ existsOnDisk: boolean;
used: boolean;
raw: string;
entries: Map<string, SnapshotEntry>;
@@ -64,9 +64,7 @@ export default class SnapshotManager {
loadingSnapshotCount: number;
testPath: AbsoluteFilePath;
defaultSnapshotPath: AbsoluteFilePath;
-
snapshots: AbsoluteFilePathMap<Snapshot>;
-
runner: TestWorkerRunner;
options: TestRunnerOptions;
@@ -122,7 +120,7 @@ export default class SnapshotManager {
const nodes = parser.parse();
const snapshot: Snapshot = {
- exists: true,
+ existsOnDisk: true,
used: false,
raw: parser.input,
entries: new Map(),
@@ -261,14 +259,16 @@ export default class SnapshotManager {
return;
}
- for (const [path, {used, exists, raw, entries}] of this.snapshots) {
+ for (const [path, {used, existsOnDisk, raw, entries}] of this.snapshots) {
const lines = this.buildSnapshot(entries.values());
const formatted = lines.join('\n');
+ let event: undefined | 'delete' | 'update' | 'create';
+
if (this.options.freezeSnapshots) {
if (!used) {
await this.emitDiagnostic(descriptions.SNAPSHOTS.REDUNDANT);
- } else if (!exists) {
+ } else if (!existsOnDisk) {
await this.emitDiagnostic(descriptions.SNAPSHOTS.MISSING);
} else if (formatted !== raw) {
await this.emitDiagnostic(
@@ -276,14 +276,23 @@ export default class SnapshotManager {
);
}
} else {
- if (exists && !used) {
+ if (existsOnDisk && !used) {
// If a snapshot wasn't used or is empty then delete it!
await unlink(path);
+ event = 'delete';
} else if (used && formatted !== raw) {
// Fresh snapshot!
await writeFile(path, formatted);
+ event = existsOnDisk ? 'update' : 'create';
}
}
+
+ if (event !== undefined) {
+ await this.runner.bridge.snapshotUpdated.call({
+ filename: path.join(),
+ event,
+ });
+ }
}
}
@@ -333,7 +342,7 @@ export default class SnapshotManager {
if (snapshot === undefined) {
snapshot = {
raw: '',
- exists: false,
+ existsOnDisk: false,
used: true,
entries: new Map(),
};

Add snapshot update count