assertron 11.1.4 → 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 +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 +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 +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 +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 +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 +3 -3
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js.map +1 -1
- package/cjs/index.d.ts +2 -1
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +15 -1
- package/cjs/index.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.d.ts +1 -1
- package/cjs/utils/isErrorConstructor.d.ts.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 +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 +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 +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 +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 +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 +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 +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 +3 -3
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js.map +1 -1
- package/esm/index.d.ts +2 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -1
- package/esm/index.js.map +1 -1
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/isErrorConstructor.d.ts +1 -1
- package/esm/utils/isErrorConstructor.d.ts.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 +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 +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 +3 -3
- package/ts/assert-order/AssertOrder.ts +138 -143
- package/ts/assert-order/InvalidOrder.ts +23 -19
- package/ts/assert-order/StateMachine.ts +96 -89
- 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 +39 -32
- 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 +28 -21
- package/ts/assertron/throws.ts +76 -55
- package/ts/assertron/truthy.ts +1 -4
- package/ts/errors.ts +9 -9
- package/ts/index.ts +17 -1
- package/ts/testUtils.ts +52 -55
- package/ts/types.ts +0 -1
- package/ts/utils/index.ts +1 -1
- package/ts/utils/isErrorConstructor.ts +2 -2
- package/ts/utils/notEqualMessage.ts +1 -1
- package/ts/utils/notSatisfiedMessage.ts +18 -14
- package/ts/utils/notThrownMessage.ts +1 -1
- package/ts/utils/promiseMessages.ts +12 -5
- package/ts/utils/unexpectedErrorMessage.ts +4 -3
|
@@ -1,96 +1,103 @@
|
|
|
1
1
|
import * as perf from 'perf_hooks'
|
|
2
|
-
import { State } from './types.js'
|
|
2
|
+
import type { State } from './types.js'
|
|
3
3
|
|
|
4
|
-
let timeTracker: { start(): void
|
|
5
|
-
|
|
6
|
-
if (process && typeof process.hrtime === 'function') {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
4
|
+
let timeTracker: { start(): void; taken(): number }
|
|
5
|
+
|
|
6
|
+
if (global['process'] && typeof global['process'].hrtime === 'function') {
|
|
7
|
+
let tick: [number, number]
|
|
8
|
+
timeTracker = {
|
|
9
|
+
start() {
|
|
10
|
+
tick = global['process'].hrtime()
|
|
11
|
+
},
|
|
12
|
+
taken() {
|
|
13
|
+
const [second, nanoSecond] = global['process'].hrtime(tick)
|
|
14
|
+
return second * 1000 + nanoSecond / 1e6
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
} else if (perf.performance && typeof perf.performance.now === 'function') {
|
|
18
|
+
const now = perf.performance.now
|
|
19
|
+
let tick: number
|
|
20
|
+
timeTracker = {
|
|
21
|
+
start() {
|
|
22
|
+
tick = now()
|
|
23
|
+
},
|
|
24
|
+
taken() {
|
|
25
|
+
return now() - tick
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
let tick: number
|
|
30
|
+
timeTracker = {
|
|
31
|
+
start() {
|
|
32
|
+
tick = new Date().valueOf()
|
|
33
|
+
},
|
|
34
|
+
taken() {
|
|
35
|
+
return new Date().valueOf() - tick
|
|
36
|
+
}
|
|
37
|
+
}
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
export class StateMachine {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
listeners: Record<number, Array<() => void>> = {}
|
|
42
|
+
step = 1
|
|
43
|
+
maxStep?: number
|
|
36
44
|
|
|
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
|
-
}
|
|
45
|
+
subStep = 0
|
|
46
|
+
minSubStep?: number
|
|
47
|
+
maxSubStep?: number
|
|
48
|
+
constructor(maxStep?: number) {
|
|
49
|
+
this.maxStep = maxStep
|
|
50
|
+
timeTracker.start()
|
|
51
|
+
}
|
|
52
|
+
jump(step: number) {
|
|
53
|
+
this.step = step
|
|
54
|
+
this.subStep = 0
|
|
55
|
+
return this.step
|
|
56
|
+
}
|
|
57
|
+
move() {
|
|
58
|
+
const listeners = this.listeners[this.step]
|
|
59
|
+
this.step = this.step + 1
|
|
60
|
+
this.subStep = 0
|
|
61
|
+
if (listeners) {
|
|
62
|
+
listeners.forEach(l => l())
|
|
63
|
+
}
|
|
64
|
+
return this.step
|
|
65
|
+
}
|
|
66
|
+
moveSubStep() {
|
|
67
|
+
const subStep = ++this.subStep
|
|
68
|
+
if (this.maxSubStep === this.subStep) this.move()
|
|
69
|
+
return subStep
|
|
70
|
+
}
|
|
71
|
+
get(): State {
|
|
72
|
+
const { step, subStep, maxStep, minSubStep, maxSubStep } = this
|
|
73
|
+
return {
|
|
74
|
+
step,
|
|
75
|
+
maxStep,
|
|
76
|
+
subStep,
|
|
77
|
+
minSubStep,
|
|
78
|
+
maxSubStep
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
on(step: number, listener: () => void) {
|
|
82
|
+
this.listeners[step] = [listener]
|
|
83
|
+
}
|
|
84
|
+
isNotValid(step: number) {
|
|
85
|
+
return step !== this.step || (this.maxStep !== undefined && this.maxStep < step)
|
|
86
|
+
}
|
|
87
|
+
isValid(step: number) {
|
|
88
|
+
return step === this.step
|
|
89
|
+
}
|
|
90
|
+
isMaxStepDefined() {
|
|
91
|
+
return this.maxStep !== undefined
|
|
92
|
+
}
|
|
93
|
+
stopAccepting() {
|
|
94
|
+
this.maxStep = this.step - 1
|
|
95
|
+
}
|
|
96
|
+
isAccepting() {
|
|
97
|
+
// istanbul ignore next
|
|
98
|
+
return this.maxStep ? this.maxStep >= this.step : true
|
|
99
|
+
}
|
|
100
|
+
getTimeTaken() {
|
|
101
|
+
return timeTracker.taken()
|
|
102
|
+
}
|
|
96
103
|
}
|
package/ts/assert-order/index.ts
CHANGED
package/ts/assert-order/types.ts
CHANGED
|
@@ -8,43 +8,50 @@ import { pathEqual } from './pathEqual.js'
|
|
|
8
8
|
import { rejects } from './rejects.js'
|
|
9
9
|
import { repeat } from './repeat.js'
|
|
10
10
|
import { resolves } from './resolves.js'
|
|
11
|
-
import { satisfies, SatisfyExpectation } from './satisfies.js'
|
|
11
|
+
import { satisfies, type SatisfyExpectation } from './satisfies.js'
|
|
12
12
|
import { throws } from './throws.js'
|
|
13
13
|
import { truthy } from './truthy.js'
|
|
14
14
|
|
|
15
15
|
export type Assertron = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
false(value: unknown): asserts value is false
|
|
17
|
+
falsy(value: any): asserts value is false | 0 | '' | null | undefined
|
|
18
|
+
isInstanceof<T extends AnyConstructor>(value: unknown, type: T): asserts value is InstanceType<T>
|
|
19
|
+
pathEqual(actual: string, expected: string): void
|
|
20
|
+
rejects<R = any>(promise: Promise<any>): Promise<R>
|
|
21
|
+
repeat<R>(
|
|
22
|
+
fn: () => R | (() => Promise<R>),
|
|
23
|
+
times: number
|
|
24
|
+
): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R
|
|
25
|
+
resolves(promise: Promise<any>): Promise<void>
|
|
26
|
+
satisfies<Actual, Expected extends Actual>(actual: Actual, expected: SatisfyExpectation<Expected>): void
|
|
27
|
+
throws<E extends Error>(value: PromiseLike<any>, error?: ErrorValidator | ErrorConstructor<E>): Promise<E>
|
|
28
|
+
throws<E extends Error>(value: (...args: any[]) => never, error?: ErrorValidator | ErrorConstructor<E>): E
|
|
29
|
+
throws<E extends Error, R = any>(
|
|
30
|
+
value: (...args: any[]) => R,
|
|
31
|
+
error?: ErrorValidator | ErrorConstructor<E>
|
|
32
|
+
): R extends Promise<any> ? Promise<E> : E
|
|
33
|
+
throws<T, R = any>(
|
|
34
|
+
value: (...args: any[]) => R,
|
|
35
|
+
validator?: ErrorValidator
|
|
36
|
+
): R extends Promise<any> ? Promise<T> : T
|
|
37
|
+
true(value: any): asserts value is true
|
|
38
|
+
truthy(value: unknown): void
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
export const assertron: Assertron = {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
},
|
|
49
|
-
truthy
|
|
42
|
+
false(value: any) {
|
|
43
|
+
if (value !== false) throw new AssertionError(notEqualMessage(value, false), { ssf: assertron.false })
|
|
44
|
+
},
|
|
45
|
+
falsy,
|
|
46
|
+
isInstanceof,
|
|
47
|
+
pathEqual,
|
|
48
|
+
rejects,
|
|
49
|
+
repeat,
|
|
50
|
+
resolves,
|
|
51
|
+
satisfies,
|
|
52
|
+
throws,
|
|
53
|
+
true(value: any) {
|
|
54
|
+
if (value !== true) throw new AssertionError(notEqualMessage(value, true), { ssf: assertron.true })
|
|
55
|
+
},
|
|
56
|
+
truthy
|
|
50
57
|
}
|
package/ts/assertron/falsy.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AssertionError } from '../errors.js'
|
|
2
2
|
|
|
3
3
|
export function falsy(value: any) {
|
|
4
|
-
|
|
5
|
-
throw new AssertionError(`Expected value to be falsy, but received ${value}`, { ssf: falsy })
|
|
4
|
+
if (value) throw new AssertionError(`Expected value to be falsy, but received ${value}`, { ssf: falsy })
|
|
6
5
|
}
|
|
@@ -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
|
}
|
|
@@ -1,32 +1,39 @@
|
|
|
1
1
|
import { createSatisfier } from 'satisfier'
|
|
2
|
-
import { If, IsExtend, NonComposableTypes } from 'type-plus'
|
|
2
|
+
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
|
-
import { ErrorConstructor, ErrorValidator } from '../types.js'
|
|
4
|
-
import {
|
|
3
|
+
import type { ErrorConstructor, ErrorValidator } from '../types.js'
|
|
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
|
}
|