test: strip ANSI escape codes from snapshot output (#412)

Strips ANSI escape codes from captured `stdout` and `stderr` before
comparing test output to snapshots.

When `FORCE_COLOR` is set (some terminals and tools set it), Node
colorizes logged errors, so
`main-token-get-owner-set-client-error.test.js` fails locally even
though it passes in CI. With this change, snapshots match regardless of
color settings. CI output is unchanged.

Split out from #411.

Co-authored-by: Aiqiao Yan <55104035+aiqiaoy@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-17 21:31:31 -08:00
1 parent e056864841
commit 01b8009bd6
1 file changed
+3 -3
+3 -3
View File
@@ -1,6 +1,6 @@
import { readdirSync } from "node:fs";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { promisify, stripVTControlCharacters } from "node:util";
import { snapshot, test } from "node:test";
@@ -60,8 +60,8 @@ for (const file of testFiles) {
({ stderr, stdout } = error);
}
const trimmedStderr = stderr.replace(/\r?\n$/, "");
const trimmedStdout = stdout.replace(/\r?\n$/, "");
const trimmedStderr = stripVTControlCharacters(stderr).replace(/\r?\n$/, "");
const trimmedStdout = stripVTControlCharacters(stdout).replace(/\r?\n$/, "");
await t.test("stderr", (t) => {
if (trimmedStderr) t.assert.snapshot(trimmedStderr);
else t.assert.strictEqual(trimmedStderr, "");