@augment-vir/assert 30.0.0 → 30.0.1
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/README.md +11 -0
- package/dist/assertions/boolean.d.ts +443 -17
- package/dist/assertions/boolean.js +365 -8
- package/dist/assertions/boundary.d.ts +657 -13
- package/dist/assertions/boundary.js +537 -5
- package/dist/assertions/enum.d.ts +236 -8
- package/dist/assertions/enum.js +197 -5
- package/dist/assertions/equality/entry-equality.d.ts +287 -11
- package/dist/assertions/equality/entry-equality.js +243 -6
- package/dist/assertions/equality/json-equality.d.ts +244 -15
- package/dist/assertions/equality/json-equality.js +207 -11
- package/dist/assertions/equality/simple-equality.d.ts +849 -28
- package/dist/assertions/equality/simple-equality.js +712 -6
- package/dist/assertions/equality/ts-type-equality.d.ts +37 -1
- package/dist/assertions/equality/ts-type-equality.js +13 -1
- package/dist/assertions/extendable-assertions.d.ts +288 -120
- package/dist/assertions/extendable-assertions.js +32 -60
- package/dist/assertions/http.d.ts +217 -10
- package/dist/assertions/http.js +182 -6
- package/dist/assertions/instance.d.ts +189 -8
- package/dist/assertions/instance.js +159 -5
- package/dist/assertions/keys.d.ts +658 -13
- package/dist/assertions/keys.js +556 -5
- package/dist/assertions/length.d.ts +381 -9
- package/dist/assertions/length.js +309 -5
- package/dist/assertions/nullish.d.ts +169 -7
- package/dist/assertions/nullish.js +137 -6
- package/dist/assertions/numeric.d.ts +965 -11
- package/dist/assertions/numeric.js +819 -1
- package/dist/assertions/output.d.ts +107 -7
- package/dist/assertions/output.js +92 -5
- package/dist/assertions/primitive.d.ts +416 -13
- package/dist/assertions/primitive.js +352 -6
- package/dist/assertions/promise.d.ts +640 -21
- package/dist/assertions/promise.js +536 -15
- package/dist/assertions/regexp.d.ts +202 -3
- package/dist/assertions/regexp.js +173 -1
- package/dist/assertions/runtime-type.d.ts +1822 -41
- package/dist/assertions/runtime-type.js +1558 -35
- package/dist/assertions/throws.d.ts +265 -17
- package/dist/assertions/throws.js +229 -17
- package/dist/assertions/uuid.d.ts +233 -10
- package/dist/assertions/uuid.js +195 -6
- package/dist/assertions/values.d.ts +1086 -15
- package/dist/assertions/values.js +907 -6
- package/dist/augments/assertion.error.d.ts +2 -1
- package/dist/augments/assertion.error.js +2 -1
- package/dist/augments/guards/assert-wrap.d.ts +82 -37
- package/dist/augments/guards/assert-wrap.js +13 -2
- package/dist/augments/guards/assert.d.ts +30 -14
- package/dist/augments/guards/assert.js +21 -4
- package/dist/augments/guards/check-wrap.d.ts +94 -51
- package/dist/augments/guards/check-wrap.js +11 -3
- package/dist/augments/guards/check.d.ts +87 -37
- package/dist/augments/guards/check.js +9 -2
- package/dist/augments/guards/wait-until.d.ts +110 -103
- package/dist/augments/guards/wait-until.js +18 -3
- package/dist/augments/if-equals.d.ts +4 -2
- package/dist/guard-types/assert-wrap-function.d.ts +5 -2
- package/dist/guard-types/check-function.d.ts +5 -2
- package/dist/guard-types/check-wrap-wrapper-function.d.ts +4 -1
- package/dist/guard-types/guard-group.d.ts +7 -8
- package/dist/guard-types/wait-until-function.d.ts +8 -3
- package/dist/guard-types/wait-until-function.js +1 -1
- package/package.json +17 -4
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MaybePromise, PartialWithNullable, TypedFunction } from '@augment-vir/core';
|
|
2
|
+
import { autoGuardSymbol } from '../guard-types/guard-override.js';
|
|
2
3
|
import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
|
|
3
4
|
declare function isError(actual: unknown, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): asserts actual is Error;
|
|
4
5
|
/**
|
|
@@ -7,6 +8,7 @@ declare function isError(actual: unknown, matchOptions?: ErrorMatchOptions | und
|
|
|
7
8
|
* property is optional, and whichever properties are provided will be checked.
|
|
8
9
|
*
|
|
9
10
|
* @category Assert : Util
|
|
11
|
+
* @category Package : @augment-vir/assert
|
|
10
12
|
* @example
|
|
11
13
|
*
|
|
12
14
|
* ```ts
|
|
@@ -27,7 +29,7 @@ declare function isError(actual: unknown, matchOptions?: ErrorMatchOptions | und
|
|
|
27
29
|
* ); // this assertion will pass
|
|
28
30
|
* ```
|
|
29
31
|
*
|
|
30
|
-
* @package
|
|
32
|
+
* @package [`@augment-vir/assert`](https://www.npmjs.com/package/@augment-vir/assert)
|
|
31
33
|
*/
|
|
32
34
|
export type ErrorMatchOptions = PartialWithNullable<{
|
|
33
35
|
/**
|
|
@@ -64,46 +66,292 @@ declare function throwsCheckWrap(callbackOrPromise: TypedFunction<void, never>,
|
|
|
64
66
|
declare function throwsCheckWrap(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error | undefined>;
|
|
65
67
|
declare function throwsCheckWrap(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
|
|
66
68
|
declare function throwsCheckWrap(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error | undefined>;
|
|
67
|
-
declare function throwsWaitUntil(
|
|
68
|
-
declare function throwsWaitUntil(matchOptions: ErrorMatchOptions,
|
|
69
|
+
declare function throwsWaitUntil(callback: TypedFunction<void, any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
|
|
70
|
+
declare function throwsWaitUntil(matchOptions: ErrorMatchOptions, callback: TypedFunction<void, any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
|
|
69
71
|
export declare const throwGuards: {
|
|
70
|
-
|
|
72
|
+
assert: {
|
|
71
73
|
/**
|
|
72
|
-
*
|
|
74
|
+
* If a function input is provided:
|
|
75
|
+
*
|
|
76
|
+
* Calls that function and asserts that the function throw an error, comparing the error with
|
|
77
|
+
* the given {@link ErrorMatchOptions}, if provided.
|
|
78
|
+
*
|
|
79
|
+
* If a promise is provided:
|
|
80
|
+
*
|
|
81
|
+
* Awaits the promise and asserts that the promise rejected with an error, comparing the error
|
|
82
|
+
* with the given {@link ErrorMatchOptions}, if provided.
|
|
83
|
+
*
|
|
84
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
85
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
86
|
+
*
|
|
87
|
+
* Performs no type guarding.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import {assert} from '@augment-vir/assert';
|
|
93
|
+
*
|
|
94
|
+
* assert.throws(() => {
|
|
95
|
+
* throw new Error();
|
|
96
|
+
* }); // passes
|
|
97
|
+
* assert.throws(
|
|
98
|
+
* () => {
|
|
99
|
+
* throw new Error();
|
|
100
|
+
* },
|
|
101
|
+
* {matchMessage: 'hi'},
|
|
102
|
+
* ); // fails
|
|
103
|
+
* await assert.throws(Promise.reject()); // passes
|
|
104
|
+
* assert.throws(() => {}); // fails
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @throws {@link AssertionError} If the assertion fails.
|
|
108
|
+
*/
|
|
109
|
+
throws: typeof throws;
|
|
110
|
+
/**
|
|
111
|
+
* Asserts that a value is an instance of the built-in `Error` class and compares it to the
|
|
73
112
|
* given {@link ErrorMatchOptions}, if provided.
|
|
74
113
|
*
|
|
75
114
|
* Type guards the input.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* import {assert} from '@augment-vir/assert';
|
|
120
|
+
*
|
|
121
|
+
* assert.isError(new Error()); // passes
|
|
122
|
+
* assert.isError(new Error(), {matchMessage: 'hi'}); // fails
|
|
123
|
+
* assert.isError({message: 'not an error'}); // fails
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* @throws {@link AssertionError} If the assertion fails.
|
|
76
127
|
*/
|
|
77
128
|
isError: typeof isError;
|
|
129
|
+
};
|
|
130
|
+
check: {
|
|
78
131
|
/**
|
|
79
132
|
* If a function input is provided:
|
|
80
133
|
*
|
|
81
|
-
* Calls that function and checks that the function throw an error, comparing the error with
|
|
82
|
-
* given {@link ErrorMatchOptions}, if provided.
|
|
134
|
+
* Calls that function and checks that the function throw an error, comparing the error with
|
|
135
|
+
* the given {@link ErrorMatchOptions}, if provided.
|
|
83
136
|
*
|
|
84
137
|
* If a promise is provided:
|
|
85
138
|
*
|
|
86
|
-
* Awaits the promise and checks that the promise rejected with an error, comparing the
|
|
87
|
-
* with the given {@link ErrorMatchOptions}, if provided.
|
|
139
|
+
* Awaits the promise and checks that the promise rejected with an error, comparing the
|
|
140
|
+
* error with the given {@link ErrorMatchOptions}, if provided.
|
|
88
141
|
*
|
|
89
|
-
* This
|
|
90
|
-
* or async function inputs results in async. Otherwise, sync.)
|
|
142
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
143
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
91
144
|
*
|
|
92
145
|
* Performs no type guarding.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* import {check} from '@augment-vir/assert';
|
|
151
|
+
*
|
|
152
|
+
* check.throws(() => {
|
|
153
|
+
* throw new Error();
|
|
154
|
+
* }); // returns `true`
|
|
155
|
+
* check.throws(
|
|
156
|
+
* () => {
|
|
157
|
+
* throw new Error();
|
|
158
|
+
* },
|
|
159
|
+
* {matchMessage: 'hi'},
|
|
160
|
+
* ); // returns `false`
|
|
161
|
+
* await check.throws(Promise.reject()); // returns `true`
|
|
162
|
+
* check.throws(() => {}); // returns `false`
|
|
163
|
+
* ```
|
|
93
164
|
*/
|
|
94
|
-
throws: typeof throws;
|
|
95
|
-
};
|
|
96
|
-
checkOverrides: {
|
|
97
165
|
throws: typeof throwsCheck;
|
|
166
|
+
/**
|
|
167
|
+
* Checks that a value is an instance of the built-in `Error` class and compares it to the
|
|
168
|
+
* given {@link ErrorMatchOptions}, if provided.
|
|
169
|
+
*
|
|
170
|
+
* Type guards the input.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* import {check} from '@augment-vir/assert';
|
|
176
|
+
*
|
|
177
|
+
* check.isError(new Error()); // returns `true`
|
|
178
|
+
* check.isError(new Error(), {matchMessage: 'hi'}); // returns `false`
|
|
179
|
+
* check.isError({message: 'not an error'}); // returns `false`
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
isError: typeof autoGuardSymbol;
|
|
98
183
|
};
|
|
99
|
-
|
|
184
|
+
assertWrap: {
|
|
185
|
+
/**
|
|
186
|
+
* If a function input is provided:
|
|
187
|
+
*
|
|
188
|
+
* Calls that function and asserts that the function throw an error, comparing the error
|
|
189
|
+
* with the given {@link ErrorMatchOptions}, if provided. Returns the Error if the assertion
|
|
190
|
+
* passes.
|
|
191
|
+
*
|
|
192
|
+
* If a promise is provided:
|
|
193
|
+
*
|
|
194
|
+
* Awaits the promise and asserts that the promise rejected with an error, comparing the
|
|
195
|
+
* error with the given {@link ErrorMatchOptions}, if provided. Returns the Error if the
|
|
196
|
+
* assertion passes.
|
|
197
|
+
*
|
|
198
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
199
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
200
|
+
*
|
|
201
|
+
* Performs no type guarding.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
*
|
|
205
|
+
* ```ts
|
|
206
|
+
* import {assertWrap} from '@augment-vir/assert';
|
|
207
|
+
*
|
|
208
|
+
* assertWrap.throws(() => {
|
|
209
|
+
* throw new Error();
|
|
210
|
+
* }); // returns the thrown error
|
|
211
|
+
* assertWrap.throws(
|
|
212
|
+
* () => {
|
|
213
|
+
* throw new Error();
|
|
214
|
+
* },
|
|
215
|
+
* {matchMessage: 'hi'},
|
|
216
|
+
* ); // throws an error
|
|
217
|
+
* await assertWrap.throws(Promise.reject()); // returns the rejection
|
|
218
|
+
* assertWrap.throws(() => {}); // throws an error
|
|
219
|
+
* ```
|
|
220
|
+
*
|
|
221
|
+
* @returns The Error if the assertion passes.
|
|
222
|
+
* @throws {@link AssertionError} If the assertion fails.
|
|
223
|
+
*/
|
|
100
224
|
throws: typeof throwsAssertWrap;
|
|
225
|
+
/**
|
|
226
|
+
* Asserts that a value is an instance of the built-in `Error` class and compares it to the
|
|
227
|
+
* given {@link ErrorMatchOptions}, if provided.
|
|
228
|
+
*
|
|
229
|
+
* Type guards the input.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
*
|
|
233
|
+
* ```ts
|
|
234
|
+
* import {assertWrap} from '@augment-vir/assert';
|
|
235
|
+
*
|
|
236
|
+
* assertWrap.isError(new Error()); // returns the error instance
|
|
237
|
+
* assertWrap.isError(new Error(), {matchMessage: 'hi'}); // throws an error
|
|
238
|
+
* assertWrap.isError({message: 'not an error'}); // throws an error
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* @returns The value if the assertion passes.
|
|
242
|
+
* @throws {@link AssertionError} If the assertion fails.
|
|
243
|
+
*/
|
|
244
|
+
isError: typeof autoGuardSymbol;
|
|
101
245
|
};
|
|
102
|
-
|
|
246
|
+
checkWrap: {
|
|
247
|
+
/**
|
|
248
|
+
* If a function input is provided:
|
|
249
|
+
*
|
|
250
|
+
* Calls that function and checks that the function throw an error, comparing the error with
|
|
251
|
+
* the given {@link ErrorMatchOptions}, if provided. Returns the error if the check passes,
|
|
252
|
+
* otherwise `undefined`.
|
|
253
|
+
*
|
|
254
|
+
* If a promise is provided:
|
|
255
|
+
*
|
|
256
|
+
* Awaits the promise and checks that the promise rejected with an error, comparing the
|
|
257
|
+
* error with the given {@link ErrorMatchOptions}, if provided. Returns the error if the
|
|
258
|
+
* check passes, otherwise `undefined`.
|
|
259
|
+
*
|
|
260
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
261
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
262
|
+
*
|
|
263
|
+
* Performs no type guarding.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
*
|
|
267
|
+
* ```ts
|
|
268
|
+
* import {checkWrap} from '@augment-vir/assert';
|
|
269
|
+
*
|
|
270
|
+
* checkWrap.throws(() => {
|
|
271
|
+
* throw new Error();
|
|
272
|
+
* }); // returns the thrown error
|
|
273
|
+
* await checkWrap.throws(Promise.reject()); // returns the rejection
|
|
274
|
+
* checkWrap.throws(() => {}); // returns `undefined`
|
|
275
|
+
* ```
|
|
276
|
+
*
|
|
277
|
+
* @returns The Error if the check passes, otherwise `undefined`.
|
|
278
|
+
*/
|
|
103
279
|
throws: typeof throwsCheckWrap;
|
|
280
|
+
/**
|
|
281
|
+
* Checks that a value is an instance of the built-in `Error` class and compares it to the
|
|
282
|
+
* given {@link ErrorMatchOptions}, if provided. Returns the error if the check passes,
|
|
283
|
+
* otherwise `undefined`.
|
|
284
|
+
*
|
|
285
|
+
* Type guards the input.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
*
|
|
289
|
+
* ```ts
|
|
290
|
+
* import {checkWrap} from '@augment-vir/assert';
|
|
291
|
+
*
|
|
292
|
+
* checkWrap.isError(new Error()); // returns the Error
|
|
293
|
+
* checkWrap.isError(new Error(), {matchMessage: 'hi'}); // returns `undefined`
|
|
294
|
+
* checkWrap.isError({message: 'not an error'}); // returns `undefined`
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* @returns The Error if the check passes, otherwise `undefined`.
|
|
298
|
+
*/
|
|
299
|
+
isError: typeof autoGuardSymbol;
|
|
104
300
|
};
|
|
105
|
-
|
|
301
|
+
waitUntil: {
|
|
302
|
+
/**
|
|
303
|
+
* Repeatedly calls a callback until it throws an error, comparing the error with the given
|
|
304
|
+
* {@link ErrorMatchOptions}, if provided (as the first input). Once the callback throws an
|
|
305
|
+
* Error, that Error is returned. If the attempts time out, an error is thrown.
|
|
306
|
+
*
|
|
307
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
308
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
309
|
+
*
|
|
310
|
+
* Unlike the other `.throws` guards, `waitUntil.throws` does not allow a Promise input,
|
|
311
|
+
* only a callback input.
|
|
312
|
+
*
|
|
313
|
+
* Performs no type guarding.
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
*
|
|
317
|
+
* ```ts
|
|
318
|
+
* import {waitUntil} from '@augment-vir/assert';
|
|
319
|
+
*
|
|
320
|
+
* await waitUntil.throws(() => {
|
|
321
|
+
* throw new Error();
|
|
322
|
+
* }); // returns the thrown error
|
|
323
|
+
* await waitUntil.throws(Promise.reject()); // not allowed
|
|
324
|
+
* await waitUntil.throws(() => {}); // throws an error
|
|
325
|
+
* await waitUntil.throws({matchMessage: 'hi'}, () => {
|
|
326
|
+
* throw new Error('bye');
|
|
327
|
+
* }); // throws an error
|
|
328
|
+
* ```
|
|
329
|
+
*
|
|
330
|
+
* @returns The Error once it passes.
|
|
331
|
+
* @throws {@link AssertionError} On timeout.
|
|
332
|
+
*/
|
|
106
333
|
throws: typeof throwsWaitUntil;
|
|
334
|
+
/**
|
|
335
|
+
* Repeatedly calls a callback until is output is an instance of the built-in `Error` class
|
|
336
|
+
* and compares it to the given {@link ErrorMatchOptions}, if provided. Once the callback
|
|
337
|
+
* output passes, that Error is returned. If the attempts time out, an error is thrown.
|
|
338
|
+
*
|
|
339
|
+
* Type guards the input.
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
*
|
|
343
|
+
* ```ts
|
|
344
|
+
* import {waitUntil} from '@augment-vir/assert';
|
|
345
|
+
*
|
|
346
|
+
* await waitUntil.isError(new Error()); // returns the error instance
|
|
347
|
+
* await waitUntil.isError(new Error(), {matchMessage: 'hi'}); // throws an error
|
|
348
|
+
* await waitUntil.isError({message: 'not an error'}); // throws an error
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* @returns The callback output once it passes.
|
|
352
|
+
* @throws {@link AssertionError} On timeout.
|
|
353
|
+
*/
|
|
354
|
+
isError: typeof autoGuardSymbol;
|
|
107
355
|
};
|
|
108
356
|
};
|
|
109
357
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ensureError, extractErrorMessage, stringify, } from '@augment-vir/core';
|
|
2
2
|
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
+
import { autoGuardSymbol } from '../guard-types/guard-override.js';
|
|
3
4
|
import { createWaitUntil } from '../guard-types/wait-until-function.js';
|
|
4
5
|
var ThrowsCheckType;
|
|
5
6
|
(function (ThrowsCheckType) {
|
|
@@ -120,24 +121,21 @@ function throwsCheckWrap(callbackOrPromise, matchOptions, failureMessage) {
|
|
|
120
121
|
return internalThrowsCheck(ThrowsCheckType.CheckWrap, callbackOrPromise, matchOptions, failureMessage);
|
|
121
122
|
}
|
|
122
123
|
const internalWaitUntilThrows = createWaitUntil(isError);
|
|
123
|
-
function throwsWaitUntil(
|
|
124
|
-
const matchOptions = typeof
|
|
125
|
-
matchOptionsOrCallbackOrPromise instanceof Promise
|
|
124
|
+
function throwsWaitUntil(matchOptionsOrCallback, callbackOrOptions, optionsOrFailureMessage, failureMessage) {
|
|
125
|
+
const matchOptions = typeof matchOptionsOrCallback === 'function' || matchOptionsOrCallback instanceof Promise
|
|
126
126
|
? undefined
|
|
127
|
-
:
|
|
128
|
-
const
|
|
127
|
+
: matchOptionsOrCallback;
|
|
128
|
+
const callback = (matchOptions ? callbackOrOptions : matchOptionsOrCallback);
|
|
129
129
|
const actualFailureMessage = typeof optionsOrFailureMessage === 'object' ? failureMessage : optionsOrFailureMessage;
|
|
130
130
|
const waitUntilOptions = typeof optionsOrFailureMessage === 'object'
|
|
131
131
|
? optionsOrFailureMessage
|
|
132
|
-
:
|
|
132
|
+
: callbackOrOptions;
|
|
133
|
+
if (typeof callback !== 'function') {
|
|
134
|
+
throw new TypeError(`Callback is not a function, got '${stringify(callback)}'`);
|
|
135
|
+
}
|
|
133
136
|
return internalWaitUntilThrows(matchOptions, async () => {
|
|
134
137
|
try {
|
|
135
|
-
|
|
136
|
-
await callbackOrPromise();
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
await callbackOrPromise;
|
|
140
|
-
}
|
|
138
|
+
await callback();
|
|
141
139
|
return undefined;
|
|
142
140
|
}
|
|
143
141
|
catch (error) {
|
|
@@ -150,17 +148,231 @@ const assertions = {
|
|
|
150
148
|
isError: isError,
|
|
151
149
|
};
|
|
152
150
|
export const throwGuards = {
|
|
153
|
-
assertions,
|
|
154
|
-
|
|
151
|
+
assert: assertions,
|
|
152
|
+
check: {
|
|
153
|
+
/**
|
|
154
|
+
* If a function input is provided:
|
|
155
|
+
*
|
|
156
|
+
* Calls that function and checks that the function throw an error, comparing the error with
|
|
157
|
+
* the given {@link ErrorMatchOptions}, if provided.
|
|
158
|
+
*
|
|
159
|
+
* If a promise is provided:
|
|
160
|
+
*
|
|
161
|
+
* Awaits the promise and checks that the promise rejected with an error, comparing the
|
|
162
|
+
* error with the given {@link ErrorMatchOptions}, if provided.
|
|
163
|
+
*
|
|
164
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
165
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
166
|
+
*
|
|
167
|
+
* Performs no type guarding.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
*
|
|
171
|
+
* ```ts
|
|
172
|
+
* import {check} from '@augment-vir/assert';
|
|
173
|
+
*
|
|
174
|
+
* check.throws(() => {
|
|
175
|
+
* throw new Error();
|
|
176
|
+
* }); // returns `true`
|
|
177
|
+
* check.throws(
|
|
178
|
+
* () => {
|
|
179
|
+
* throw new Error();
|
|
180
|
+
* },
|
|
181
|
+
* {matchMessage: 'hi'},
|
|
182
|
+
* ); // returns `false`
|
|
183
|
+
* await check.throws(Promise.reject()); // returns `true`
|
|
184
|
+
* check.throws(() => {}); // returns `false`
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
155
187
|
throws: throwsCheck,
|
|
188
|
+
/**
|
|
189
|
+
* Checks that a value is an instance of the built-in `Error` class and compares it to the
|
|
190
|
+
* given {@link ErrorMatchOptions}, if provided.
|
|
191
|
+
*
|
|
192
|
+
* Type guards the input.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* import {check} from '@augment-vir/assert';
|
|
198
|
+
*
|
|
199
|
+
* check.isError(new Error()); // returns `true`
|
|
200
|
+
* check.isError(new Error(), {matchMessage: 'hi'}); // returns `false`
|
|
201
|
+
* check.isError({message: 'not an error'}); // returns `false`
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
isError: autoGuardSymbol,
|
|
156
205
|
},
|
|
157
|
-
|
|
206
|
+
assertWrap: {
|
|
207
|
+
/**
|
|
208
|
+
* If a function input is provided:
|
|
209
|
+
*
|
|
210
|
+
* Calls that function and asserts that the function throw an error, comparing the error
|
|
211
|
+
* with the given {@link ErrorMatchOptions}, if provided. Returns the Error if the assertion
|
|
212
|
+
* passes.
|
|
213
|
+
*
|
|
214
|
+
* If a promise is provided:
|
|
215
|
+
*
|
|
216
|
+
* Awaits the promise and asserts that the promise rejected with an error, comparing the
|
|
217
|
+
* error with the given {@link ErrorMatchOptions}, if provided. Returns the Error if the
|
|
218
|
+
* assertion passes.
|
|
219
|
+
*
|
|
220
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
221
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
222
|
+
*
|
|
223
|
+
* Performs no type guarding.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
*
|
|
227
|
+
* ```ts
|
|
228
|
+
* import {assertWrap} from '@augment-vir/assert';
|
|
229
|
+
*
|
|
230
|
+
* assertWrap.throws(() => {
|
|
231
|
+
* throw new Error();
|
|
232
|
+
* }); // returns the thrown error
|
|
233
|
+
* assertWrap.throws(
|
|
234
|
+
* () => {
|
|
235
|
+
* throw new Error();
|
|
236
|
+
* },
|
|
237
|
+
* {matchMessage: 'hi'},
|
|
238
|
+
* ); // throws an error
|
|
239
|
+
* await assertWrap.throws(Promise.reject()); // returns the rejection
|
|
240
|
+
* assertWrap.throws(() => {}); // throws an error
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* @returns The Error if the assertion passes.
|
|
244
|
+
* @throws {@link AssertionError} If the assertion fails.
|
|
245
|
+
*/
|
|
158
246
|
throws: throwsAssertWrap,
|
|
247
|
+
/**
|
|
248
|
+
* Asserts that a value is an instance of the built-in `Error` class and compares it to the
|
|
249
|
+
* given {@link ErrorMatchOptions}, if provided.
|
|
250
|
+
*
|
|
251
|
+
* Type guards the input.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* import {assertWrap} from '@augment-vir/assert';
|
|
257
|
+
*
|
|
258
|
+
* assertWrap.isError(new Error()); // returns the error instance
|
|
259
|
+
* assertWrap.isError(new Error(), {matchMessage: 'hi'}); // throws an error
|
|
260
|
+
* assertWrap.isError({message: 'not an error'}); // throws an error
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* @returns The value if the assertion passes.
|
|
264
|
+
* @throws {@link AssertionError} If the assertion fails.
|
|
265
|
+
*/
|
|
266
|
+
isError: autoGuardSymbol,
|
|
159
267
|
},
|
|
160
|
-
|
|
268
|
+
checkWrap: {
|
|
269
|
+
/**
|
|
270
|
+
* If a function input is provided:
|
|
271
|
+
*
|
|
272
|
+
* Calls that function and checks that the function throw an error, comparing the error with
|
|
273
|
+
* the given {@link ErrorMatchOptions}, if provided. Returns the error if the check passes,
|
|
274
|
+
* otherwise `undefined`.
|
|
275
|
+
*
|
|
276
|
+
* If a promise is provided:
|
|
277
|
+
*
|
|
278
|
+
* Awaits the promise and checks that the promise rejected with an error, comparing the
|
|
279
|
+
* error with the given {@link ErrorMatchOptions}, if provided. Returns the error if the
|
|
280
|
+
* check passes, otherwise `undefined`.
|
|
281
|
+
*
|
|
282
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
283
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
284
|
+
*
|
|
285
|
+
* Performs no type guarding.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
*
|
|
289
|
+
* ```ts
|
|
290
|
+
* import {checkWrap} from '@augment-vir/assert';
|
|
291
|
+
*
|
|
292
|
+
* checkWrap.throws(() => {
|
|
293
|
+
* throw new Error();
|
|
294
|
+
* }); // returns the thrown error
|
|
295
|
+
* await checkWrap.throws(Promise.reject()); // returns the rejection
|
|
296
|
+
* checkWrap.throws(() => {}); // returns `undefined`
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* @returns The Error if the check passes, otherwise `undefined`.
|
|
300
|
+
*/
|
|
161
301
|
throws: throwsCheckWrap,
|
|
302
|
+
/**
|
|
303
|
+
* Checks that a value is an instance of the built-in `Error` class and compares it to the
|
|
304
|
+
* given {@link ErrorMatchOptions}, if provided. Returns the error if the check passes,
|
|
305
|
+
* otherwise `undefined`.
|
|
306
|
+
*
|
|
307
|
+
* Type guards the input.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
*
|
|
311
|
+
* ```ts
|
|
312
|
+
* import {checkWrap} from '@augment-vir/assert';
|
|
313
|
+
*
|
|
314
|
+
* checkWrap.isError(new Error()); // returns the Error
|
|
315
|
+
* checkWrap.isError(new Error(), {matchMessage: 'hi'}); // returns `undefined`
|
|
316
|
+
* checkWrap.isError({message: 'not an error'}); // returns `undefined`
|
|
317
|
+
* ```
|
|
318
|
+
*
|
|
319
|
+
* @returns The Error if the check passes, otherwise `undefined`.
|
|
320
|
+
*/
|
|
321
|
+
isError: autoGuardSymbol,
|
|
162
322
|
},
|
|
163
|
-
|
|
323
|
+
waitUntil: {
|
|
324
|
+
/**
|
|
325
|
+
* Repeatedly calls a callback until it throws an error, comparing the error with the given
|
|
326
|
+
* {@link ErrorMatchOptions}, if provided (as the first input). Once the callback throws an
|
|
327
|
+
* Error, that Error is returned. If the attempts time out, an error is thrown.
|
|
328
|
+
*
|
|
329
|
+
* This assertion will automatically type itself as async vs async based on the input. (A
|
|
330
|
+
* promise or async function inputs results in async. Otherwise, sync.)
|
|
331
|
+
*
|
|
332
|
+
* Unlike the other `.throws` guards, `waitUntil.throws` does not allow a Promise input,
|
|
333
|
+
* only a callback input.
|
|
334
|
+
*
|
|
335
|
+
* Performs no type guarding.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
*
|
|
339
|
+
* ```ts
|
|
340
|
+
* import {waitUntil} from '@augment-vir/assert';
|
|
341
|
+
*
|
|
342
|
+
* await waitUntil.throws(() => {
|
|
343
|
+
* throw new Error();
|
|
344
|
+
* }); // returns the thrown error
|
|
345
|
+
* await waitUntil.throws(Promise.reject()); // not allowed
|
|
346
|
+
* await waitUntil.throws(() => {}); // throws an error
|
|
347
|
+
* await waitUntil.throws({matchMessage: 'hi'}, () => {
|
|
348
|
+
* throw new Error('bye');
|
|
349
|
+
* }); // throws an error
|
|
350
|
+
* ```
|
|
351
|
+
*
|
|
352
|
+
* @returns The Error once it passes.
|
|
353
|
+
* @throws {@link AssertionError} On timeout.
|
|
354
|
+
*/
|
|
164
355
|
throws: throwsWaitUntil,
|
|
356
|
+
/**
|
|
357
|
+
* Repeatedly calls a callback until is output is an instance of the built-in `Error` class
|
|
358
|
+
* and compares it to the given {@link ErrorMatchOptions}, if provided. Once the callback
|
|
359
|
+
* output passes, that Error is returned. If the attempts time out, an error is thrown.
|
|
360
|
+
*
|
|
361
|
+
* Type guards the input.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* import {waitUntil} from '@augment-vir/assert';
|
|
367
|
+
*
|
|
368
|
+
* await waitUntil.isError(new Error()); // returns the error instance
|
|
369
|
+
* await waitUntil.isError(new Error(), {matchMessage: 'hi'}); // throws an error
|
|
370
|
+
* await waitUntil.isError({message: 'not an error'}); // throws an error
|
|
371
|
+
* ```
|
|
372
|
+
*
|
|
373
|
+
* @returns The callback output once it passes.
|
|
374
|
+
* @throws {@link AssertionError} On timeout.
|
|
375
|
+
*/
|
|
376
|
+
isError: autoGuardSymbol,
|
|
165
377
|
},
|
|
166
378
|
};
|