test: cover error model and unauth order create

This commit is contained in:
zetaloop
2026-02-28 07:26:34 +08:00
parent 411ee8293d
commit d5f59f9a4a
2 changed files with 51 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
import { isApiError, toApiError } from "@/lib/errors"
import { describe, expect, it } from "vitest"
describe("lib/errors", () => {
describe("toApiError", () => {
it("passes through {code,msg} ApiError", () => {
const err = { code: 400, msg: "bad request" }
expect(isApiError(err)).toBe(true)
try {
throw err
} catch (e) {
expect(toApiError(e)).toBe(err)
}
})
it("converts Error to safe {code,msg}", () => {
expect(toApiError(new Error("x"))).toEqual({ code: 500, msg: "x" })
})
it("converts string to safe {code,msg}", () => {
expect(toApiError("x")).toEqual({ code: 500, msg: "x" })
})
it("converts unknown input to safe {code,msg}", () => {
expect(toApiError(null)).toEqual({ code: 500, msg: "Unknown error" })
})
})
})