-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
229 lines (194 loc) · 7.2 KB
/
index.test.js
File metadata and controls
229 lines (194 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// @ts-check
import { test, describe } from "node:test";
import assert from "node:assert";
import { getError } from "./index.js";
describe("getError", () => {
// Basic JavaScript value types
describe("primitive values", () => {
test("should handle null", () => {
const error = getError(null);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle undefined", () => {
const error = getError(undefined);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle strings", () => {
const errorMessage = "Something went wrong";
const error = getError(errorMessage);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, errorMessage);
});
test("should handle numbers", () => {
const error = getError(42);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle booleans", () => {
const error = getError(true);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle symbols", () => {
const error = getError(Symbol("test"));
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle BigInt", () => {
const error = getError(BigInt(123));
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
});
// Object types
describe("object values", () => {
test("should handle plain objects", () => {
const error = getError({});
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle arrays", () => {
const error = getError([1, 2, 3]);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle objects with message property", () => {
const errorObj = { message: "Custom error message" };
const error = getError(errorObj);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Custom error message");
});
test("should preserve additional properties from message objects", () => {
const errorObj = {
message: "Custom error message",
code: "ERR_CUSTOM",
details: { something: "important" },
};
const error = getError(errorObj);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Custom error message");
assert.ok("code" in error);
assert.strictEqual(error.code, "ERR_CUSTOM");
assert.ok("details" in error);
assert.deepStrictEqual(error.details, { something: "important" });
});
});
// Error instances
describe("error instances", () => {
test("should pass through Error instances unchanged", () => {
const originalError = new Error("Original error");
Object.assign(originalError, { code: "ORIGINAL_ERROR" });
const error = getError(originalError);
assert.strictEqual(error, originalError); // Should be the same instance
assert.strictEqual(error.message, "Original error");
assert.ok("code" in error);
assert.strictEqual(error.code, "ORIGINAL_ERROR");
});
test("should handle Error subclasses", () => {
class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}
const originalError = new CustomError("Custom error type");
const error = getError(originalError);
assert.strictEqual(error, originalError);
assert.strictEqual(error.name, "CustomError");
});
test("should handle DOMExceptions", () => {
const exception = new DOMException("Dom Exception Message");
const error = getError(exception);
assert.strictEqual(error, exception); // Should be the same instance
assert.strictEqual(error.message, "Dom Exception Message");
});
});
// Response objects
describe("Response objects", () => {
test("should handle Response objects", () => {
const response = new Response(null, {
status: 404,
statusText: "Not Found",
});
const error = getError(response);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "404 Not Found");
});
});
// RPC-style error objects
describe("RPC-style error objects", () => {
test("should handle { error: string } objects", () => {
const rpcError = { error: "Something failed" };
const error = getError(rpcError);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Something failed");
});
test("should handle { error: { message: string } } objects", () => {
const rpcError = { error: { message: "Nested error message" } };
const error = getError(rpcError);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Nested error message");
});
test("should handle complex nested error objects", () => {
const rpcError = {
error: {
message: "RPC failed",
code: "INVALID_REQUEST",
data: { reason: "Bad input" },
},
};
const error = getError(rpcError);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "RPC failed");
assert.ok("code" in error);
assert.strictEqual(error.code, "INVALID_REQUEST");
assert.ok("data" in error);
assert.deepStrictEqual(error.data, { reason: "Bad input" });
});
});
// Custom fallback
describe("custom fallback errors", () => {
test("should use provided fallback error", () => {
const fallback = new Error("Custom fallback");
const error = getError(null, fallback);
assert.strictEqual(error, fallback);
assert.strictEqual(error.message, "Custom fallback");
});
test("should not use fallback when a valid error can be extracted", () => {
const fallback = new Error("Custom fallback");
const error = getError({ message: "Real error" }, fallback);
assert.notStrictEqual(error, fallback);
assert.strictEqual(error.message, "Real error");
});
});
// Promise-related
describe("promise-related errors", () => {
test("should handle rejected promises in async functions", async () => {
try {
await Promise.reject({ message: "Promise rejected" });
} catch (unknown) {
const error = getError(unknown);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Promise rejected");
}
});
});
// Edge cases
describe("edge cases", () => {
test("should handle circular references", () => {
const circular = {};
circular.self = circular;
const error = getError(circular);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
test("should handle functions", () => {
const func = () => {};
const error = getError(func);
assert.ok(error instanceof Error);
assert.strictEqual(error.message, "Unknown Error");
});
});
});