From e7572aaf8a6caba2c6e759957be7f25b9b8e1b48 Mon Sep 17 00:00:00 2001 From: leaysgur <6259812+leaysgur@users.noreply.github.com> Date: Fri, 27 Feb 2026 04:40:12 +0000 Subject: [PATCH] test(oxfmt): Add snapshot for invalid file error reports (#19802) Just adding tests for sure. ..., but to do so, we need to force dumb theme for `miette`. --- .../__snapshots__/error_reports.test.ts.snap | 38 +++++++++++++++++++ .../cli/error_reports/error_reports.test.ts | 17 +++++++++ .../cli/error_reports/fixtures/invalid.js | 1 + .../cli/error_reports/fixtures/invalid.yaml | 1 + apps/oxfmt/test/cli/utils.ts | 36 +++++++++++------- 5 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 apps/oxfmt/test/cli/error_reports/__snapshots__/error_reports.test.ts.snap create mode 100644 apps/oxfmt/test/cli/error_reports/error_reports.test.ts create mode 100644 apps/oxfmt/test/cli/error_reports/fixtures/invalid.js create mode 100644 apps/oxfmt/test/cli/error_reports/fixtures/invalid.yaml diff --git a/apps/oxfmt/test/cli/error_reports/__snapshots__/error_reports.test.ts.snap b/apps/oxfmt/test/cli/error_reports/__snapshots__/error_reports.test.ts.snap new file mode 100644 index 0000000000000..ead6e4ca73aed --- /dev/null +++ b/apps/oxfmt/test/cli/error_reports/__snapshots__/error_reports.test.ts.snap @@ -0,0 +1,38 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`error_reports > should report errors for invalid files 1`] = ` +"-------------------- +arguments: --check invalid.js +working directory: error_reports/fixtures +exit code: 2 +--- STDOUT --------- +Checking formatting... + +--- STDERR --------- + + x Expected \`}\` but found \`EOF\` + ,-[invalid.js:2:1] + 1 | class { + : | + : \`-- Opened here + \`---- +Error occurred when checking code style in the above files. +-------------------- +-------------------- +arguments: --check invalid.yaml +working directory: error_reports/fixtures +exit code: 2 +--- STDOUT --------- +Checking formatting... + +--- STDERR --------- + + x SyntaxError: Nested mappings are not allowed in compact mappings (1:6) + | [invalid.yaml] + | > 1 | key: val: nested + | | ^^^^^^^^^^^ + | > 2 | + | | ^ +Error occurred when checking code style in the above files. +--------------------" +`; diff --git a/apps/oxfmt/test/cli/error_reports/error_reports.test.ts b/apps/oxfmt/test/cli/error_reports/error_reports.test.ts new file mode 100644 index 0000000000000..26f5ae71be1e7 --- /dev/null +++ b/apps/oxfmt/test/cli/error_reports/error_reports.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from "vitest"; +import { join } from "node:path"; +import { runAndSnapshot } from "../utils"; + +const fixturesDir = join(import.meta.dirname, "fixtures"); + +describe("error_reports", () => { + it("should report errors for invalid files", async () => { + const testCases = [ + ["--check", "invalid.js"], + ["--check", "invalid.yaml"], + ]; + + const snapshot = await runAndSnapshot(fixturesDir, testCases); + expect(snapshot).toMatchSnapshot(); + }); +}); diff --git a/apps/oxfmt/test/cli/error_reports/fixtures/invalid.js b/apps/oxfmt/test/cli/error_reports/fixtures/invalid.js new file mode 100644 index 0000000000000..24ffba711ae2b --- /dev/null +++ b/apps/oxfmt/test/cli/error_reports/fixtures/invalid.js @@ -0,0 +1 @@ +class { diff --git a/apps/oxfmt/test/cli/error_reports/fixtures/invalid.yaml b/apps/oxfmt/test/cli/error_reports/fixtures/invalid.yaml new file mode 100644 index 0000000000000..b2c8fdca2bea0 --- /dev/null +++ b/apps/oxfmt/test/cli/error_reports/fixtures/invalid.yaml @@ -0,0 +1 @@ +key: val: nested diff --git a/apps/oxfmt/test/cli/utils.ts b/apps/oxfmt/test/cli/utils.ts index 4bf050af3f64a..098fe5b2de275 100644 --- a/apps/oxfmt/test/cli/utils.ts +++ b/apps/oxfmt/test/cli/utils.ts @@ -81,23 +81,33 @@ ${afterContent} // --- function normalizeOutput(output: string, cwd: string): string { - let normalized = output; - - // Normalize timing information - normalized = normalized.replace(/\d+(?:\.\d+)?s|\d+ms/g, "ms"); - // Normalize thread count (e.g., "using 8 threads" -> "using 1 threads") - normalized = normalized.replace(/using \d+ threads/g, "using 1 threads"); - // Normalize path separators (Windows compatibility) - normalized = normalized.replace(/\\/g, "/"); - // Replace absolute paths const cwdPath = cwd.replace(/\\/g, "/"); - normalized = normalized.replace(new RegExp(RegExp.escape(cwdPath), "g"), ""); - // Replace repo root path const repoRoot = join(import.meta.dirname, "..", "..", "..", ".."); const rootPath = repoRoot.replace(/\\/g, "/"); - normalized = normalized.replace(new RegExp(RegExp.escape(rootPath), "g"), ""); - return normalized; + return ( + output + // Normalize timing information + .replace(/\d+(?:\.\d+)?s|\d+ms/g, "ms") + // Normalize path separators (Windows compatibility) + .replace(/\\/g, "/") + // Replace absolute paths + .replace(new RegExp(RegExp.escape(cwdPath), "g"), "") + .replace(new RegExp(RegExp.escape(rootPath), "g"), "") + // Strip ANSI escape codes (e.g. from Prettier error messages) + // eslint-disable-next-line no-control-regex + .replace(/\x1b\[[0-9;]*m/g, "") + // Normalize miette Unicode theme to ASCII + .replace(/×/g, "x") + .replace(/╭/g, ",") + .replace(/─/g, "-") + .replace(/│/g, "|") + .replace(/·/g, ":") + .replace(/┬/g, "|") + .replace(/╰/g, "`") + // Trim trailing whitespace per line + .replace(/[^\S\n]+$/gm, "") + ); } function formatSnapshot(cwd: string, args: string[], { stdout, stderr, exitCode }: Result): string {