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.
Files changed (155) hide show
  1. package/cjs/assert-order/AssertOrder.d.ts.map +1 -1
  2. package/cjs/assert-order/AssertOrder.js +3 -1
  3. package/cjs/assert-order/AssertOrder.js.map +1 -1
  4. package/cjs/assert-order/InvalidOrder.d.ts +1 -1
  5. package/cjs/assert-order/InvalidOrder.d.ts.map +1 -1
  6. package/cjs/assert-order/InvalidOrder.js +4 -2
  7. package/cjs/assert-order/InvalidOrder.js.map +1 -1
  8. package/cjs/assert-order/StateMachine.d.ts +1 -1
  9. package/cjs/assert-order/StateMachine.d.ts.map +1 -1
  10. package/cjs/assert-order/StateMachine.js +17 -8
  11. package/cjs/assert-order/StateMachine.js.map +1 -1
  12. package/cjs/assert-order/types.d.ts.map +1 -1
  13. package/cjs/assertron/assertron.d.ts +1 -1
  14. package/cjs/assertron/assertron.d.ts.map +1 -1
  15. package/cjs/assertron/assertron.js.map +1 -1
  16. package/cjs/assertron/falsy.d.ts.map +1 -1
  17. package/cjs/assertron/falsy.js.map +1 -1
  18. package/cjs/assertron/isInstanceof.d.ts.map +1 -1
  19. package/cjs/assertron/isInstanceof.js.map +1 -1
  20. package/cjs/assertron/pathEqual.js.map +1 -1
  21. package/cjs/assertron/rejects.d.ts.map +1 -1
  22. package/cjs/assertron/rejects.js +3 -1
  23. package/cjs/assertron/rejects.js.map +1 -1
  24. package/cjs/assertron/repeat.d.ts.map +1 -1
  25. package/cjs/assertron/repeat.js +3 -1
  26. package/cjs/assertron/repeat.js.map +1 -1
  27. package/cjs/assertron/resolves.d.ts.map +1 -1
  28. package/cjs/assertron/resolves.js +3 -1
  29. package/cjs/assertron/resolves.js.map +1 -1
  30. package/cjs/assertron/satisfies.d.ts +1 -1
  31. package/cjs/assertron/satisfies.d.ts.map +1 -1
  32. package/cjs/assertron/satisfies.js.map +1 -1
  33. package/cjs/assertron/throws.d.ts +1 -1
  34. package/cjs/assertron/throws.d.ts.map +1 -1
  35. package/cjs/assertron/throws.js +7 -3
  36. package/cjs/assertron/throws.js.map +1 -1
  37. package/cjs/assertron/truthy.d.ts.map +1 -1
  38. package/cjs/assertron/truthy.js.map +1 -1
  39. package/cjs/errors.d.ts +3 -3
  40. package/cjs/errors.d.ts.map +1 -1
  41. package/cjs/errors.js.map +1 -1
  42. package/cjs/index.d.ts +2 -1
  43. package/cjs/index.d.ts.map +1 -1
  44. package/cjs/index.js +15 -1
  45. package/cjs/index.js.map +1 -1
  46. package/cjs/package.json +1 -1
  47. package/cjs/types.d.ts.map +1 -1
  48. package/cjs/utils/index.js.map +1 -1
  49. package/cjs/utils/isErrorConstructor.d.ts +1 -1
  50. package/cjs/utils/isErrorConstructor.d.ts.map +1 -1
  51. package/cjs/utils/isErrorConstructor.js.map +1 -1
  52. package/cjs/utils/notEqualMessage.js.map +1 -1
  53. package/cjs/utils/notSatisfiedMessage.d.ts +1 -1
  54. package/cjs/utils/notSatisfiedMessage.d.ts.map +1 -1
  55. package/cjs/utils/notSatisfiedMessage.js +8 -3
  56. package/cjs/utils/notSatisfiedMessage.js.map +1 -1
  57. package/cjs/utils/notThrownMessage.js.map +1 -1
  58. package/cjs/utils/promiseMessages.d.ts.map +1 -1
  59. package/cjs/utils/promiseMessages.js +8 -4
  60. package/cjs/utils/promiseMessages.js.map +1 -1
  61. package/cjs/utils/unexpectedErrorMessage.d.ts +1 -1
  62. package/cjs/utils/unexpectedErrorMessage.d.ts.map +1 -1
  63. package/cjs/utils/unexpectedErrorMessage.js +1 -2
  64. package/cjs/utils/unexpectedErrorMessage.js.map +1 -1
  65. package/esm/assert-order/AssertOrder.d.ts.map +1 -1
  66. package/esm/assert-order/AssertOrder.js +3 -1
  67. package/esm/assert-order/AssertOrder.js.map +1 -1
  68. package/esm/assert-order/InvalidOrder.d.ts +1 -1
  69. package/esm/assert-order/InvalidOrder.d.ts.map +1 -1
  70. package/esm/assert-order/InvalidOrder.js +4 -2
  71. package/esm/assert-order/InvalidOrder.js.map +1 -1
  72. package/esm/assert-order/StateMachine.d.ts +1 -1
  73. package/esm/assert-order/StateMachine.d.ts.map +1 -1
  74. package/esm/assert-order/StateMachine.js +17 -8
  75. package/esm/assert-order/StateMachine.js.map +1 -1
  76. package/esm/assert-order/types.d.ts.map +1 -1
  77. package/esm/assertron/assertron.d.ts +1 -1
  78. package/esm/assertron/assertron.d.ts.map +1 -1
  79. package/esm/assertron/assertron.js.map +1 -1
  80. package/esm/assertron/falsy.d.ts.map +1 -1
  81. package/esm/assertron/falsy.js.map +1 -1
  82. package/esm/assertron/isInstanceof.d.ts.map +1 -1
  83. package/esm/assertron/isInstanceof.js.map +1 -1
  84. package/esm/assertron/pathEqual.js.map +1 -1
  85. package/esm/assertron/rejects.d.ts.map +1 -1
  86. package/esm/assertron/rejects.js +3 -1
  87. package/esm/assertron/rejects.js.map +1 -1
  88. package/esm/assertron/repeat.d.ts.map +1 -1
  89. package/esm/assertron/repeat.js +3 -1
  90. package/esm/assertron/repeat.js.map +1 -1
  91. package/esm/assertron/resolves.d.ts.map +1 -1
  92. package/esm/assertron/resolves.js +3 -1
  93. package/esm/assertron/resolves.js.map +1 -1
  94. package/esm/assertron/satisfies.d.ts +1 -1
  95. package/esm/assertron/satisfies.d.ts.map +1 -1
  96. package/esm/assertron/satisfies.js.map +1 -1
  97. package/esm/assertron/throws.d.ts +1 -1
  98. package/esm/assertron/throws.d.ts.map +1 -1
  99. package/esm/assertron/throws.js +7 -3
  100. package/esm/assertron/throws.js.map +1 -1
  101. package/esm/assertron/truthy.d.ts.map +1 -1
  102. package/esm/assertron/truthy.js.map +1 -1
  103. package/esm/errors.d.ts +3 -3
  104. package/esm/errors.d.ts.map +1 -1
  105. package/esm/errors.js.map +1 -1
  106. package/esm/index.d.ts +2 -1
  107. package/esm/index.d.ts.map +1 -1
  108. package/esm/index.js +2 -1
  109. package/esm/index.js.map +1 -1
  110. package/esm/types.d.ts.map +1 -1
  111. package/esm/utils/index.js.map +1 -1
  112. package/esm/utils/isErrorConstructor.d.ts +1 -1
  113. package/esm/utils/isErrorConstructor.d.ts.map +1 -1
  114. package/esm/utils/isErrorConstructor.js.map +1 -1
  115. package/esm/utils/notEqualMessage.js.map +1 -1
  116. package/esm/utils/notSatisfiedMessage.d.ts +1 -1
  117. package/esm/utils/notSatisfiedMessage.d.ts.map +1 -1
  118. package/esm/utils/notSatisfiedMessage.js +8 -3
  119. package/esm/utils/notSatisfiedMessage.js.map +1 -1
  120. package/esm/utils/notThrownMessage.js.map +1 -1
  121. package/esm/utils/promiseMessages.d.ts.map +1 -1
  122. package/esm/utils/promiseMessages.js +8 -4
  123. package/esm/utils/promiseMessages.js.map +1 -1
  124. package/esm/utils/unexpectedErrorMessage.d.ts +1 -1
  125. package/esm/utils/unexpectedErrorMessage.d.ts.map +1 -1
  126. package/esm/utils/unexpectedErrorMessage.js +1 -2
  127. package/esm/utils/unexpectedErrorMessage.js.map +1 -1
  128. package/package.json +3 -3
  129. package/ts/assert-order/AssertOrder.ts +138 -143
  130. package/ts/assert-order/InvalidOrder.ts +23 -19
  131. package/ts/assert-order/StateMachine.ts +96 -89
  132. package/ts/assert-order/index.ts +0 -1
  133. package/ts/assert-order/internalInterfaces.ts +3 -3
  134. package/ts/assert-order/types.ts +5 -5
  135. package/ts/assertron/assertron.ts +39 -32
  136. package/ts/assertron/falsy.ts +1 -2
  137. package/ts/assertron/isInstanceof.ts +6 -3
  138. package/ts/assertron/pathEqual.ts +2 -2
  139. package/ts/assertron/rejects.ts +6 -4
  140. package/ts/assertron/repeat.ts +27 -21
  141. package/ts/assertron/resolves.ts +6 -4
  142. package/ts/assertron/satisfies.ts +28 -21
  143. package/ts/assertron/throws.ts +76 -55
  144. package/ts/assertron/truthy.ts +1 -4
  145. package/ts/errors.ts +9 -9
  146. package/ts/index.ts +17 -1
  147. package/ts/testUtils.ts +52 -55
  148. package/ts/types.ts +0 -1
  149. package/ts/utils/index.ts +1 -1
  150. package/ts/utils/isErrorConstructor.ts +2 -2
  151. package/ts/utils/notEqualMessage.ts +1 -1
  152. package/ts/utils/notSatisfiedMessage.ts +18 -14
  153. package/ts/utils/notThrownMessage.ts +1 -1
  154. package/ts/utils/promiseMessages.ts +12 -5
  155. 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, taken(): number }
5
- // istanbul ignore else
6
- if (process && typeof process.hrtime === 'function') {
7
- let tick: [number, number]
8
- timeTracker = {
9
- start() { tick = process.hrtime() },
10
- taken() {
11
- const [second, nanoSecond] = process.hrtime(tick)
12
- return second * 1000 + nanoSecond / 1e6
13
- }
14
- }
15
- }
16
- else if (perf.performance && typeof perf.performance.now === 'function') {
17
- const now = perf.performance.now
18
- let tick: number
19
- timeTracker = {
20
- start() { tick = now() },
21
- taken() { return now() - tick }
22
- }
23
- }
24
- else {
25
- let tick: number
26
- timeTracker = {
27
- start() { tick = new Date().valueOf() },
28
- taken() { return new Date().valueOf() - tick }
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
- listeners: Record<number, Array<() => void>> = {}
34
- step = 1
35
- maxStep?: number
41
+ listeners: Record<number, Array<() => void>> = {}
42
+ step = 1
43
+ maxStep?: number
36
44
 
37
- subStep = 0
38
- minSubStep?: number
39
- maxSubStep?: number
40
- constructor(maxStep?: number) {
41
- this.maxStep = maxStep
42
- timeTracker.start()
43
- }
44
- jump(step: number) {
45
- this.step = step
46
- this.subStep = 0
47
- return this.step
48
- }
49
- move() {
50
- const listeners = this.listeners[this.step]
51
- this.step = this.step + 1
52
- this.subStep = 0
53
- if (listeners) {
54
- listeners.forEach(l => l())
55
- }
56
- return this.step
57
- }
58
- moveSubStep() {
59
- const subStep = ++this.subStep
60
- if (this.maxSubStep === this.subStep)
61
- this.move()
62
- return subStep
63
- }
64
- get(): State {
65
- const { step, subStep, maxStep, minSubStep, maxSubStep } = this
66
- return {
67
- step,
68
- maxStep,
69
- subStep,
70
- minSubStep,
71
- maxSubStep
72
- }
73
- }
74
- on(step: number, listener: () => void) {
75
- this.listeners[step] = [listener]
76
- }
77
- isNotValid(step: number) {
78
- return step !== this.step || (this.maxStep !== undefined && this.maxStep < step)
79
- }
80
- isValid(step: number) {
81
- return step === this.step
82
- }
83
- isMaxStepDefined() {
84
- return this.maxStep !== undefined
85
- }
86
- stopAccepting() {
87
- this.maxStep = this.step - 1
88
- }
89
- isAccepting() {
90
- // istanbul ignore next
91
- return this.maxStep ? this.maxStep >= this.step : true
92
- }
93
- getTimeTaken() {
94
- return timeTracker.taken()
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
  }
@@ -1,4 +1,3 @@
1
1
  export * from './AssertOrder.js'
2
2
  export * from './InvalidOrder.js'
3
3
  export * from './types.js'
4
-
@@ -1,5 +1,5 @@
1
1
  export interface Steps {
2
- once?: number[],
3
- some?: number[],
4
- all?: number[],
2
+ once?: number[]
3
+ some?: number[]
4
+ all?: number[]
5
5
  }
@@ -1,7 +1,7 @@
1
1
  export interface State {
2
- step: number,
3
- maxStep?: number,
4
- subStep: number,
5
- minSubStep?: number,
6
- maxSubStep?: number,
2
+ step: number
3
+ maxStep?: number
4
+ subStep: number
5
+ minSubStep?: number
6
+ maxSubStep?: number
7
7
  }
@@ -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
- 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>(fn: () => R | (() => Promise<R>), times: number): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R,
22
- resolves(promise: Promise<any>): Promise<void>,
23
- satisfies<Actual, Expected extends Actual>(actual: Actual, expected: SatisfyExpectation<Expected>): void,
24
- throws<E extends Error>(value: PromiseLike<any>, error?: ErrorValidator | ErrorConstructor<E>): Promise<E>,
25
- throws<E extends Error>(value: (...args: any[]) => never, error?: ErrorValidator | ErrorConstructor<E>): E,
26
- throws<E extends Error, R = any>(value: (...args: any[]) => R, error?: ErrorValidator | ErrorConstructor<E>): R extends Promise<any> ? Promise<E> : E,
27
- throws<T, R = any>(value: (...args: any[]) => R, validator?: ErrorValidator): R extends Promise<any> ? Promise<T> : T,
28
- true(value: any): asserts value is true,
29
- truthy(value: unknown): void
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
- false(value: any) {
34
- if (value !== false)
35
- throw new AssertionError(notEqualMessage(value, false), { ssf: assertron.false })
36
- },
37
- falsy,
38
- isInstanceof,
39
- pathEqual,
40
- rejects,
41
- repeat,
42
- resolves,
43
- satisfies,
44
- throws,
45
- true(value: any) {
46
- if (value !== true)
47
- throw new AssertionError(notEqualMessage(value, true), { ssf: assertron.true })
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
  }
@@ -1,6 +1,5 @@
1
1
  import { AssertionError } from '../errors.js'
2
2
 
3
3
  export function falsy(value: any) {
4
- if (value)
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>(value: unknown, type: T): asserts value is InstanceType<T> {
5
- if (value instanceof type) return
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
- throw new AssertionError(`Expected ${value} to be an instance of ${type.name}`, { ssf: isInstanceof })
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
- if (!isPathEqual(actual, expected))
7
- throw new AssertionError(notEqualMessage(actual, expected), { ssf: pathEqual })
6
+ if (!isPathEqual(actual, expected))
7
+ throw new AssertionError(notEqualMessage(actual, expected), { ssf: pathEqual })
8
8
  }
@@ -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
- return promise.then(
6
- value => { throw new AssertionError(notRejectedMessage(value), { ssf: rejects }) },
7
- err => err
8
- )
5
+ return promise.then(
6
+ value => {
7
+ throw new AssertionError(notRejectedMessage(value), { ssf: rejects })
8
+ },
9
+ err => err
10
+ )
9
11
  }
@@ -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>(fn: () => R | (() => Promise<R>), times: number): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R {
9
- return repeatRecur(fn, 1, times)
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>(fn: () => R | (() => Promise<R>), i: number, times: number): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R {
13
- try {
14
- const result = fn()
15
- if (i === times) return result as any
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
- if (isPromise(result)) {
18
- return result.then(
19
- () => repeatRecur(fn, i + 1, times),
20
- (e) => { throw new FailOnOccurrence(i, e, { ssf: repeat }) }
21
- ) as any
22
- }
23
- else {
24
- return repeatRecur(fn, i + 1, times)
25
- }
26
- }
27
- catch (e) {
28
- if (e instanceof FailOnOccurrence) throw e
29
- throw new FailOnOccurrence(i, e, { ssf: repeat })
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
-
@@ -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
- return promise.then(
6
- () => { },
7
- error => { throw new AssertionError(notResolvedMessage(error), { ssf: resolves }) }
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> = If<
7
- IsExtend<T, string>,
8
- T | RegExp,
9
- If<
10
- IsExtend<T, NonComposableTypes>,
11
- T,
12
- If<IsExtend<T, Array<any>>,
13
- T extends Array<infer E> ? Array<SatisfyExpectation<E>> : never,
14
- {
15
- [P in keyof T]?: T[P] extends string
16
- ? SatisfyExpectation<T[P]> | RegExp
17
- : SatisfyExpectation<T[P]>
18
- }
19
- >>
20
- > | ((v: T) => boolean)
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>(actual: Actual, expected: SatisfyExpectation<Expected>) {
28
- const diff = createSatisfier(expected as any).exec(actual)
29
- if (diff) {
30
- throw new AssertionError(notSatisfiedMessage(diff), { ssf: satisfies })
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
  }
@@ -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 { isErrorConstructor, notRejectedMessage, notThrownMessage, unexpectedErrorMessage } from '../utils/index.js'
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>(value: PromiseLike<any>, error?: ErrorValidator | ErrorConstructor<E>): Promise<E>
7
- export function throws<E extends Error>(value: (...args: any[]) => never, error?: ErrorValidator | ErrorConstructor<E>): E
8
- export function throws<E extends Error, R = any>(value: (...args: any[]) => R, error?: ErrorValidator | ErrorConstructor<E>): R extends Promise<any> ? Promise<E> : E
9
- export function throws<T, R = any>(value: (...args: any[]) => R, validator?: ErrorValidator): R extends Promise<any> ? Promise<T> : T
10
- export function throws(value: PromiseLike<any> | (() => any | PromiseLike<any>), validator?: ErrorValidator | ErrorConstructor<any>): any {
11
- if (typeof value !== 'function' && !isPromise(value)) {
12
- throw new AssertionError(
13
- '`assertron.throws()` must be called with a function or promise.',
14
- { ssf: throws }
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
- if (!isPromise(value)) {
19
- const { threw, err, result } = tryCatch(value)
20
- if (threw) {
21
- if (failValidation(validator, err)) {
22
- throw new AssertionError(unexpectedErrorMessage(err, validator), { ssf: throws })
23
- }
24
- return err
25
- }
26
- value = result
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
- if (isPromise(value)) {
30
- return value.then(
31
- value => { throw new AssertionError(notRejectedMessage(value), { ssf: throws }) },
32
- err => {
33
- if (failValidation(validator, err)) {
34
- throw new AssertionError(unexpectedErrorMessage(err, validator), { ssf: throws })
35
- }
36
- return err
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
- throw new AssertionError(notThrownMessage(value), { ssf: throws })
62
+ throw new AssertionError(notThrownMessage(value), { ssf: throws })
42
63
  }
43
64
 
44
65
  function tryCatch(fn: () => any) {
45
- let result
46
- let err
47
- let threw = false
48
- try {
49
- result = fn()
50
- }
51
- catch (e) {
52
- err = e
53
- threw = true
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(validator: ErrorValidator | ErrorConstructor<any> | undefined, error: any): validator is ErrorValidator | ErrorConstructor<any> {
58
- if (!validator) return false
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
- if (isErrorConstructor(validator)) {
61
- return !(error instanceof validator)
62
- }
63
- else {
64
- return !validator(error)
65
- }
83
+ if (isErrorConstructor(validator)) {
84
+ return !(error instanceof validator)
85
+ } else {
86
+ return !validator(error)
87
+ }
66
88
  }
67
-
@@ -1,8 +1,5 @@
1
1
  import { AssertionError } from '../errors.js'
2
2
 
3
3
  export function truthy(value: unknown) {
4
- if (!value) throw new AssertionError(
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
  }