diff --git a/test/result.test.ts b/test/result.test.ts index b736a30..5caa99b 100644 --- a/test/result.test.ts +++ b/test/result.test.ts @@ -1,5 +1,5 @@ import {describe, it, expect, expectTypeOf, vi} from "vitest" -import {Panic, Ok, Err, Result, Some, None, ErrorWithTag, ResultPromise} from "../src" +import {Panic, Ok, Err, Result, Some, None, ErrorWithTag, ResultPromise, tryFn} from "../src" export function TestOk(value: T): Result { return Ok(value) @@ -133,6 +133,44 @@ describe.concurrent("andThen", () => { const a = TestErr("early error") expect(a.andThen((value) => Ok(value + 1)).unwrapErr()).toEqual(a.unwrapErr()) }) + + it("can map primitive error type", () => { + const result = TestOk(1).andThen((num) => { + if (num === null) { + return Err("bar" as const) + } + if (num !== 1) { + return Err("baz" as const) + } + return Ok(num) + }) + expectTypeOf(result).toEqualTypeOf>() + }) + + it("can map non-primitive error type", () => { + class Foo extends ErrorWithTag { + readonly tag = "foo" + } + + class Bar extends ErrorWithTag { + readonly tag = "bar" + } + + class Baz extends ErrorWithTag { + readonly tag = "baz" + } + + const result = TestOk(1).andThen((num) => { + if (num === null) { + return Err(new Bar()) + } + if (num !== 1) { + return Err(new Baz()) + } + return Ok(num) + }) + expectTypeOf(result).toEqualTypeOf>() + }) }) describe.concurrent("andThenAsync", () => { @@ -147,20 +185,6 @@ describe.concurrent("andThenAsync", () => { a.unwrapErr(), ) }) - - it("can map error type", async () => { - const a = TestErr("foo") - const b = a.andThenAsync(async (value) => { - if (value < 0) { - return Err("bar" as const) - } - if (value > 1) { - return Ok(value) - } - return Err("baz" as const) - }) - expectTypeOf(b).toEqualTypeOf>() - }) }) describe.concurrent("expect", () => { diff --git a/test/result_promise.test.ts b/test/result_promise.test.ts index 425b43e..0cd221b 100644 --- a/test/result_promise.test.ts +++ b/test/result_promise.test.ts @@ -10,6 +10,7 @@ import { None, CaughtError, tryPromise, + tryFn, } from "../src" import {TestErr, TestOk} from "./result.test" import {vi} from "vitest" @@ -84,65 +85,6 @@ describe.concurrent("andThenAsync", () => { const a = TestErrPromise(0) await expect(a.andThenAsync(async (value) => Ok(value + 1)).unwrapErr()).resolves.toEqual(0) }) - - it("can map error type", async () => { - const a = TestErrPromise<"foo", number>("foo") - const b = a.andThenAsync(async (value) => { - if (value < 0) { - return Err("bar" as const) - } - if (value > 1) { - return Ok(value) - } - return Err("baz" as const) - }) - expectTypeOf(b).toEqualTypeOf>() - }) - - it("can map error type with complicated error", async () => { - class PrismaError extends ErrorWithTag { - readonly tag = "prisma" - // readonly message = "Prisma error" - - // constructor(public readonly error: CaughtError) { - // super() - // } - } - - abstract class NotFoundError extends ErrorWithTag { - // category = "not_found" - } - - class AccountNotFoundError extends NotFoundError { - readonly tag = "account" - // readonly message = "Account not found" - } - - async function fn(): Promise<{id: number} | null> { - return {id: 1} - } - - const maybeExpiredToken = await tryPromise(fn()) - .mapErr((error) => new PrismaError(error)) - .andThen((maybeAccount) => { - if (maybeAccount) { - return Ok(maybeAccount) - } - return Err(new AccountNotFoundError()) - }) - .andThenAsync(async (account) => { - if (account.id < 0) { - return Err(new AccountNotFoundError()) - } - - const ye = await tryPromise(fn()).mapErr((error) => new PrismaError(error)) - if (ye.isErr) { - return Err(ye.value) - } - - return Ok(account) - }) - }) }) describe.concurrent("expect", () => {