-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.test.js
213 lines (206 loc) · 5.79 KB
/
errors.test.js
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
import {
mustBe,
hasErrors,
isValidOrLogAll,
mustBeOrThrowAll,
SchemaValidationError,
EnumValidationError,
SerieValidationError,
TypeValidationError,
} from "garn-validator";
describe("AggregateError", () => {
test.each([AggregateError, SchemaValidationError, Error])(
"if schema fails with more than 2 errors should throw %p",
(ErrorType) => {
expect(() => {
mustBeOrThrowAll({ a: Number, b: String })({});
}).toThrow(ErrorType);
}
);
test.each([AggregateError, EnumValidationError, Error])(
"if enums fails with more than 2 errors should throw %p",
(ErrorType) => {
expect(() => {
mustBeOrThrowAll([Number, String])(true);
}).toThrow(ErrorType);
}
);
test.each([AggregateError, SerieValidationError, Error])(
"if Series fails with more than 2 errors should throw %p",
(ErrorType) => {
expect(() => {
mustBeOrThrowAll(Number, String)(true);
}).toThrow(ErrorType);
}
);
test("checking schema should throw SchemaValidationError or TypeValidationError", () => {
try {
mustBeOrThrowAll({ a: 1, b: 2 })({});
} catch (error) {
expect(error instanceof SchemaValidationError).toBe(true);
expect(error instanceof AggregateError).toBe(true);
expect(error.errors.length).toBe(2);
}
try {
mustBe({ a: 1, b: 2 })({});
} catch (error) {
expect(error instanceof SchemaValidationError).toBe(false);
expect(error instanceof TypeValidationError).toBe(true);
}
// only 1 key fails
try {
mustBeOrThrowAll({ a: 1 })({});
} catch (error) {
expect(error instanceof TypeValidationError).toBe(true);
expect(error instanceof SchemaValidationError).toBe(false);
}
});
test("checking enum should throw EnumValidationError or TypeValidationError", () => {
try {
mustBe([Boolean, String])(1);
} catch (error) {
expect(error instanceof EnumValidationError).toBe(true);
expect(error instanceof AggregateError).toBe(true);
}
try {
mustBe([Boolean])(1);
} catch (error) {
expect(error instanceof EnumValidationError).toBe(false);
expect(error instanceof TypeValidationError).toBe(true);
}
});
test("checking series should throw SerieValidationError or TypeValidationError ", () => {
try {
mustBeOrThrowAll(Boolean, String)(1);
} catch (error) {
expect(error instanceof SerieValidationError).toBe(true);
expect(error instanceof AggregateError).toBe(true);
}
try {
mustBeOrThrowAll(Boolean)(1);
} catch (error) {
expect(error instanceof SerieValidationError).toBe(false);
expect(error instanceof TypeValidationError).toBe(true);
}
});
test("should message enum", () => {
try {
mustBe([
() => {
throw "ups";
},
String,
])(1);
throw "mec";
} catch (error) {
expect(error).toBeInstanceOf(EnumValidationError);
expect(error.message).toMatch("enum");
expect(error).toBeInstanceOf(AggregateError);
}
});
});
describe("check errors", () => {
test("by default throws TypeValidationError", () => {
expect(() => {
mustBe(Boolean)(33);
}).toThrow(TypeValidationError);
});
test("Should throw meaningfully message", () => {
expect(() => {
mustBe(1)(33);
}).toThrow("33 do not match primitive 1");
});
test("should throw a custom type of error", () => {
expect(() => {
mustBe((v) => {
if (v > 10) throw new RangeError("ups");
})(33);
}).toThrow(RangeError);
});
test("should throw a custom type of error", () => {
expect(() => {
mustBe((v) => {
if (v > 10) throw new RangeError("ups");
})(33);
}).toThrow("ups");
});
test("should throw anything", () => {
try {
mustBe((v) => {
if (v > 10) throw "ups";
})(33);
} catch (error) {
expect(error).toBe("ups");
}
});
test("should throw anything", () => {
try {
mustBeOrThrowAll(
() => {
throw 1;
},
() => {
throw 2;
}
)(33);
} catch (error) {
// error is AggregateError
expect(error).toBeInstanceOf(AggregateError);
expect(error.errors).toEqual([1, 2]);
}
});
test("should format the schema", () => {
expect(() => {
mustBe({ a: Number })(33);
}).toThrow('33 do not match schema {"a":Number}');
});
test("should format the value", () => {
expect(() => {
mustBe({ a: Number })({ b: 33 });
}).toThrow("At path /a undefined do not match constructor Number");
});
});
describe("check errors in serie", () => {
test("should throw the error message related to the check failed", () => {
expect(() => {
mustBe(Number, String)(2);
}).toThrow("2 do not match constructor String");
});
test("should throw the error message related to the check failed", () => {
expect(() => {
mustBe(() => {
throw new Error();
}, String)(2);
}).toThrow(Error);
});
test("should check only until the first check fails", () => {
jest.spyOn(globalThis.console, "log");
try {
mustBe(
() => {
throw new Error();
},
() => console.log("I run?")
)(2);
} catch (err) {}
expect(globalThis.console.log).not.toHaveBeenCalled();
});
});
// describes("check with invalid validator", () => {
// test("should detect async functions", () => {
// try {
// mustBe(async () => false)(1);
// throw "mec";
// } catch (error) {
// expect(error).toBeInstanceOf(SyntaxError);
// }
// });
// test("should detect generators", () => {
// try {
// mustBe(function* () {})(1);
// throw "mec";
// } catch (error) {
// expect(error).toBeInstanceOf(SyntaxError);
// }
// });
// });