Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
--------------------"
`;
17 changes: 17 additions & 0 deletions apps/oxfmt/test/cli/error_reports/error_reports.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
1 change: 1 addition & 0 deletions apps/oxfmt/test/cli/error_reports/fixtures/invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class {
1 change: 1 addition & 0 deletions apps/oxfmt/test/cli/error_reports/fixtures/invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key: val: nested
36 changes: 23 additions & 13 deletions apps/oxfmt/test/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<variable>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"), "<cwd>");
// Replace repo root path
const repoRoot = join(import.meta.dirname, "..", "..", "..", "..");
const rootPath = repoRoot.replace(/\\/g, "/");
normalized = normalized.replace(new RegExp(RegExp.escape(rootPath), "g"), "<cwd>");

return normalized;
return (
output
// Normalize timing information
.replace(/\d+(?:\.\d+)?s|\d+ms/g, "<variable>ms")
// Normalize path separators (Windows compatibility)
.replace(/\\/g, "/")
// Replace absolute paths
.replace(new RegExp(RegExp.escape(cwdPath), "g"), "<cwd>")
.replace(new RegExp(RegExp.escape(rootPath), "g"), "<cwd>")
// 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 {
Expand Down
Loading