Skip to content

Commit c342988

Browse files
lambdalisueclaude
andcommitted
test: add tests for error module
Add comprehensive tests for error.ts including: - ExpectedError class constructor with and without source - handleError function for ExpectedError, AssertError, and unknown errors - withHandleError wrapper for sync and async functions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e892695 commit c342988

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

denops/fall/error_test.ts

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { DenopsStub } from "jsr:@denops/test@^3.0.0";
2+
import { assertEquals, assertInstanceOf } from "jsr:@std/assert@^1.0.0";
3+
import { describe, it } from "jsr:@std/testing@^1.0.0/bdd";
4+
import { AssertError } from "jsr:@core/unknownutil@^4.3.0/assert";
5+
import { ExpectedError, handleError, withHandleError } from "./error.ts";
6+
7+
describe("ExpectedError", () => {
8+
it("should create an error with message", () => {
9+
const error = new ExpectedError("test message");
10+
assertInstanceOf(error, Error);
11+
assertEquals(error.message, "test message");
12+
assertEquals(error.source, undefined);
13+
});
14+
15+
it("should create an error with message and source", () => {
16+
const source = new Error("source error");
17+
const error = new ExpectedError("test message", source);
18+
assertEquals(error.message, "test message");
19+
assertEquals(error.source, source);
20+
});
21+
});
22+
23+
describe("handleError", () => {
24+
it("should handle ExpectedError by showing echo message", async () => {
25+
const called: string[] = [];
26+
const denops = new DenopsStub({
27+
cmd(command: string): Promise<void> {
28+
called.push(command);
29+
return Promise.resolve();
30+
},
31+
});
32+
const error = new ExpectedError("expected error");
33+
34+
await handleError(denops, error);
35+
36+
assertEquals(called.length, 1);
37+
assertEquals(
38+
called[0],
39+
"redraw | echohl Error | echomsg '[fall] expected error' | echohl None",
40+
);
41+
});
42+
43+
it("should handle AssertError by showing echo message", async () => {
44+
const called: string[] = [];
45+
const denops = new DenopsStub({
46+
cmd(command: string): Promise<void> {
47+
called.push(command);
48+
return Promise.resolve();
49+
},
50+
});
51+
const error = new AssertError("assertion failed");
52+
53+
await handleError(denops, error);
54+
55+
assertEquals(called.length, 1);
56+
assertEquals(
57+
called[0],
58+
"redraw | echohl Error | echomsg '[fall] assertion failed' | echohl None",
59+
);
60+
});
61+
62+
it("should handle unknown errors by logging to console", async () => {
63+
const called: string[] = [];
64+
const denops = new DenopsStub({
65+
cmd(command: string): Promise<void> {
66+
called.push(command);
67+
return Promise.resolve();
68+
},
69+
});
70+
const error = new Error("unknown error");
71+
const originalConsoleError = console.error;
72+
let capturedError: unknown;
73+
74+
console.error = (err: unknown) => {
75+
capturedError = err;
76+
};
77+
78+
try {
79+
await handleError(denops, error);
80+
assertEquals(capturedError, error);
81+
assertEquals(called.length, 0);
82+
} finally {
83+
console.error = originalConsoleError;
84+
}
85+
});
86+
});
87+
88+
describe("withHandleError", () => {
89+
it("should return the result when no error occurs", async () => {
90+
const denops = new DenopsStub();
91+
const fn = (x: number, y: number) => x + y;
92+
const wrapped = withHandleError(denops, fn);
93+
94+
const result = await wrapped(1, 2);
95+
assertEquals(result, 3);
96+
});
97+
98+
it("should return the result for async functions", async () => {
99+
const denops = new DenopsStub();
100+
const fn = async (x: number, y: number) => {
101+
await Promise.resolve();
102+
return x + y;
103+
};
104+
const wrapped = withHandleError(denops, fn);
105+
106+
const result = await wrapped(1, 2);
107+
assertEquals(result, 3);
108+
});
109+
110+
it("should handle errors and return undefined", async () => {
111+
const called: string[] = [];
112+
const denops = new DenopsStub({
113+
cmd(command: string): Promise<void> {
114+
called.push(command);
115+
return Promise.resolve();
116+
},
117+
});
118+
const fn = () => {
119+
throw new ExpectedError("test error");
120+
};
121+
const wrapped = withHandleError(denops, fn);
122+
123+
const result = await wrapped();
124+
assertEquals(result, undefined);
125+
126+
// Wait for setTimeout to execute
127+
await new Promise((resolve) => setTimeout(resolve, 10));
128+
129+
assertEquals(called.length, 1);
130+
assertEquals(
131+
called[0],
132+
"redraw | echohl Error | echomsg '[fall] test error' | echohl None",
133+
);
134+
});
135+
136+
it("should handle async function errors", async () => {
137+
const denops = new DenopsStub();
138+
const fn = async () => {
139+
await Promise.resolve();
140+
throw new Error("async error");
141+
};
142+
const wrapped = withHandleError(denops, fn);
143+
144+
const originalConsoleError = console.error;
145+
let capturedError: unknown;
146+
147+
console.error = (err: unknown) => {
148+
capturedError = err;
149+
};
150+
151+
try {
152+
const result = await wrapped();
153+
assertEquals(result, undefined);
154+
155+
// Wait for setTimeout to execute
156+
await new Promise((resolve) => setTimeout(resolve, 10));
157+
158+
assertInstanceOf(capturedError, Error);
159+
assertEquals((capturedError as Error).message, "async error");
160+
} finally {
161+
console.error = originalConsoleError;
162+
}
163+
});
164+
});

0 commit comments

Comments
 (0)