assertron 11.2.0 → 11.2.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/cjs/assert-order/AssertOrder.d.ts.map +1 -1
- package/cjs/assert-order/AssertOrder.js +3 -1
- package/cjs/assert-order/AssertOrder.js.map +1 -1
- package/cjs/assert-order/InvalidOrder.d.ts.map +1 -1
- package/cjs/assert-order/InvalidOrder.js +4 -2
- package/cjs/assert-order/InvalidOrder.js.map +1 -1
- package/cjs/assert-order/StateMachine.d.ts.map +1 -1
- package/cjs/assert-order/StateMachine.js +17 -8
- package/cjs/assert-order/StateMachine.js.map +1 -1
- package/cjs/assert-order/types.d.ts.map +1 -1
- package/cjs/assertron/assertron.d.ts.map +1 -1
- package/cjs/assertron/assertron.js.map +1 -1
- package/cjs/assertron/falsy.d.ts.map +1 -1
- package/cjs/assertron/falsy.js.map +1 -1
- package/cjs/assertron/isInstanceof.d.ts.map +1 -1
- package/cjs/assertron/isInstanceof.js.map +1 -1
- package/cjs/assertron/pathEqual.js.map +1 -1
- package/cjs/assertron/rejects.d.ts.map +1 -1
- package/cjs/assertron/rejects.js +3 -1
- package/cjs/assertron/rejects.js.map +1 -1
- package/cjs/assertron/repeat.d.ts.map +1 -1
- package/cjs/assertron/repeat.js +3 -1
- package/cjs/assertron/repeat.js.map +1 -1
- package/cjs/assertron/resolves.d.ts.map +1 -1
- package/cjs/assertron/resolves.js +3 -1
- package/cjs/assertron/resolves.js.map +1 -1
- package/cjs/assertron/satisfies.d.ts.map +1 -1
- package/cjs/assertron/satisfies.js.map +1 -1
- package/cjs/assertron/throws.d.ts.map +1 -1
- package/cjs/assertron/throws.js +7 -3
- package/cjs/assertron/throws.js.map +1 -1
- package/cjs/assertron/truthy.d.ts.map +1 -1
- package/cjs/assertron/truthy.js.map +1 -1
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js.map +1 -1
- package/cjs/package.json +1 -1
- package/cjs/types.d.ts.map +1 -1
- package/cjs/utils/index.js.map +1 -1
- package/cjs/utils/isErrorConstructor.js.map +1 -1
- package/cjs/utils/notEqualMessage.js.map +1 -1
- package/cjs/utils/notSatisfiedMessage.d.ts.map +1 -1
- package/cjs/utils/notSatisfiedMessage.js +8 -3
- package/cjs/utils/notSatisfiedMessage.js.map +1 -1
- package/cjs/utils/notThrownMessage.js.map +1 -1
- package/cjs/utils/promiseMessages.d.ts.map +1 -1
- package/cjs/utils/promiseMessages.js +8 -4
- package/cjs/utils/promiseMessages.js.map +1 -1
- package/cjs/utils/unexpectedErrorMessage.d.ts.map +1 -1
- package/cjs/utils/unexpectedErrorMessage.js +1 -2
- package/cjs/utils/unexpectedErrorMessage.js.map +1 -1
- package/esm/assert-order/AssertOrder.d.ts.map +1 -1
- package/esm/assert-order/AssertOrder.js +3 -1
- package/esm/assert-order/AssertOrder.js.map +1 -1
- package/esm/assert-order/InvalidOrder.d.ts.map +1 -1
- package/esm/assert-order/InvalidOrder.js +4 -2
- package/esm/assert-order/InvalidOrder.js.map +1 -1
- package/esm/assert-order/StateMachine.d.ts.map +1 -1
- package/esm/assert-order/StateMachine.js +17 -8
- package/esm/assert-order/StateMachine.js.map +1 -1
- package/esm/assert-order/types.d.ts.map +1 -1
- package/esm/assertron/assertron.d.ts.map +1 -1
- package/esm/assertron/assertron.js.map +1 -1
- package/esm/assertron/falsy.d.ts.map +1 -1
- package/esm/assertron/falsy.js.map +1 -1
- package/esm/assertron/isInstanceof.d.ts.map +1 -1
- package/esm/assertron/isInstanceof.js.map +1 -1
- package/esm/assertron/pathEqual.js.map +1 -1
- package/esm/assertron/rejects.d.ts.map +1 -1
- package/esm/assertron/rejects.js +3 -1
- package/esm/assertron/rejects.js.map +1 -1
- package/esm/assertron/repeat.d.ts.map +1 -1
- package/esm/assertron/repeat.js +3 -1
- package/esm/assertron/repeat.js.map +1 -1
- package/esm/assertron/resolves.d.ts.map +1 -1
- package/esm/assertron/resolves.js +3 -1
- package/esm/assertron/resolves.js.map +1 -1
- package/esm/assertron/satisfies.d.ts.map +1 -1
- package/esm/assertron/satisfies.js.map +1 -1
- package/esm/assertron/throws.d.ts.map +1 -1
- package/esm/assertron/throws.js +7 -3
- package/esm/assertron/throws.js.map +1 -1
- package/esm/assertron/truthy.d.ts.map +1 -1
- package/esm/assertron/truthy.js.map +1 -1
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js.map +1 -1
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/isErrorConstructor.js.map +1 -1
- package/esm/utils/notEqualMessage.js.map +1 -1
- package/esm/utils/notSatisfiedMessage.d.ts.map +1 -1
- package/esm/utils/notSatisfiedMessage.js +8 -3
- package/esm/utils/notSatisfiedMessage.js.map +1 -1
- package/esm/utils/notThrownMessage.js.map +1 -1
- package/esm/utils/promiseMessages.d.ts.map +1 -1
- package/esm/utils/promiseMessages.js +8 -4
- package/esm/utils/promiseMessages.js.map +1 -1
- package/esm/utils/unexpectedErrorMessage.d.ts.map +1 -1
- package/esm/utils/unexpectedErrorMessage.js +1 -2
- package/esm/utils/unexpectedErrorMessage.js.map +1 -1
- package/package.json +1 -1
- package/ts/assert-order/AssertOrder.ts +138 -143
- package/ts/assert-order/InvalidOrder.ts +22 -18
- package/ts/assert-order/StateMachine.ts +95 -88
- package/ts/assert-order/index.ts +0 -1
- package/ts/assert-order/internalInterfaces.ts +3 -3
- package/ts/assert-order/types.ts +5 -5
- package/ts/assertron/assertron.ts +38 -31
- package/ts/assertron/falsy.ts +1 -2
- package/ts/assertron/isInstanceof.ts +6 -3
- package/ts/assertron/pathEqual.ts +2 -2
- package/ts/assertron/rejects.ts +6 -4
- package/ts/assertron/repeat.ts +27 -21
- package/ts/assertron/resolves.ts +6 -4
- package/ts/assertron/satisfies.ts +27 -20
- package/ts/assertron/throws.ts +75 -54
- package/ts/assertron/truthy.ts +1 -4
- package/ts/errors.ts +7 -7
- package/ts/testUtils.ts +51 -54
- package/ts/types.ts +0 -1
- package/ts/utils/index.ts +1 -1
- package/ts/utils/isErrorConstructor.ts +1 -1
- package/ts/utils/notEqualMessage.ts +1 -1
- package/ts/utils/notSatisfiedMessage.ts +17 -13
- package/ts/utils/notThrownMessage.ts +1 -1
- package/ts/utils/promiseMessages.ts +12 -5
- package/ts/utils/unexpectedErrorMessage.ts +3 -2
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { AnyConstructor } from 'type-plus'
|
|
2
2
|
import { AssertionError } from '../errors.js'
|
|
3
3
|
|
|
4
|
-
export function isInstanceof<T extends AnyConstructor>(
|
|
5
|
-
|
|
4
|
+
export function isInstanceof<T extends AnyConstructor>(
|
|
5
|
+
value: unknown,
|
|
6
|
+
type: T
|
|
7
|
+
): asserts value is InstanceType<T> {
|
|
8
|
+
if (value instanceof type) return
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
throw new AssertionError(`Expected ${value} to be an instance of ${type.name}`, { ssf: isInstanceof })
|
|
8
11
|
}
|
|
@@ -3,6 +3,6 @@ import { AssertionError } from '../errors.js'
|
|
|
3
3
|
import { notEqualMessage } from '../utils/index.js'
|
|
4
4
|
|
|
5
5
|
export function pathEqual(actual: string, expected: string) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
if (!isPathEqual(actual, expected))
|
|
7
|
+
throw new AssertionError(notEqualMessage(actual, expected), { ssf: pathEqual })
|
|
8
8
|
}
|
package/ts/assertron/rejects.ts
CHANGED
|
@@ -2,8 +2,10 @@ import { AssertionError } from '../errors.js'
|
|
|
2
2
|
import { notRejectedMessage } from '../utils/index.js'
|
|
3
3
|
|
|
4
4
|
export function rejects<R = any>(promise: Promise<any>): Promise<R> {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
return promise.then(
|
|
6
|
+
value => {
|
|
7
|
+
throw new AssertionError(notRejectedMessage(value), { ssf: rejects })
|
|
8
|
+
},
|
|
9
|
+
err => err
|
|
10
|
+
)
|
|
9
11
|
}
|
package/ts/assertron/repeat.ts
CHANGED
|
@@ -5,28 +5,34 @@ import { FailOnOccurrence } from '../errors.js'
|
|
|
5
5
|
* Repeat the specified function n times and return the last result.
|
|
6
6
|
* If the result is a promise, it will run the function sequentially.
|
|
7
7
|
*/
|
|
8
|
-
export function repeat<R>(
|
|
9
|
-
|
|
8
|
+
export function repeat<R>(
|
|
9
|
+
fn: () => R | (() => Promise<R>),
|
|
10
|
+
times: number
|
|
11
|
+
): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R {
|
|
12
|
+
return repeatRecur(fn, 1, times)
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
function repeatRecur<R>(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
function repeatRecur<R>(
|
|
16
|
+
fn: () => R | (() => Promise<R>),
|
|
17
|
+
i: number,
|
|
18
|
+
times: number
|
|
19
|
+
): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R {
|
|
20
|
+
try {
|
|
21
|
+
const result = fn()
|
|
22
|
+
if (i === times) return result as any
|
|
16
23
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
if (isPromise(result)) {
|
|
25
|
+
return result.then(
|
|
26
|
+
() => repeatRecur(fn, i + 1, times),
|
|
27
|
+
e => {
|
|
28
|
+
throw new FailOnOccurrence(i, e, { ssf: repeat })
|
|
29
|
+
}
|
|
30
|
+
) as any
|
|
31
|
+
} else {
|
|
32
|
+
return repeatRecur(fn, i + 1, times)
|
|
33
|
+
}
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e instanceof FailOnOccurrence) throw e
|
|
36
|
+
throw new FailOnOccurrence(i, e, { ssf: repeat })
|
|
37
|
+
}
|
|
31
38
|
}
|
|
32
|
-
|
package/ts/assertron/resolves.ts
CHANGED
|
@@ -2,8 +2,10 @@ import { AssertionError } from '../errors.js'
|
|
|
2
2
|
import { notResolvedMessage } from '../utils/index.js'
|
|
3
3
|
|
|
4
4
|
export function resolves(promise: Promise<any>) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
return promise.then(
|
|
6
|
+
() => {},
|
|
7
|
+
error => {
|
|
8
|
+
throw new AssertionError(notResolvedMessage(error), { ssf: resolves })
|
|
9
|
+
}
|
|
10
|
+
)
|
|
9
11
|
}
|
|
@@ -3,30 +3,37 @@ import type { If, IsExtend, NonComposableTypes } from 'type-plus'
|
|
|
3
3
|
import { AssertionError } from '../errors.js'
|
|
4
4
|
import { notSatisfiedMessage } from '../utils/index.js'
|
|
5
5
|
|
|
6
|
-
export type SatisfyExpectation<T> =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
6
|
+
export type SatisfyExpectation<T> =
|
|
7
|
+
| If<
|
|
8
|
+
IsExtend<T, string>,
|
|
9
|
+
T | RegExp,
|
|
10
|
+
If<
|
|
11
|
+
IsExtend<T, NonComposableTypes>,
|
|
12
|
+
T,
|
|
13
|
+
If<
|
|
14
|
+
IsExtend<T, Array<any>>,
|
|
15
|
+
T extends Array<infer E> ? Array<SatisfyExpectation<E>> : never,
|
|
16
|
+
{
|
|
17
|
+
[P in keyof T]?: T[P] extends string
|
|
18
|
+
? SatisfyExpectation<T[P]> | RegExp
|
|
19
|
+
: SatisfyExpectation<T[P]>
|
|
20
|
+
}
|
|
21
|
+
>
|
|
22
|
+
>
|
|
23
|
+
>
|
|
24
|
+
| ((v: T) => boolean)
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Check if `actual` satisfies criteria in `expected`.
|
|
24
28
|
* @param expected All properties can be a value which will be compared to the same property in `actual`,
|
|
25
29
|
* RegExp, or a predicate function that will be used to check against the property.
|
|
26
30
|
*/
|
|
27
|
-
export function satisfies<Actual, Expected extends Actual>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
export function satisfies<Actual, Expected extends Actual>(
|
|
32
|
+
actual: Actual,
|
|
33
|
+
expected: SatisfyExpectation<Expected>
|
|
34
|
+
) {
|
|
35
|
+
const diff = createSatisfier(expected as any).exec(actual)
|
|
36
|
+
if (diff) {
|
|
37
|
+
throw new AssertionError(notSatisfiedMessage(diff), { ssf: satisfies })
|
|
38
|
+
}
|
|
32
39
|
}
|
package/ts/assertron/throws.ts
CHANGED
|
@@ -1,67 +1,88 @@
|
|
|
1
1
|
import isPromise from 'is-promise'
|
|
2
2
|
import { AssertionError } from '../errors.js'
|
|
3
3
|
import type { ErrorConstructor, ErrorValidator } from '../types.js'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
isErrorConstructor,
|
|
6
|
+
notRejectedMessage,
|
|
7
|
+
notThrownMessage,
|
|
8
|
+
unexpectedErrorMessage
|
|
9
|
+
} from '../utils/index.js'
|
|
5
10
|
|
|
6
|
-
export function throws<E extends Error>(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export function throws
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
export function throws<E extends Error>(
|
|
12
|
+
value: PromiseLike<any>,
|
|
13
|
+
error?: ErrorValidator | ErrorConstructor<E>
|
|
14
|
+
): Promise<E>
|
|
15
|
+
export function throws<E extends Error>(
|
|
16
|
+
value: (...args: any[]) => never,
|
|
17
|
+
error?: ErrorValidator | ErrorConstructor<E>
|
|
18
|
+
): E
|
|
19
|
+
export function throws<E extends Error, R = any>(
|
|
20
|
+
value: (...args: any[]) => R,
|
|
21
|
+
error?: ErrorValidator | ErrorConstructor<E>
|
|
22
|
+
): R extends Promise<any> ? Promise<E> : E
|
|
23
|
+
export function throws<T, R = any>(
|
|
24
|
+
value: (...args: any[]) => R,
|
|
25
|
+
validator?: ErrorValidator
|
|
26
|
+
): R extends Promise<any> ? Promise<T> : T
|
|
27
|
+
export function throws(
|
|
28
|
+
value: PromiseLike<any> | (() => any | PromiseLike<any>),
|
|
29
|
+
validator?: ErrorValidator | ErrorConstructor<any>
|
|
30
|
+
): any {
|
|
31
|
+
if (typeof value !== 'function' && !isPromise(value)) {
|
|
32
|
+
throw new AssertionError('`assertron.throws()` must be called with a function or promise.', {
|
|
33
|
+
ssf: throws
|
|
34
|
+
})
|
|
35
|
+
}
|
|
17
36
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
if (!isPromise(value)) {
|
|
38
|
+
const { threw, err, result } = tryCatch(value)
|
|
39
|
+
if (threw) {
|
|
40
|
+
if (failValidation(validator, err)) {
|
|
41
|
+
throw new AssertionError(unexpectedErrorMessage(err, validator), { ssf: throws })
|
|
42
|
+
}
|
|
43
|
+
return err
|
|
44
|
+
}
|
|
45
|
+
value = result
|
|
46
|
+
}
|
|
28
47
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
if (isPromise(value)) {
|
|
49
|
+
return value.then(
|
|
50
|
+
value => {
|
|
51
|
+
throw new AssertionError(notRejectedMessage(value), { ssf: throws })
|
|
52
|
+
},
|
|
53
|
+
err => {
|
|
54
|
+
if (failValidation(validator, err)) {
|
|
55
|
+
throw new AssertionError(unexpectedErrorMessage(err, validator), { ssf: throws, cause: err })
|
|
56
|
+
}
|
|
57
|
+
return err
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
}
|
|
40
61
|
|
|
41
|
-
|
|
62
|
+
throw new AssertionError(notThrownMessage(value), { ssf: throws })
|
|
42
63
|
}
|
|
43
64
|
|
|
44
65
|
function tryCatch(fn: () => any) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return { threw, err, result }
|
|
66
|
+
let result
|
|
67
|
+
let err
|
|
68
|
+
let threw = false
|
|
69
|
+
try {
|
|
70
|
+
result = fn()
|
|
71
|
+
} catch (e) {
|
|
72
|
+
err = e
|
|
73
|
+
threw = true
|
|
74
|
+
}
|
|
75
|
+
return { threw, err, result }
|
|
56
76
|
}
|
|
57
|
-
function failValidation(
|
|
58
|
-
|
|
77
|
+
function failValidation(
|
|
78
|
+
validator: ErrorValidator | ErrorConstructor<any> | undefined,
|
|
79
|
+
error: any
|
|
80
|
+
): validator is ErrorValidator | ErrorConstructor<any> {
|
|
81
|
+
if (!validator) return false
|
|
59
82
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
83
|
+
if (isErrorConstructor(validator)) {
|
|
84
|
+
return !(error instanceof validator)
|
|
85
|
+
} else {
|
|
86
|
+
return !validator(error)
|
|
87
|
+
}
|
|
66
88
|
}
|
|
67
|
-
|
package/ts/assertron/truthy.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { AssertionError } from '../errors.js'
|
|
2
2
|
|
|
3
3
|
export function truthy(value: unknown) {
|
|
4
|
-
|
|
5
|
-
`Expected value to be truthy, but received ${value}`,
|
|
6
|
-
{ ssf: truthy }
|
|
7
|
-
)
|
|
4
|
+
if (!value) throw new AssertionError(`Expected value to be truthy, but received ${value}`, { ssf: truthy })
|
|
8
5
|
}
|
package/ts/errors.ts
CHANGED
|
@@ -6,17 +6,17 @@ import { failOnOccurrence } from './utils/index.js'
|
|
|
6
6
|
* A base error for all assertion errors
|
|
7
7
|
*/
|
|
8
8
|
export class AssertionError extends ModuleError {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
constructor(message: string, options: AssertionError.Options) {
|
|
10
|
+
super('assertron', message, options)
|
|
11
|
+
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export namespace AssertionError {
|
|
15
|
-
|
|
15
|
+
export type Options = RequiredPick<ModuleError.Options, 'ssf'>
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export class FailOnOccurrence extends AssertionError {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
constructor(public occurrence: number, public error: any, options: AssertionError.Options) {
|
|
20
|
+
super(failOnOccurrence(occurrence, error), options)
|
|
21
|
+
}
|
|
22
22
|
}
|
package/ts/testUtils.ts
CHANGED
|
@@ -7,78 +7,75 @@ import type { ErrorConstructor } from './types.js'
|
|
|
7
7
|
export type AnyFunction = (...args: any[]) => any
|
|
8
8
|
|
|
9
9
|
export function runAsync(fn: AnyFunction) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
})
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
setTimeout(() => {
|
|
12
|
+
try {
|
|
13
|
+
resolve(fn())
|
|
14
|
+
} catch (err) {
|
|
15
|
+
reject(err)
|
|
16
|
+
}
|
|
17
|
+
}, 1)
|
|
18
|
+
})
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
export function runSequentialAsync(...fns: AnyFunction[]) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
setTimeout(() => {
|
|
24
|
+
try {
|
|
25
|
+
let i = 0
|
|
26
|
+
for (; i < fns.length - 1; i++) {
|
|
27
|
+
fns[i]()
|
|
28
|
+
}
|
|
29
|
+
resolve(fns[i]())
|
|
30
|
+
} catch (err) {
|
|
31
|
+
reject(err)
|
|
32
|
+
}
|
|
33
|
+
}, 1)
|
|
34
|
+
})
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
export function runParallelAsync(...fns: AnyFunction[]) {
|
|
40
|
-
|
|
38
|
+
return Promise.all(fns.map(fn => runAsync(fn)))
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
|
|
44
41
|
export function assertIsPromise(promiseError: Promise<any>) {
|
|
45
|
-
|
|
42
|
+
t(isPromise(promiseError))
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
export function assertIsError(err: Error) {
|
|
49
|
-
|
|
46
|
+
t(isError(err))
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
export function assertThrows<E extends Error>(fn: AnyFunction, ErrorType?: ErrorConstructor<E>): E {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
throw new Error(`${fn} does not throw as expected`)
|
|
50
|
+
try {
|
|
51
|
+
fn()
|
|
52
|
+
} catch (e: any) {
|
|
53
|
+
if (ErrorType && !(e instanceof ErrorType)) {
|
|
54
|
+
throw new Error(`thrown error ${e} is not of expected type ${ErrorType}`)
|
|
55
|
+
}
|
|
56
|
+
return e
|
|
57
|
+
}
|
|
58
|
+
throw new Error(`${fn} does not throw as expected`)
|
|
63
59
|
}
|
|
64
60
|
|
|
65
|
-
export async function assertAsyncThrows<E extends Error>(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
61
|
+
export async function assertAsyncThrows<E extends Error>(
|
|
62
|
+
fn: AnyFunction,
|
|
63
|
+
ErrorType?: ErrorConstructor<E>
|
|
64
|
+
): Promise<E> {
|
|
65
|
+
try {
|
|
66
|
+
await fn()
|
|
67
|
+
} catch (e: any) {
|
|
68
|
+
// console.log('catch clause', e)
|
|
69
|
+
if (ErrorType && !(e instanceof ErrorType)) {
|
|
70
|
+
throw new Error(`thrown error ${e} is not of expected type ${ErrorType}`)
|
|
71
|
+
}
|
|
72
|
+
// console.log('returning e')
|
|
73
|
+
return e
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`${fn} does not throw as expected`)
|
|
78
76
|
}
|
|
79
77
|
|
|
80
|
-
|
|
81
78
|
export function noStackTraceFor(filename: string, err: Error) {
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
if (err.stack && RegExp(escapeRegExp(filename)).test(err.stack))
|
|
80
|
+
throw new Error(`contains internal stack: \n${err.stack}`)
|
|
84
81
|
}
|
package/ts/types.ts
CHANGED
package/ts/utils/index.ts
CHANGED
|
@@ -8,5 +8,5 @@ export * from './promiseMessages.js'
|
|
|
8
8
|
export * from './unexpectedErrorMessage.js'
|
|
9
9
|
|
|
10
10
|
export function failOnOccurrence(occurrence: number, error: any) {
|
|
11
|
-
|
|
11
|
+
return `Failed on ${occurrence} occurrence: ${tersify(error, { maxLength: Infinity })}`
|
|
12
12
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ErrorConstructor } from '../types.js'
|
|
2
2
|
|
|
3
3
|
export function isErrorConstructor(validator: any): validator is ErrorConstructor<any> {
|
|
4
|
-
|
|
4
|
+
return Object.prototype.isPrototypeOf.call(Error.prototype, validator.prototype)
|
|
5
5
|
}
|
|
@@ -2,26 +2,30 @@ import type { Diff } from 'satisfier'
|
|
|
2
2
|
import { tersify } from 'tersify'
|
|
3
3
|
|
|
4
4
|
export function notSatisfiedMessage(entries: Diff[]) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
return entries
|
|
6
|
+
.map(e => {
|
|
7
|
+
const path = formatPath(e.path)
|
|
8
|
+
return `Expect ${path} to satisfy ${formatValue(e.expected)}, but received ${formatValue(e.actual)}`
|
|
9
|
+
})
|
|
10
|
+
.join('\n')
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
function formatPath(entries: (string | number)[]) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
if (entries.length === 0) return 'actual'
|
|
15
|
+
const nodes = [...entries]
|
|
16
|
+
if (isIndice(nodes[0])) {
|
|
17
|
+
nodes.unshift('actual')
|
|
18
|
+
}
|
|
19
|
+
return nodes
|
|
20
|
+
.map(n => (isIndice(n) ? `[${n}]` : '.' + n))
|
|
21
|
+
.join('')
|
|
22
|
+
.slice(1)
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
function isIndice(node: string | number): node is number {
|
|
22
|
-
|
|
26
|
+
return typeof node === 'number'
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
function formatValue(value: any) {
|
|
26
|
-
|
|
30
|
+
return tersify(value)
|
|
27
31
|
}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { tersify } from 'tersify'
|
|
2
2
|
|
|
3
3
|
export function notRejectedMessage(value: any) {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
return (
|
|
5
|
+
`Expected promise to reject, but it resolves with ` +
|
|
6
|
+
(typeof value === 'string' ? `'${value}'` : tersify(value, { maxLength: Infinity }))
|
|
7
|
+
)
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export function notResolvedMessage(error: any) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
return (
|
|
12
|
+
`Expected promise to resolve, but it rejects with ` +
|
|
13
|
+
(typeof error === 'string'
|
|
14
|
+
? `'${error}'`
|
|
15
|
+
: error instanceof Error
|
|
16
|
+
? error
|
|
17
|
+
: tersify(error, { maxLength: Infinity }))
|
|
18
|
+
)
|
|
11
19
|
}
|
|
12
|
-
|
|
@@ -3,6 +3,7 @@ import type { ErrorConstructor, ErrorValidator } from '../types.js'
|
|
|
3
3
|
import { isErrorConstructor } from './isErrorConstructor.js'
|
|
4
4
|
|
|
5
5
|
export function unexpectedErrorMessage(actual: any, expected: ErrorValidator | ErrorConstructor<any>) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
return `Unexpected error. Expecting '${
|
|
7
|
+
isErrorConstructor(expected) ? expected.name : tersify(expected)
|
|
8
|
+
}' but received ${tersify(actual)}`
|
|
8
9
|
}
|