import { test, expect } from "vitest"; import { visualizeAnsiCodes, defaultCodes } from "visualize-ansi-codes"; import { formatError } from "./index"; function clean(str) { return visualizeAnsiCodes( str.replace(new RegExp(process.cwd(), "g"), ""), { ...defaultCodes, "\x1B[90m": "", } ); } function one() { throw new Error("Uh oh!"); } function two() { one(); } function three() { two(); } test("basic", () => { try { three(); } catch (err) { const result = formatError(err, { color: false }); expect(clean(result)).toMatchInlineSnapshot(` "Error: Uh oh! at one (/src/index.test.ts:17:9) at two (/src/index.test.ts:20:3) at three (/src/index.test.ts:23:3) at /src/index.test.ts:27:5 at /node_modules/vitest/dist/chunk-runtime-chain.2a787014.js:74:26 at runTest (/node_modules/vitest/dist/entry.js:1765:40) at async runSuite (/node_modules/vitest/dist/entry.js:1829:13) at async runFiles (/node_modules/vitest/dist/entry.js:1866:5) at async startTests (/node_modules/vitest/dist/entry.js:1872:3) at async /node_modules/vitest/dist/entry.js:1897:7" `); const result2 = formatError(err, { color: true }); expect(clean(result2)).toMatchInlineSnapshot(` "Error: Uh oh! at one (/src/index.test.ts:17:9) at two (/src/index.test.ts:20:3) at three (/src/index.test.ts:23:3) at /src/index.test.ts:27:5 at /node_modules/vitest/dist/chunk-runtime-chain.2a787014.js:74:26 at runTest (/node_modules/vitest/dist/entry.js:1765:40) at async runSuite (/node_modules/vitest/dist/entry.js:1829:13) at async runFiles (/node_modules/vitest/dist/entry.js:1866:5) at async startTests (/node_modules/vitest/dist/entry.js:1872:3) at async /node_modules/vitest/dist/entry.js:1897:7" `); } }); test("basic", () => { try { three(); } catch (err) { const result = formatError(err, { color: false }); expect(clean(result)).toMatchInlineSnapshot(` "Error: Uh oh! at one (/src/index.test.ts:17:9) at two (/src/index.test.ts:20:3) at three (/src/index.test.ts:23:3) at /src/index.test.ts:61:5 at /node_modules/vitest/dist/chunk-runtime-chain.2a787014.js:74:26 at runTest (/node_modules/vitest/dist/entry.js:1765:40) at async runSuite (/node_modules/vitest/dist/entry.js:1829:13) at async runFiles (/node_modules/vitest/dist/entry.js:1866:5) at async startTests (/node_modules/vitest/dist/entry.js:1872:3) at async /node_modules/vitest/dist/entry.js:1897:7" `); const result2 = formatError(err, { color: true }); expect(clean(result2)).toMatchInlineSnapshot(` "Error: Uh oh! at one (/src/index.test.ts:17:9) at two (/src/index.test.ts:20:3) at three (/src/index.test.ts:23:3) at /src/index.test.ts:61:5 at /node_modules/vitest/dist/chunk-runtime-chain.2a787014.js:74:26 at runTest (/node_modules/vitest/dist/entry.js:1765:40) at async runSuite (/node_modules/vitest/dist/entry.js:1829:13) at async runFiles (/node_modules/vitest/dist/entry.js:1866:5) at async startTests (/node_modules/vitest/dist/entry.js:1872:3) at async /node_modules/vitest/dist/entry.js:1897:7" `); } }); test("with code frame in error stack", () => { const err = new SyntaxError("Unexpected identifier"); Object.defineProperty(err, "stack", { value: "evalmachine.:1\n(bla bla bla)\n ^^^\n\nSyntaxError: Unexpected identifier\n at new Script (node:vm:100:7)\n at createScript (node:vm:257:10)\n at Object.runInContext (node:vm:288:10)\n at runExpression (/something/somewhere.js:235:20)\n at evalmachine.:1:53\n at /something/somewhere/@:76:16", }); const result = formatError(err, { color: false }); expect(clean(result)).toMatchInlineSnapshot(` "SyntaxError: Unexpected identifier evalmachine.:1 (bla bla bla) ^^^ at runExpression (/something/somewhere.js:235:20) at evalmachine.:1:53 at /something/somewhere/@:76:16" `); const result2 = formatError(err, { color: true }); expect(clean(result2)).toMatchInlineSnapshot(` "SyntaxError: Unexpected identifier evalmachine.:1 (bla bla bla) ^^^ at runExpression (/something/somewhere.js:235:20) at evalmachine.:1:53 at /something/somewhere/@:76:16" `); }); test("with additional properties", () => { try { three(); } catch (err) { (err as any).hi = "yes hello"; const result = formatError(err, { color: false }); expect(clean(result)).toMatchInlineSnapshot(` "Error: Uh oh! at one (/src/index.test.ts:17:9) at two (/src/index.test.ts:20:3) at three (/src/index.test.ts:23:3) at /src/index.test.ts:121:5 at /node_modules/vitest/dist/chunk-runtime-chain.2a787014.js:74:26 at runTest (/node_modules/vitest/dist/entry.js:1765:40) at async runSuite (/node_modules/vitest/dist/entry.js:1829:13) at async runFiles (/node_modules/vitest/dist/entry.js:1866:5) at async startTests (/node_modules/vitest/dist/entry.js:1872:3) at async /node_modules/vitest/dist/entry.js:1897:7 The above error also had these properties on it: { hi: 'yes hello' }" `); const result2 = formatError(err, { color: true }); expect(clean(result2)).toMatchInlineSnapshot(` "Error: Uh oh! at one (/src/index.test.ts:17:9) at two (/src/index.test.ts:20:3) at three (/src/index.test.ts:23:3) at /src/index.test.ts:121:5 at /node_modules/vitest/dist/chunk-runtime-chain.2a787014.js:74:26 at runTest (/node_modules/vitest/dist/entry.js:1765:40) at async runSuite (/node_modules/vitest/dist/entry.js:1829:13) at async runFiles (/node_modules/vitest/dist/entry.js:1866:5) at async startTests (/node_modules/vitest/dist/entry.js:1872:3) at async /node_modules/vitest/dist/entry.js:1897:7 The above error also had these properties on it: { hi: 'yes hello' }" `); } });