mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
Widen the narrowed instanceof Error checks introduced in the TypeScript 6 upgrade with a shared errToId helper that accepts any object carrying a string id, restoring the login email-matching recovery path.
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { toError, errToString, errToId } from "./error";
|
|
|
|
describe("toError", () => {
|
|
it("returns the same Error instance when given an Error", () => {
|
|
const error = new Error("boom");
|
|
expect(toError(error)).toBe(error);
|
|
});
|
|
|
|
it("preserves Error subclasses", () => {
|
|
const error = new TypeError("nope");
|
|
expect(toError(error)).toBe(error);
|
|
});
|
|
|
|
it("wraps a string in an Error", () => {
|
|
expect(toError("boom")).toBeInstanceOf(Error);
|
|
expect(toError("boom").message).toBe("boom");
|
|
});
|
|
|
|
it("wraps non-string values using their string representation", () => {
|
|
expect(toError(42).message).toBe("42");
|
|
expect(toError(null).message).toBe("null");
|
|
expect(toError(undefined).message).toBe("undefined");
|
|
});
|
|
});
|
|
|
|
describe("errToString", () => {
|
|
it("returns the message of an Error", () => {
|
|
expect(errToString(new Error("boom"))).toBe("boom");
|
|
});
|
|
|
|
it("returns the string representation of a non-Error", () => {
|
|
expect(errToString("boom")).toBe("boom");
|
|
expect(errToString(42)).toBe("42");
|
|
expect(errToString(undefined)).toBe("undefined");
|
|
});
|
|
});
|
|
|
|
describe("errToId", () => {
|
|
it("returns the id of an Error", () => {
|
|
const error = Object.assign(new Error("boom"), { id: "invalid_request" });
|
|
expect(errToId(error)).toBe("invalid_request");
|
|
});
|
|
|
|
it("returns the id of a plain object", () => {
|
|
expect(errToId({ id: "invalid_request", message: "boom" })).toBe(
|
|
"invalid_request"
|
|
);
|
|
});
|
|
|
|
it("returns undefined when there is no string id", () => {
|
|
expect(errToId(new Error("boom"))).toBeUndefined();
|
|
expect(errToId({ id: 42 })).toBeUndefined();
|
|
expect(errToId("boom")).toBeUndefined();
|
|
expect(errToId(null)).toBeUndefined();
|
|
expect(errToId(undefined)).toBeUndefined();
|
|
});
|
|
});
|