Skip to content

Commit 046d4da

Browse files
committed
--wip--
1 parent e7830f9 commit 046d4da

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

tests/result/index.test-d.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// oxlint-disable typescript/restrict-template-expressions
2+
// oxlint-disable typescript/prefer-promise-reject-errors
3+
// oxlint-disable typescript/only-throw-error
4+
// oxlint-disable require-yield
5+
6+
import { describe, expect, expectTypeOf, test } from "vitest";
7+
8+
import { Err, Ok, Result } from "@/result";
9+
10+
describe("Result.gen (type)", () => {
11+
test("should handle generator returning non-Result value", () => {
12+
const value = Result.gen(function* () {
13+
return 1;
14+
});
15+
expect(Result.is(value)).toBe(true);
16+
expect(value.isOk()).toBe(true);
17+
expect(value.unwrap()).toBe(1);
18+
});
19+
20+
test("should handle generator returning Result value", () => {
21+
const ok = Ok(2);
22+
const value = Result.gen(function* () {
23+
return ok;
24+
});
25+
expect(value).toBe(ok);
26+
27+
const err = Err("error");
28+
const error = Result.gen(function* () {
29+
return err;
30+
});
31+
expect(error).toBe(err);
32+
});
33+
34+
test("should handle async generators returning non-Result value", () => {
35+
const value = Result.gen(function* () {
36+
return 1;
37+
});
38+
39+
expect(Result.is(value)).toBe(true);
40+
expect(value.isOk()).toBe(true);
41+
expect(value.unwrap()).toBe(1);
42+
});
43+
44+
test("should handle async generators returning Result value", () => {
45+
const ok = Ok(2);
46+
const value = Result.gen(function* () {
47+
return ok;
48+
});
49+
expect(value).toBe(ok);
50+
51+
const err = Err("error");
52+
const error = Result.gen(function* () {
53+
return err;
54+
});
55+
expect(error).toBe(err);
56+
});
57+
});
58+
59+
describe("Result.try (type)", () => {
60+
test("should return Ok if the function executes successfully", () => {
61+
const result = Result.try(() => {
62+
return 42;
63+
});
64+
expectTypeOf(result).toEqualTypeOf<Result<number, unknown>>();
65+
expect(result.isOk()).toBe(true);
66+
expect(result.unwrap()).toBe(42);
67+
});
68+
69+
test("should return Err if the function throws", () => {
70+
const result = Result.try(() => {
71+
throw "Test error";
72+
});
73+
expectTypeOf(result).toEqualTypeOf<Result<never, unknown>>();
74+
expect(result.isErr()).toBe(true);
75+
expect(result.unwrapErr()).toBe("Test error");
76+
});
77+
78+
test("should return Err with Error instance if the function throws", () => {
79+
const result = Result.try(() => {
80+
throw "Test error";
81+
}, Error);
82+
expectTypeOf(result).toEqualTypeOf<Result<never, Error>>();
83+
expect(result.isErr()).toBe(true);
84+
expect(result.unwrapErr()).toBeInstanceOf(Error);
85+
expect(result.unwrapErr().message).toBe("Test error");
86+
});
87+
88+
test("should return Err with the specified error type if the function throws", () => {
89+
const result = Result.try(
90+
() => {
91+
throw "Test error";
92+
},
93+
error => `Custom error: ${error}`,
94+
);
95+
expectTypeOf(result).toEqualTypeOf<Result<never, string>>();
96+
expect(result.isErr()).toBe(true);
97+
expect(result.unwrapErr()).toBe("Custom error: Test error");
98+
});
99+
100+
test("should return Ok if the async function executes successfully", () => {
101+
const result = Result.try(() => {
102+
return 42;
103+
});
104+
expectTypeOf(result).toEqualTypeOf<Result<number, unknown>>();
105+
expect(result.isOk()).toBe(true);
106+
expect(result.unwrap()).toBe(42);
107+
});
108+
109+
test("should return Err if the async function throws", () => {
110+
const result = Result.try(() => {
111+
throw "Test error";
112+
});
113+
expectTypeOf(result).toEqualTypeOf<Result<never, unknown>>();
114+
expect(result.isErr()).toBe(true);
115+
expect(result.unwrapErr()).toBe("Test error");
116+
});
117+
118+
test("should return Err with Error instance if the async function throws", () => {
119+
const result = Result.try(() => {
120+
throw "Test error";
121+
}, Error);
122+
expectTypeOf(result).toEqualTypeOf<Result<never, Error>>();
123+
expect(result.isErr()).toBe(true);
124+
expect(result.unwrapErr()).toBeInstanceOf(Error);
125+
expect(result.unwrapErr().message).toBe("Test error");
126+
});
127+
128+
test("should return Err with the specified error type if the async function throws", () => {
129+
const result = Result.try(
130+
() => {
131+
throw "Test error";
132+
},
133+
error => `Custom error: ${error}`,
134+
);
135+
expectTypeOf(result).toEqualTypeOf<Result<never, string>>();
136+
expect(result.isErr()).toBe(true);
137+
expect(result.unwrapErr()).toBe("Custom error: Test error");
138+
});
139+
140+
test("should return Ok if the promise resolves", async () => {
141+
const result = await Result.try(async () => Promise.resolve(42));
142+
expectTypeOf(result).toEqualTypeOf<Result<number, unknown>>();
143+
expect(result.isOk()).toBe(true);
144+
expect(result.unwrap()).toBe(42);
145+
});
146+
147+
test("should return Err if the promise rejects", async () => {
148+
const result = await Result.try(async () => Promise.reject("Test error"));
149+
expectTypeOf(result).toEqualTypeOf<Result<never, unknown>>();
150+
expect(result.isErr()).toBe(true);
151+
expect(result.unwrapErr()).toBe("Test error");
152+
});
153+
154+
test("should return Err with Error instance if the promise rejects with an Error", async () => {
155+
const result = await Result.try(async () => Promise.reject("Test error"), Error);
156+
expectTypeOf(result).toEqualTypeOf<Result<never, Error>>();
157+
expect(result.isErr()).toBe(true);
158+
expect(result.unwrapErr()).toBeInstanceOf(Error);
159+
expect(result.unwrapErr().message).toBe("Test error");
160+
});
161+
162+
test("should return Err with the specified error type if the promise rejects with an error", async () => {
163+
const result = await Result.try(
164+
async () => Promise.reject("Test error"),
165+
error => `Custom error: ${error}`,
166+
);
167+
expectTypeOf(result).toEqualTypeOf<Result<never, string>>();
168+
expect(result.isErr()).toBe(true);
169+
expect(result.unwrapErr()).toBe("Custom error: Test error");
170+
});
171+
});

vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ export default defineConfig({
99
],
1010
test: {
1111
include: ["tests/**/*.test.ts"],
12+
typecheck: {
13+
enabled: true,
14+
},
1215
},
1316
});

0 commit comments

Comments
 (0)