immediate-error 12.2.1 → 12.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.test.js CHANGED
@@ -1,257 +1,266 @@
1
- // tests
2
-
3
- const {
4
- immediateError,
5
- ErrorType,
6
- throwWhatever,
7
- getError,
8
- MESSAGES
9
- } = require("./index")
10
-
11
- describe("immediateError utility", () => {
12
- test("throws a regular Error with default message when no arguments are passed", () => {
13
- expect(() => immediateError()).toThrow(Error)
14
- expect(() => immediateError()).toThrow("ERROR!")
15
- })
16
-
17
- test("throws a regular Error with a custom message", () => {
18
- expect(() => immediateError("Aaaaah")).toThrow(Error)
19
- expect(() => immediateError("Aaaaah")).toThrow("Aaaaah")
20
- })
21
-
22
- test.each([
23
- ["BaseError", ErrorType.BaseError, Error],
24
- ["EvalError", ErrorType.EvalError, EvalError],
25
- ["RangeError", ErrorType.RangeError, RangeError],
26
- ["ReferenceError", ErrorType.ReferenceError, ReferenceError],
27
- ["SyntaxError", ErrorType.SyntaxError, SyntaxError],
28
- ["TypeError", ErrorType.TypeError, TypeError],
29
- ["URIError", ErrorType.URIError, URIError],
30
- ])("throws % when specified", (name, type, constructor) => {
31
- expect(() => immediateError("test message", type)).toThrow(constructor)
32
- })
33
-
34
- test.each([
35
- ["FruitConsumptionError", ErrorType.FruitConsumptionError],
36
- ["VegetablesDoNotTalkError", ErrorType.VegetablesDoNotTalkError],
37
- ["PersonNotHungryError", ErrorType.PersonNotHungryError],
38
- ["PortionsError", ErrorType.PortionsError],
39
- ])("throws domain-specific % correctly", (name, type) => {
40
- const expectedConstructor = getError(type)
41
- expect(() => immediateError("enterprise failure", type)).toThrow(expectedConstructor)
42
- expect(() => immediateError("enterprise failure", type)).toThrow("enterprise failure")
43
- })
44
-
45
- test("throws a custom user-defined Error class", () => {
46
- class MyCustomError extends Error {
47
- constructor(message) {
48
- super("Custom: " + message)
49
- this.name = "MyCustomError"
50
- }
51
- }
52
-
53
- expect(() => immediateError("Error!", MyCustomError)).toThrow(MyCustomError)
54
- expect(() => immediateError("Error!", MyCustomError)).toThrow(
55
- "Custom: Error!",
56
- )
57
- })
58
-
59
- test("captures stack trace correctly and hides internal frames", () => {
60
- try {
61
- immediateError("stack check")
62
- } catch (error) {
63
- expect(error.stack).not.toMatch(/at immediateError/)
64
- }
65
- })
66
- })
67
-
68
- const { attempt } = require("./index")
69
-
70
- describe("attempt utility", () => {
71
- test("works as a standard function", () => {
72
- let called = false
73
- attempt(() => {
74
- called = true
75
- }).end()
76
- expect(called).toBe(true)
77
- })
78
-
79
- test("works as a constructor returning an instance", () => {
80
- const instance = new attempt(() => {})
81
- expect(instance).toBeDefined()
82
- expect(typeof instance.rescue).toBe("function")
83
- })
84
-
85
- test("triggers rescue when the handler fails", () => {
86
- let errorCaught = false
87
- attempt(() => {
88
- throw new Error("fail")
89
- })
90
- .rescue((e) => {
91
- errorCaught = true
92
- expect(e.message).toBe("fail")
93
- })
94
- .end()
95
- expect(errorCaught).toBe(true)
96
- })
97
-
98
- test("triggers else only when the handler succeeds", () => {
99
- let elseCalled = false
100
- attempt(() => {
101
- return "success"
102
- })
103
- .else(() => {
104
- elseCalled = true
105
- })
106
- .end()
107
- expect(elseCalled).toBe(true)
108
- })
109
-
110
- test("triggers ensure regardless of success or failure", () => {
111
- let counter = 0
112
-
113
- attempt(() => {})
114
- .ensure(() => {
115
- counter++
116
- })
117
- .end()
118
-
119
- attempt(() => {
120
- throw new Error()
121
- })
122
- .rescue(() => {})
123
- .ensure(() => {
124
- counter++
125
- })
126
- .end()
127
-
128
- expect(counter).toBe(2)
129
- })
130
-
131
- test("returns this (the instance) from chaining methods", () => {
132
- const a = attempt(() => {})
133
- const b = a.rescue(() => {})
134
- const c = b.else(() => {})
135
- const d = c.ensure(() => {})
136
-
137
- expect(a).toBe(d)
138
- })
139
- })
140
-
141
- const { delayedError } = require("./index")
142
-
143
- describe("delayedError utility", () => {
144
- const SHORT_DELAY = 10
145
-
146
- test("throws the error after a specified delay", async () => {
147
- const start = Date.now()
148
-
149
- try {
150
- await delayedError("Delayed fail", ErrorType.BaseError, SHORT_DELAY)
151
- } catch (error) {
152
- const duration = Date.now() - start
153
- expect(duration).toBeGreaterThanOrEqual(SHORT_DELAY)
154
- expect(error.message).toBe("Delayed fail")
155
- }
156
- })
157
-
158
- test("uses default error message and type if only delay is provided", async () => {
159
- try {
160
- await delayedError(undefined, undefined, SHORT_DELAY)
161
- } catch (error) {
162
- expect(error.message).toBe("ERROR!")
163
- expect(error).toBeInstanceOf(Error)
164
- }
165
- })
166
-
167
- test("respects custom error types in delayed mode", async () => {
168
- try {
169
- await delayedError("Type fail", ErrorType.TypeError, SHORT_DELAY)
170
- } catch (error) {
171
- expect(error).toBeInstanceOf(TypeError)
172
- expect(error.message).toBe("Type fail")
173
- }
174
- })
175
- })
176
-
177
- describe("throwWhatever utility", () => {
178
- test("throws a standard error object when passed", () => {
179
- const err = new Error("standard fail")
180
- expect(() => throwWhatever(err)).toThrow("standard fail")
181
- })
182
-
183
- test("throws primitive false when passed false", () => {
184
- try {
185
- throwWhatever(false)
186
- } catch (e) {
187
- expect(e).toBe(false)
188
- }
189
- })
190
-
191
- test("throws primitive zero when passed 0", () => {
192
- try {
193
- throwWhatever(0)
194
- } catch (e) {
195
- expect(e).toBe(0)
196
- }
197
- })
198
-
199
- test("throws a string directly when passed a string", () => {
200
- expect(() => throwWhatever("Direct String")).toThrow("Direct String")
201
- })
202
-
203
- test("integrates with getError for custom domain errors", () => {
204
- const FruitError = getError(ErrorType.FruitConsumptionError)
205
- const err = new FruitError("Too many apples")
206
- expect(() => throwWhatever(err)).toThrow(FruitError)
207
- })
208
- })
209
-
210
- describe("getError utility", () => {
211
- test("returns the intrinsic Error constructor for BaseError", () => {
212
- const result = getError(ErrorType.BaseError)
213
- expect(result).toBe(Error)
214
- })
215
-
216
- test("returns the intrinsic TypeError constructor for TypeError", () => {
217
- const result = getError(ErrorType.TypeError)
218
- expect(result).toBe(TypeError)
219
- })
220
-
221
- test("successfully extracts FruitConsumptionError from jsfruit logic", () => {
222
- const FruitError = getError(ErrorType.FruitConsumptionError)
223
- expect(typeof FruitError).toBe("function")
224
- const instance = new FruitError("test")
225
- expect(instance).toBeInstanceOf(Error)
226
- })
227
-
228
- test("successfully extracts PersonNotHungryError from libperson logic", () => {
229
- const HungerError = getError(ErrorType.PersonNotHungryError)
230
- expect(typeof HungerError).toBe("function")
231
- expect(new HungerError()).toBeDefined()
232
- })
233
-
234
- test("successfully extracts PortionsError from libguacamole logic", () => {
235
- const GuacError = getError(ErrorType.PortionsError)
236
- expect(typeof GuacError).toBe("function")
237
- expect(GuacError.name).toBe("PortionsError")
238
- })
239
-
240
- test("returns the same constructor when passed a constructor (identity)", () => {
241
- class MyError extends Error {}
242
- const result = getError(MyError)
243
- expect(result).toBe(MyError)
244
- })
245
- })
246
-
247
- describe("error messages", () => {
248
- test.each([
249
- ["no fruit left", MESSAGES.DOMAIN.FRUIT_CONSUMPTION_ERROR.NO_FRUIT_LEFT],
250
- ["vegetables can not talk", MESSAGES.DOMAIN.VEGETABLES_DO_NOT_TALK_ERROR.VEGETABLES_CAN_NOT_TALK],
251
- ["% is not hungry and cannot be fed", MESSAGES.DOMAIN.PERSON_NOT_HUNGRY_ERROR.IS_NOT_HUNGRY_AND_CANNOT_BE_FED],
252
- ["Portion size expected to be a positive integer", MESSAGES.DOMAIN.PORTIONS_ERROR.PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER],
253
- ["Too many portions", MESSAGES.DOMAIN.PORTIONS_ERROR.TOO_MANY_PORTIONS]
254
- ])("provides error message \"%s\" correctly", (a, b) => {
255
- expect(a).toEqual(b)
256
- })
257
- })
1
+ /* eslint-disable */
2
+ // tests
3
+
4
+ const {
5
+ immediateError,
6
+ ErrorType,
7
+ throwWhatever,
8
+ getError,
9
+ MESSAGES
10
+ } = require("./index")
11
+
12
+ describe("immediateError utility", () => {
13
+ test("throws a regular Error with default message when no arguments are passed", () => {
14
+ expect(() => immediateError()).toThrow(Error)
15
+ expect(() => immediateError()).toThrow("ERROR!")
16
+ })
17
+
18
+ test("throws a regular Error with a custom message", () => {
19
+ expect(() => immediateError("Aaaaah")).toThrow(Error)
20
+ expect(() => immediateError("Aaaaah")).toThrow("Aaaaah")
21
+ })
22
+
23
+ test.each([
24
+ ["BaseError", ErrorType.BaseError, Error],
25
+ ["EvalError", ErrorType.EvalError, EvalError],
26
+ ["RangeError", ErrorType.RangeError, RangeError],
27
+ ["ReferenceError", ErrorType.ReferenceError, ReferenceError],
28
+ ["SyntaxError", ErrorType.SyntaxError, SyntaxError],
29
+ ["TypeError", ErrorType.TypeError, TypeError],
30
+ ["URIError", ErrorType.URIError, URIError],
31
+ ])("throws % when specified", (name, type, constructor) => {
32
+ expect(() => immediateError("test message", type)).toThrow(constructor)
33
+ })
34
+
35
+ test.each([
36
+ ["FruitConsumptionError", ErrorType.FruitConsumptionError],
37
+ ["VegetablesDoNotTalkError", ErrorType.VegetablesDoNotTalkError],
38
+ ["PersonNotHungryError", ErrorType.PersonNotHungryError],
39
+ ["PortionsError", ErrorType.PortionsError],
40
+ ["FalseJSValidationFailedToPassError", ErrorType.FalseJSValidationFailedToPassError]
41
+ ])("throws domain-specific % correctly", (name, type) => {
42
+ const expectedConstructor = getError(type)
43
+ expect(() => immediateError("enterprise failure", type)).toThrow(expectedConstructor)
44
+ expect(() => immediateError("enterprise failure", type)).toThrow("enterprise failure")
45
+ })
46
+
47
+ test("throws a custom user-defined Error class", () => {
48
+ class MyCustomError extends Error {
49
+ constructor(message) {
50
+ super("Custom: " + message)
51
+ this.name = "MyCustomError"
52
+ }
53
+ }
54
+
55
+ expect(() => immediateError("Error!", MyCustomError)).toThrow(MyCustomError)
56
+ expect(() => immediateError("Error!", MyCustomError)).toThrow(
57
+ "Custom: Error!",
58
+ )
59
+ })
60
+
61
+ test("captures stack trace correctly and hides internal frames", () => {
62
+ try {
63
+ immediateError("stack check")
64
+ } catch (error) {
65
+ expect(error.stack).not.toMatch(/at immediateError/)
66
+ }
67
+ })
68
+ })
69
+
70
+ const { attempt } = require("./index")
71
+
72
+ describe("attempt utility", () => {
73
+ test("works as a standard function", () => {
74
+ let called = false
75
+ attempt(() => {
76
+ called = true
77
+ }).end()
78
+ expect(called).toBe(true)
79
+ })
80
+
81
+ test("works as a constructor returning an instance", () => {
82
+ const instance = new attempt(() => {})
83
+ expect(instance).toBeDefined()
84
+ expect(typeof instance.rescue).toBe("function")
85
+ })
86
+
87
+ test("triggers rescue when the handler fails", () => {
88
+ let errorCaught = false
89
+ attempt(() => {
90
+ throw new Error("fail")
91
+ })
92
+ .rescue((e) => {
93
+ errorCaught = true
94
+ expect(e.message).toBe("fail")
95
+ })
96
+ .end()
97
+ expect(errorCaught).toBe(true)
98
+ })
99
+
100
+ test("triggers else only when the handler succeeds", () => {
101
+ let elseCalled = false
102
+ attempt(() => {
103
+ return "success"
104
+ })
105
+ .else(() => {
106
+ elseCalled = true
107
+ })
108
+ .end()
109
+ expect(elseCalled).toBe(true)
110
+ })
111
+
112
+ test("triggers ensure regardless of success or failure", () => {
113
+ let counter = 0
114
+
115
+ attempt(() => {})
116
+ .ensure(() => {
117
+ counter++
118
+ })
119
+ .end()
120
+
121
+ attempt(() => {
122
+ throw new Error()
123
+ })
124
+ .rescue(() => {})
125
+ .ensure(() => {
126
+ counter++
127
+ })
128
+ .end()
129
+
130
+ expect(counter).toBe(2)
131
+ })
132
+
133
+ test("returns this (the instance) from chaining methods", () => {
134
+ const a = attempt(() => {})
135
+ const b = a.rescue(() => {})
136
+ const c = b.else(() => {})
137
+ const d = c.ensure(() => {})
138
+
139
+ expect(a).toBe(d)
140
+ })
141
+ })
142
+
143
+ const { delayedError } = require("./index")
144
+
145
+ describe("delayedError utility", () => {
146
+ const SHORT_DELAY = 10
147
+
148
+ test("throws the error after a specified delay", async () => {
149
+ const start = Date.now()
150
+
151
+ try {
152
+ await delayedError("Delayed fail", ErrorType.BaseError, SHORT_DELAY)
153
+ } catch (error) {
154
+ const duration = Date.now() - start
155
+ expect(duration).toBeGreaterThanOrEqual(SHORT_DELAY)
156
+ expect(error.message).toBe("Delayed fail")
157
+ }
158
+ })
159
+
160
+ test("uses default error message and type if only delay is provided", async () => {
161
+ try {
162
+ await delayedError(undefined, undefined, SHORT_DELAY)
163
+ } catch (error) {
164
+ expect(error.message).toBe("ERROR!")
165
+ expect(error).toBeInstanceOf(Error)
166
+ }
167
+ })
168
+
169
+ test("respects custom error types in delayed mode", async () => {
170
+ try {
171
+ await delayedError("Type fail", ErrorType.TypeError, SHORT_DELAY)
172
+ } catch (error) {
173
+ expect(error).toBeInstanceOf(TypeError)
174
+ expect(error.message).toBe("Type fail")
175
+ }
176
+ })
177
+ })
178
+
179
+ describe("throwWhatever utility", () => {
180
+ test("throws a standard error object when passed", () => {
181
+ const err = new Error("standard fail")
182
+ expect(() => throwWhatever(err)).toThrow("standard fail")
183
+ })
184
+
185
+ test("throws primitive false when passed false", () => {
186
+ try {
187
+ throwWhatever(false)
188
+ } catch (e) {
189
+ expect(e).toBe(false)
190
+ }
191
+ })
192
+
193
+ test("throws primitive zero when passed 0", () => {
194
+ try {
195
+ throwWhatever(0)
196
+ } catch (e) {
197
+ expect(e).toBe(0)
198
+ }
199
+ })
200
+
201
+ test("throws a string directly when passed a string", () => {
202
+ expect(() => throwWhatever("Direct String")).toThrow("Direct String")
203
+ })
204
+
205
+ test("integrates with getError for custom domain errors", () => {
206
+ const FruitError = getError(ErrorType.FruitConsumptionError)
207
+ const err = new FruitError("Too many apples")
208
+ expect(() => throwWhatever(err)).toThrow(FruitError)
209
+ })
210
+ })
211
+
212
+ describe("getError utility", () => {
213
+ test("returns the intrinsic Error constructor for BaseError", () => {
214
+ const result = getError(ErrorType.BaseError)
215
+ expect(result).toBe(Error)
216
+ })
217
+
218
+ test("returns the intrinsic TypeError constructor for TypeError", () => {
219
+ const result = getError(ErrorType.TypeError)
220
+ expect(result).toBe(TypeError)
221
+ })
222
+
223
+ test("successfully extracts FruitConsumptionError from jsfruit logic", () => {
224
+ const FruitError = getError(ErrorType.FruitConsumptionError)
225
+ expect(typeof FruitError).toBe("function")
226
+ const instance = new FruitError("test")
227
+ expect(instance).toBeInstanceOf(Error)
228
+ })
229
+
230
+ test("successfully extracts PersonNotHungryError from libperson logic", () => {
231
+ const HungerError = getError(ErrorType.PersonNotHungryError)
232
+ expect(typeof HungerError).toBe("function")
233
+ expect(new HungerError()).toBeDefined()
234
+ })
235
+
236
+ test("successfully extracts PortionsError from libguacamole logic", () => {
237
+ const GuacError = getError(ErrorType.PortionsError)
238
+ expect(typeof GuacError).toBe("function")
239
+ expect(GuacError.name).toBe("PortionsError")
240
+ })
241
+
242
+ test("successfully extracts FalseJSValidationFailedToPassError", () => {
243
+ const FalseJSValidationFailedToPassError = getError(ErrorType.FalseJSValidationFailedToPassError)
244
+ expect(typeof FalseJSValidationFailedToPassError).toBe("function")
245
+ expect(FalseJSValidationFailedToPassError.name).toBe("FalseJSValidationFailedToPassError")
246
+ })
247
+
248
+ test("returns the same constructor when passed a constructor (identity)", () => {
249
+ class MyError extends Error {}
250
+ const result = getError(MyError)
251
+ expect(result).toBe(MyError)
252
+ })
253
+ })
254
+
255
+ describe("error messages", () => {
256
+ test.each([
257
+ ["no fruit left", MESSAGES.DOMAIN.FRUIT_CONSUMPTION_ERROR.NO_FRUIT_LEFT],
258
+ ["vegetables can not talk", MESSAGES.DOMAIN.VEGETABLES_DO_NOT_TALK_ERROR.VEGETABLES_CAN_NOT_TALK],
259
+ ["% is not hungry and cannot be fed", MESSAGES.DOMAIN.PERSON_NOT_HUNGRY_ERROR.IS_NOT_HUNGRY_AND_CANNOT_BE_FED],
260
+ ["Portion size expected to be a positive integer", MESSAGES.DOMAIN.PORTIONS_ERROR.PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER],
261
+ ["Too many portions", MESSAGES.DOMAIN.PORTIONS_ERROR.TOO_MANY_PORTIONS],
262
+ ["Validation failed to pass", MESSAGES.DOMAIN.FALSEJS_VALIDATION_FAILED_TO_PASS_ERROR.VALIDATION_FAILED_TO_PASS]
263
+ ])("provides error message \"%s\" correctly", (a, b) => {
264
+ expect(a).toEqual(b)
265
+ })
266
+ })