Test footer error cleanup and misc test improvements
MODIFY
packages/@romejs/cli-diagnostics/DiagnosticsPrinter.ts
packages/@romejs/cli-diagnostics/DiagnosticsPrinter.ts
@@ -83,7 +83,10 @@ function equalPosition(
return true;
}
-type BeforeFooterPrintFn = (reporter: Reporter, error: boolean) => void;
+type FooterPrintCallback = (
+ reporter: Reporter,
+ error: boolean,
+) => void | boolean;
export const DEFAULT_PRINTER_FLAGS: DiagnosticsPrinterFlags = {
grep: '',
@@ -143,12 +146,12 @@ export default class DiagnosticsPrinter extends Error {
this.missingFileSources = new AbsoluteFilePathSet();
this.fileSources = new UnknownFilePathMap();
this.fileMtimes = new UnknownFilePathMap();
- this.beforeFooterPrint = [];
+ this.onFooterPrintCallbacks = [];
}
reporter: Reporter;
processor: DiagnosticsProcessor;
- beforeFooterPrint: Array<BeforeFooterPrintFn>;
+ onFooterPrintCallbacks: Array<FooterPrintCallback>;
flags: DiagnosticsPrinterFlags;
cwd: AbsoluteFilePath;
readFile: DiagnosticsFileReader;
@@ -518,8 +521,8 @@ export default class DiagnosticsPrinter extends Error {
return filteredDiagnostics;
}
- onBeforeFooterPrint(fn: BeforeFooterPrintFn) {
- this.beforeFooterPrint.push(fn);
+ onFooterPrint(fn: FooterPrintCallback) {
+ this.onFooterPrintCallbacks.push(fn);
}
footer() {
@@ -539,14 +542,27 @@ export default class DiagnosticsPrinter extends Error {
);
}
- for (const handler of this.beforeFooterPrint) {
- handler(reporter, isError);
+ if (isError) {
+ if (this.flags.fieri) {
+ this.showBanner(errorBanner);
+ }
+ } else {
+ if (this.flags.fieri) {
+ this.showBanner(successBanner);
+ }
+ }
+
+ for (const handler of this.onFooterPrintCallbacks) {
+ const stop = handler(reporter, isError);
+ if (stop) {
+ return;
+ }
}
if (isError) {
this.footerError();
} else {
- this.footerSuccess();
+ reporter.success('No known problems!');
}
}
@@ -579,23 +595,9 @@ export default class DiagnosticsPrinter extends Error {
}
}
- footerSuccess() {
- const {reporter} = this;
-
- if (this.flags.fieri) {
- this.showBanner(successBanner);
- }
-
- reporter.success('No known problems!');
- }
-
footerError() {
const {reporter, filteredCount} = this;
- if (this.flags.fieri) {
- this.showBanner(errorBanner);
- }
-
const displayableProblems = this.getDisplayedProblemsCount();
let str = `Found <number emphasis>${displayableProblems}</number> <grammarNumber plural="problems" singular="problem">${displayableProblems}</grammarNumber>`;

MODIFY
packages/@romejs/core/master/linter/Linter.ts
packages/@romejs/core/master/linter/Linter.ts
@@ -76,7 +76,8 @@ function createDiagnosticsPrinter(
): DiagnosticsPrinter {
const printer = request.createDiagnosticsPrinter(processor);
- printer.onBeforeFooterPrint((reporter, isError) => {
+ printer.onFooterPrint(
+ (reporter, isError) => {
if (isError) {
let hasPendingFixes = false;
@@ -111,7 +112,9 @@ function createDiagnosticsPrinter(
);
}
}
- });
+ },
+ true,
+ );
return printer;
}

MODIFY
packages/@romejs/core/master/testing/TestRunner.ts
packages/@romejs/core/master/testing/TestRunner.ts
@@ -821,42 +821,54 @@ export default class TestRunner {
}
}
- throwPrinter() {
- const {printer} = this;
-
- printer.onBeforeFooterPrint((reporter, isError) => {
- this.printCoverageReport(isError);
-
+ printSnapshotCounts(reporter: Reporter) {
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>`,
- );
+
+ const snapshotCounts: Array<{
+ count: number;
+ noun: string;
+ }> = [
+ {count: createdSnapshots, noun: 'created'},
+ {count: updatedSnapshots, noun: 'updated'},
+ {count: deletedSnapshots, noun: 'deleted'},
+ ].filter(({count}) => count > 0);
+
+ if (snapshotCounts.length === 0) {
+ return;
}
- if (deletedSnapshots > 0) {
- snapshotLogParts.push(
- `<error><number emphasis>${deletedSnapshots}</number> deleted</error>`,
- );
+
+ const first = snapshotCounts.shift()!;
+ const parts = [
+ `<number emphasis>${first.count}</number> snapshots ${first.noun}`,
+ ];
+
+ for (const {count, noun} of snapshotCounts) {
+ parts.push(`<number emphasis>${count}</emphasis> ${noun}`);
}
- if (snapshotLogParts.length > 0) {
- reporter.logAll(`Snapshots: ${snapshotLogParts.join(' ')}`);
+
+ reporter.success(parts.join(', '));
}
+ throwPrinter() {
+ const {printer} = this;
+
+ printer.onFooterPrint((reporter, isError) => {
+ this.printCoverageReport(isError);
+ this.printSnapshotCounts(reporter);
+
if (!isError) {
reporter.success(
`All <emphasis>${humanizeNumber(this.progress.totalTests)}</emphasis> tests passed!`,
);
+ return true;
}
+
+ // Show default footer
+ return false;
});
throw printer;

MODIFY
packages/@romejs/core/test-worker/SnapshotManager.ts
packages/@romejs/core/test-worker/SnapshotManager.ts
@@ -106,11 +106,6 @@ export default class SnapshotManager {
return;
}
- // If we're force updating, pretend that no snapshots exist on disk
- if (this.options.updateSnapshots) {
- return;
- }
-
const content = await readFileText(path);
const parser = createSnapshotParser({
path,
@@ -259,6 +254,8 @@ export default class SnapshotManager {
return;
}
+ const {hasDiagnostics} = this.runner;
+
for (const [path, {used, existsOnDisk, raw, entries}] of this.snapshots) {
const lines = this.buildSnapshot(entries.values());
const formatted = lines.join('\n');
@@ -277,9 +274,12 @@ export default class SnapshotManager {
}
} else {
if (existsOnDisk && !used) {
+ // Don't delete a snapshot if there are test failures as those failures may be hiding a snapshot usage
+ if (!hasDiagnostics) {
// 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);
@@ -314,6 +314,11 @@ export default class SnapshotManager {
snapshot.used = true;
+ // If we're force updating, pretend that there was no entry
+ if (this.options.updateSnapshots) {
+ return undefined;
+ }
+
const entry = snapshot.entries.get(buildEntriesKey(testName, entryName));
if (entry === undefined) {
return undefined;

MODIFY
packages/@romejs/core/test-worker/TestWorkerRunner.ts
packages/@romejs/core/test-worker/TestWorkerRunner.ts
@@ -24,6 +24,7 @@ import {
TestOptions,
} from '@romejs-runtime/rome/test';
import {
+ TestRef,
default as TestWorkerBridge,
TestWorkerBridgeRunOptions,
} from '../common/bridges/TestWorkerBridge';
@@ -99,6 +100,7 @@ export default class TestWorkerRunner {
createAbsoluteFilePath(opts.file.real),
);
+ this.hasDiagnostics = false;
this.emitConsoleDiagnostics = false;
this.consoleAdvice = [];
this.hasFocusedTest = false;
@@ -123,6 +125,7 @@ export default class TestWorkerRunner {
locked: boolean;
consoleAdvice: DiagnosticAdvice;
emitConsoleDiagnostics: boolean;
+ hasDiagnostics: boolean;
createConsole(): Partial<Console> {
const addDiagnostic = (
@@ -215,9 +218,10 @@ export default class TestWorkerRunner {
};
}
- async emitDiagnostic(diagnostic: Diagnostic) {
+ async emitDiagnostic(diagnostic: Diagnostic, ref?: TestRef) {
+ this.hasDiagnostics = true;
await this.bridge.testError.call({
- ref: undefined,
+ ref,
diagnostic,
});
}
@@ -327,10 +331,7 @@ export default class TestWorkerRunner {
};
this.emitConsoleDiagnostics = true;
- this.bridge.testError.send({
- ref,
- diagnostic,
- });
+ this.emitDiagnostic(diagnostic, ref);
}
async teardownTest(testName: string, api: TestAPI) {
@@ -408,14 +409,11 @@ export default class TestWorkerRunner {
const {foundTests} = this;
if (foundTests.size === 0) {
- this.bridge.testError.send({
- ref: undefined,
- diagnostic: {
+ this.emitDiagnostic({
location: {
filename: this.file.uid,
},
description: descriptions.TESTS.UNDECLARED,
- },
});
}
@@ -493,10 +491,7 @@ export default class TestWorkerRunner {
if (diagnostics !== undefined) {
for (const diagnostic of diagnostics) {
- await this.bridge.testError.call({
- ref: undefined,
- diagnostic,
- });
+ await this.emitDiagnostic(diagnostic);
}
}
} catch (err) {

Test footer error cleanup and misc test improvements