assertron 10.0.0 → 10.4.0

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 (188) hide show
  1. package/README.md +2 -1
  2. package/cjs/assert-order/AssertOrder.d.ts +1 -1
  3. package/cjs/assert-order/AssertOrder.d.ts.map +1 -1
  4. package/cjs/assert-order/AssertOrder.js +103 -176
  5. package/cjs/assert-order/AssertOrder.js.map +1 -1
  6. package/cjs/assert-order/InvalidOrder.d.ts +1 -1
  7. package/cjs/assert-order/InvalidOrder.d.ts.map +1 -1
  8. package/cjs/assert-order/InvalidOrder.js +18 -34
  9. package/cjs/assert-order/InvalidOrder.js.map +1 -1
  10. package/cjs/assert-order/StateMachine.d.ts +1 -1
  11. package/cjs/assert-order/StateMachine.d.ts.map +1 -1
  12. package/cjs/assert-order/StateMachine.js +102 -125
  13. package/cjs/assert-order/StateMachine.js.map +1 -1
  14. package/cjs/assert-order/types.d.ts.map +1 -1
  15. package/cjs/assertron/assertron.d.ts +20 -19
  16. package/cjs/assertron/assertron.d.ts.map +1 -1
  17. package/cjs/assertron/assertron.js +22 -13
  18. package/cjs/assertron/assertron.js.map +1 -1
  19. package/cjs/assertron/falsy.d.ts +1 -1
  20. package/cjs/assertron/falsy.d.ts.map +1 -1
  21. package/cjs/assertron/falsy.js +2 -2
  22. package/cjs/assertron/falsy.js.map +1 -1
  23. package/cjs/assertron/index.d.ts +1 -0
  24. package/cjs/assertron/index.d.ts.map +1 -1
  25. package/cjs/assertron/isInstanceof.d.ts +3 -0
  26. package/cjs/assertron/isInstanceof.d.ts.map +1 -0
  27. package/cjs/assertron/isInstanceof.js +11 -0
  28. package/cjs/assertron/isInstanceof.js.map +1 -0
  29. package/cjs/assertron/pathEqual.d.ts.map +1 -1
  30. package/cjs/assertron/pathEqual.js +3 -3
  31. package/cjs/assertron/pathEqual.js.map +1 -1
  32. package/cjs/assertron/rejects.d.ts +1 -1
  33. package/cjs/assertron/rejects.d.ts.map +1 -1
  34. package/cjs/assertron/rejects.js +5 -3
  35. package/cjs/assertron/rejects.js.map +1 -1
  36. package/cjs/assertron/repeat.d.ts.map +1 -1
  37. package/cjs/assertron/repeat.js +6 -4
  38. package/cjs/assertron/repeat.js.map +1 -1
  39. package/cjs/assertron/resolves.d.ts.map +1 -1
  40. package/cjs/assertron/resolves.js +5 -3
  41. package/cjs/assertron/resolves.js.map +1 -1
  42. package/cjs/assertron/satisfies.d.ts +4 -3
  43. package/cjs/assertron/satisfies.d.ts.map +1 -1
  44. package/cjs/assertron/satisfies.js +6 -5
  45. package/cjs/assertron/satisfies.js.map +1 -1
  46. package/cjs/assertron/throws.d.ts +1 -1
  47. package/cjs/assertron/throws.d.ts.map +1 -1
  48. package/cjs/assertron/throws.js +15 -11
  49. package/cjs/assertron/throws.js.map +1 -1
  50. package/cjs/assertron/truthy.d.ts +1 -1
  51. package/cjs/assertron/truthy.d.ts.map +1 -1
  52. package/cjs/assertron/truthy.js +2 -2
  53. package/cjs/assertron/truthy.js.map +1 -1
  54. package/cjs/errors.d.ts +3 -3
  55. package/cjs/errors.d.ts.map +1 -1
  56. package/cjs/errors.js +12 -32
  57. package/cjs/errors.js.map +1 -1
  58. package/cjs/index.d.ts +2 -1
  59. package/cjs/index.d.ts.map +1 -1
  60. package/cjs/index.js +16 -2
  61. package/cjs/index.js.map +1 -1
  62. package/cjs/package.json +1 -0
  63. package/cjs/types.d.ts +2 -2
  64. package/cjs/types.d.ts.map +1 -1
  65. package/cjs/utils/index.js +2 -2
  66. package/cjs/utils/index.js.map +1 -1
  67. package/cjs/utils/isErrorConstructor.d.ts +1 -1
  68. package/cjs/utils/isErrorConstructor.d.ts.map +1 -1
  69. package/cjs/utils/isErrorConstructor.js.map +1 -1
  70. package/cjs/utils/notEqualMessage.js +1 -1
  71. package/cjs/utils/notEqualMessage.js.map +1 -1
  72. package/cjs/utils/notSatisfiedMessage.d.ts +1 -1
  73. package/cjs/utils/notSatisfiedMessage.d.ts.map +1 -1
  74. package/cjs/utils/notSatisfiedMessage.js +12 -16
  75. package/cjs/utils/notSatisfiedMessage.js.map +1 -1
  76. package/cjs/utils/notThrownMessage.js +2 -2
  77. package/cjs/utils/notThrownMessage.js.map +1 -1
  78. package/cjs/utils/promiseMessages.d.ts.map +1 -1
  79. package/cjs/utils/promiseMessages.js +9 -5
  80. package/cjs/utils/promiseMessages.js.map +1 -1
  81. package/cjs/utils/unexpectedErrorMessage.d.ts +1 -1
  82. package/cjs/utils/unexpectedErrorMessage.d.ts.map +1 -1
  83. package/cjs/utils/unexpectedErrorMessage.js +3 -4
  84. package/cjs/utils/unexpectedErrorMessage.js.map +1 -1
  85. package/esm/assert-order/AssertOrder.d.ts +1 -1
  86. package/esm/assert-order/AssertOrder.d.ts.map +1 -1
  87. package/esm/assert-order/AssertOrder.js +12 -8
  88. package/esm/assert-order/AssertOrder.js.map +1 -1
  89. package/esm/assert-order/InvalidOrder.d.ts +1 -1
  90. package/esm/assert-order/InvalidOrder.d.ts.map +1 -1
  91. package/esm/assert-order/InvalidOrder.js +4 -2
  92. package/esm/assert-order/InvalidOrder.js.map +1 -1
  93. package/esm/assert-order/StateMachine.d.ts +1 -1
  94. package/esm/assert-order/StateMachine.d.ts.map +1 -1
  95. package/esm/assert-order/StateMachine.js +20 -10
  96. package/esm/assert-order/StateMachine.js.map +1 -1
  97. package/esm/assert-order/types.d.ts.map +1 -1
  98. package/esm/assertron/assertron.d.ts +20 -19
  99. package/esm/assertron/assertron.d.ts.map +1 -1
  100. package/esm/assertron/assertron.js +10 -1
  101. package/esm/assertron/assertron.js.map +1 -1
  102. package/esm/assertron/falsy.d.ts +1 -1
  103. package/esm/assertron/falsy.d.ts.map +1 -1
  104. package/esm/assertron/falsy.js.map +1 -1
  105. package/esm/assertron/index.d.ts +1 -0
  106. package/esm/assertron/index.d.ts.map +1 -1
  107. package/esm/assertron/isInstanceof.d.ts +3 -0
  108. package/esm/assertron/isInstanceof.d.ts.map +1 -0
  109. package/esm/assertron/isInstanceof.js +7 -0
  110. package/esm/assertron/isInstanceof.js.map +1 -0
  111. package/esm/assertron/pathEqual.d.ts.map +1 -1
  112. package/esm/assertron/pathEqual.js.map +1 -1
  113. package/esm/assertron/rejects.d.ts +1 -1
  114. package/esm/assertron/rejects.d.ts.map +1 -1
  115. package/esm/assertron/rejects.js +3 -1
  116. package/esm/assertron/rejects.js.map +1 -1
  117. package/esm/assertron/repeat.d.ts.map +1 -1
  118. package/esm/assertron/repeat.js +3 -1
  119. package/esm/assertron/repeat.js.map +1 -1
  120. package/esm/assertron/resolves.d.ts.map +1 -1
  121. package/esm/assertron/resolves.js +3 -1
  122. package/esm/assertron/resolves.js.map +1 -1
  123. package/esm/assertron/satisfies.d.ts +4 -3
  124. package/esm/assertron/satisfies.d.ts.map +1 -1
  125. package/esm/assertron/satisfies.js +2 -1
  126. package/esm/assertron/satisfies.js.map +1 -1
  127. package/esm/assertron/throws.d.ts +1 -1
  128. package/esm/assertron/throws.d.ts.map +1 -1
  129. package/esm/assertron/throws.js +7 -3
  130. package/esm/assertron/throws.js.map +1 -1
  131. package/esm/assertron/truthy.d.ts +1 -1
  132. package/esm/assertron/truthy.d.ts.map +1 -1
  133. package/esm/assertron/truthy.js.map +1 -1
  134. package/esm/errors.d.ts +3 -3
  135. package/esm/errors.d.ts.map +1 -1
  136. package/esm/errors.js.map +1 -1
  137. package/esm/index.d.ts +2 -1
  138. package/esm/index.d.ts.map +1 -1
  139. package/esm/index.js +2 -1
  140. package/esm/index.js.map +1 -1
  141. package/esm/types.d.ts +2 -2
  142. package/esm/types.d.ts.map +1 -1
  143. package/esm/utils/index.js.map +1 -1
  144. package/esm/utils/isErrorConstructor.d.ts +1 -1
  145. package/esm/utils/isErrorConstructor.d.ts.map +1 -1
  146. package/esm/utils/isErrorConstructor.js.map +1 -1
  147. package/esm/utils/notEqualMessage.js.map +1 -1
  148. package/esm/utils/notSatisfiedMessage.d.ts +1 -1
  149. package/esm/utils/notSatisfiedMessage.d.ts.map +1 -1
  150. package/esm/utils/notSatisfiedMessage.js +8 -3
  151. package/esm/utils/notSatisfiedMessage.js.map +1 -1
  152. package/esm/utils/notThrownMessage.js.map +1 -1
  153. package/esm/utils/promiseMessages.d.ts.map +1 -1
  154. package/esm/utils/promiseMessages.js +8 -4
  155. package/esm/utils/promiseMessages.js.map +1 -1
  156. package/esm/utils/unexpectedErrorMessage.d.ts +1 -1
  157. package/esm/utils/unexpectedErrorMessage.d.ts.map +1 -1
  158. package/esm/utils/unexpectedErrorMessage.js +1 -2
  159. package/esm/utils/unexpectedErrorMessage.js.map +1 -1
  160. package/package.json +56 -50
  161. package/ts/assert-order/AssertOrder.ts +139 -143
  162. package/ts/assert-order/InvalidOrder.ts +23 -19
  163. package/ts/assert-order/StateMachine.ts +97 -89
  164. package/ts/assert-order/index.ts +0 -1
  165. package/ts/assert-order/internalInterfaces.ts +3 -3
  166. package/ts/assert-order/types.ts +5 -5
  167. package/ts/assertron/assertron.ts +56 -18
  168. package/ts/assertron/falsy.ts +2 -3
  169. package/ts/assertron/index.ts +1 -0
  170. package/ts/assertron/isInstanceof.ts +11 -0
  171. package/ts/assertron/pathEqual.ts +3 -3
  172. package/ts/assertron/rejects.ts +7 -5
  173. package/ts/assertron/repeat.ts +27 -21
  174. package/ts/assertron/resolves.ts +7 -5
  175. package/ts/assertron/satisfies.ts +30 -22
  176. package/ts/assertron/throws.ts +76 -55
  177. package/ts/assertron/truthy.ts +2 -5
  178. package/ts/errors.ts +9 -9
  179. package/ts/index.ts +17 -1
  180. package/ts/testUtils.ts +52 -55
  181. package/ts/types.ts +0 -1
  182. package/ts/utils/index.ts +1 -1
  183. package/ts/utils/isErrorConstructor.ts +2 -2
  184. package/ts/utils/notEqualMessage.ts +1 -1
  185. package/ts/utils/notSatisfiedMessage.ts +18 -14
  186. package/ts/utils/notThrownMessage.ts +1 -1
  187. package/ts/utils/promiseMessages.ts +12 -5
  188. package/ts/utils/unexpectedErrorMessage.ts +4 -3
@@ -1,35 +1,39 @@
1
1
  import { AssertionError } from '../errors.js'
2
- import { State } from './types.js'
2
+ import type { State } from './types.js'
3
3
 
4
4
  export class InvalidOrder extends AssertionError {
5
- constructor(public state: State, public method: string, public args: any[], options: AssertionError.Options) {
6
- super(method === 'end' ? getEndMessage(state) : getExpectingMessage(state, method, args), options)
7
- }
5
+ constructor(
6
+ public state: State,
7
+ public method: string,
8
+ public args: any[],
9
+ options: AssertionError.Options
10
+ ) {
11
+ super(method === 'end' ? getEndMessage(state) : getExpectingMessage(state, method, args), options)
12
+ }
8
13
  }
9
14
 
10
15
  function getEndMessage(state: State) {
11
- return `Planned for ${state.maxStep} step but expecting step ${state.step} when 'end()' is called`
16
+ return `Planned for ${state.maxStep} step but expecting step ${state.step} when 'end()' is called`
12
17
  }
13
18
 
14
19
  function getExpectingMessage(state: State, method: string, args: any[]) {
15
- return `Expecting ${getExpectingCalls(state)}, but received '${method}(${getMethodArgs(args)})'`
20
+ return `Expecting ${getExpectingCalls(state)}, but received '${method}(${getMethodArgs(args)})'`
16
21
  }
17
22
 
18
23
  function getExpectingCalls(state: State) {
19
- let message: string
20
- if (state.subStep === 0) {
21
- const methods = ['is', 'once', 'any']
22
- message = methods.map(
23
- m => `'${m}(${['any'].indexOf(m) === -1 ? state.step : `[${state.step}]`})'`
24
- ).join(', ')
25
- }
26
- else {
27
- const methods = ['exactly']
28
- message = methods.map(m => `'${m}(${state.step}, ${state.maxSubStep})'`).join(', ')
29
- }
30
- return message
24
+ let message: string
25
+ if (state.subStep === 0) {
26
+ const methods = ['is', 'once', 'any']
27
+ message = methods
28
+ .map(m => `'${m}(${['any'].indexOf(m) === -1 ? state.step : `[${state.step}]`})'`)
29
+ .join(', ')
30
+ } else {
31
+ const methods = ['exactly']
32
+ message = methods.map(m => `'${m}(${state.step}, ${state.maxSubStep})'`).join(', ')
33
+ }
34
+ return message
31
35
  }
32
36
 
33
37
  function getMethodArgs(args: any[]) {
34
- return args.map(a => Array.isArray(a) ? `[${a.join(', ')}]` : a).join(', ')
38
+ return args.map(a => (Array.isArray(a) ? `[${a.join(', ')}]` : a)).join(', ')
35
39
  }
@@ -1,95 +1,103 @@
1
- import { performance } from 'perf_hooks'
2
- import { State } from './types.js'
1
+ import * as perf from 'perf_hooks'
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 (performance && typeof performance.now === 'function') {
17
- let tick: number
18
- timeTracker = {
19
- start() { tick = performance.now() },
20
- taken() { return performance.now() - tick }
21
- }
22
- }
23
- else {
24
- let tick: number
25
- timeTracker = {
26
- start() { tick = new Date().valueOf() },
27
- taken() { return new Date().valueOf() - tick }
28
- }
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
+ }
29
38
  }
30
39
 
31
40
  export class StateMachine {
32
- listeners: Record<number, Array<() => void>> = {}
33
- step = 1
34
- maxStep?: number
41
+ listeners: Record<number, Array<() => void>> = {}
42
+ step = 1
43
+ maxStep?: number
35
44
 
36
- subStep = 0
37
- minSubStep?: number
38
- maxSubStep?: number
39
- constructor(maxStep?: number) {
40
- this.maxStep = maxStep
41
- timeTracker.start()
42
- }
43
- jump(step: number) {
44
- this.step = step
45
- this.subStep = 0
46
- return this.step
47
- }
48
- move() {
49
- const listeners = this.listeners[this.step]
50
- this.step = this.step + 1
51
- this.subStep = 0
52
- if (listeners) {
53
- listeners.forEach(l => l())
54
- }
55
- return this.step
56
- }
57
- moveSubStep() {
58
- const subStep = ++this.subStep
59
- if (this.maxSubStep === this.subStep)
60
- this.move()
61
- return subStep
62
- }
63
- get(): State {
64
- const { step, subStep, maxStep, minSubStep, maxSubStep } = this
65
- return {
66
- step,
67
- maxStep,
68
- subStep,
69
- minSubStep,
70
- maxSubStep
71
- }
72
- }
73
- on(step: number, listener: () => void) {
74
- this.listeners[step] = [listener]
75
- }
76
- isNotValid(step: number) {
77
- return step !== this.step || (this.maxStep !== undefined && this.maxStep < step)
78
- }
79
- isValid(step: number) {
80
- return step === this.step
81
- }
82
- isMaxStepDefined() {
83
- return this.maxStep !== undefined
84
- }
85
- stopAccepting() {
86
- this.maxStep = this.step - 1
87
- }
88
- isAccepting() {
89
- // istanbul ignore next
90
- return this.maxStep ? this.maxStep >= this.step : true
91
- }
92
- getTimeTaken() {
93
- return timeTracker.taken()
94
- }
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
+ }
95
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
  }
@@ -1,29 +1,67 @@
1
+ import type { AnyConstructor } from 'type-plus'
1
2
  import { AssertionError } from '../errors.js'
3
+ import type { ErrorConstructor, ErrorValidator } from '../types.js'
2
4
  import { notEqualMessage } from '../utils/index.js'
3
5
  import { falsy } from './falsy.js'
6
+ import { isInstanceof } from './isInstanceof.js'
4
7
  import { pathEqual } from './pathEqual.js'
5
8
  import { rejects } from './rejects.js'
6
9
  import { repeat } from './repeat.js'
7
10
  import { resolves } from './resolves.js'
8
- import { satisfies } from './satisfies.js'
11
+ import { satisfies, type SatisfyExpectation } from './satisfies.js'
9
12
  import { throws } from './throws.js'
10
13
  import { truthy } from './truthy.js'
11
14
 
12
- export const assertron = {
13
- false(value: any) {
14
- if (value !== false)
15
- throw new AssertionError(notEqualMessage(value, false), { ssf: assertron.false })
16
- },
17
- falsy,
18
- pathEqual,
19
- rejects,
20
- repeat,
21
- resolves,
22
- satisfies,
23
- throws,
24
- true(value: any) {
25
- if (value !== true)
26
- throw new AssertionError(notEqualMessage(value, true), { ssf: assertron.true })
27
- },
28
- truthy
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>(
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: unknown): asserts value is true
38
+ truthy(value: unknown): void
39
+ uuid(value: unknown): void
40
+ }
41
+
42
+ export const assertron: Assertron = {
43
+ false(value: unknown): void {
44
+ if (value !== false) throw new AssertionError(notEqualMessage(value, false), { ssf: assertron.false })
45
+ },
46
+ falsy,
47
+ isInstanceof,
48
+ pathEqual,
49
+ rejects,
50
+ repeat,
51
+ resolves,
52
+ satisfies,
53
+ throws,
54
+ true(value: unknown): void {
55
+ if (value !== true) throw new AssertionError(notEqualMessage(value, true), { ssf: assertron.true })
56
+ },
57
+ truthy,
58
+ uuid(value: unknown) {
59
+ if (typeof value !== 'string')
60
+ throw new AssertionError(`Expected ${value} to be a string`, { ssf: assertron.uuid })
61
+
62
+ if (/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/.test(value))
63
+ return
64
+
65
+ throw new AssertionError(`Expected ${value} to be a valid UUID`, { ssf: assertron.uuid })
66
+ }
29
67
  }
@@ -1,6 +1,5 @@
1
1
  import { AssertionError } from '../errors.js'
2
2
 
3
- export function falsy(value: any) {
4
- if (value)
5
- throw new AssertionError(`Expected value to be falsy, but received ${value}`, { ssf: falsy })
3
+ export function falsy(value: unknown): void {
4
+ if (value) throw new AssertionError(`Expected value to be falsy, but received ${value}`, { ssf: falsy })
6
5
  }
@@ -1 +1,2 @@
1
1
  export * from './assertron.js'
2
+ export type { SatisfyExpectation } from './satisfies.js'
@@ -0,0 +1,11 @@
1
+ import type { AnyConstructor } from 'type-plus'
2
+ import { AssertionError } from '../errors.js'
3
+
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
9
+
10
+ throw new AssertionError(`Expected ${value} to be an instance of ${type.name}`, { ssf: isInstanceof })
11
+ }
@@ -2,7 +2,7 @@ import { pathEqual as isPathEqual } from 'path-equal'
2
2
  import { AssertionError } from '../errors.js'
3
3
  import { notEqualMessage } from '../utils/index.js'
4
4
 
5
- export function pathEqual(actual: string, expected: string) {
6
- if (!isPathEqual(actual, expected))
7
- throw new AssertionError(notEqualMessage(actual, expected), { ssf: pathEqual })
5
+ export function pathEqual(actual: string, expected: string): void {
6
+ if (!isPathEqual(actual, expected))
7
+ throw new AssertionError(notEqualMessage(actual, expected), { ssf: pathEqual })
8
8
  }
@@ -1,9 +1,11 @@
1
1
  import { AssertionError } from '../errors.js'
2
2
  import { notRejectedMessage } from '../utils/index.js'
3
3
 
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
- )
4
+ export function rejects<R = any>(promise: Promise<unknown>): Promise<R> {
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
-
@@ -1,9 +1,11 @@
1
1
  import { AssertionError } from '../errors.js'
2
2
  import { notResolvedMessage } from '../utils/index.js'
3
3
 
4
- export function resolves(promise: Promise<any>) {
5
- return promise.then(
6
- () => { },
7
- error => { throw new AssertionError(notResolvedMessage(error), { ssf: resolves }) }
8
- )
4
+ export function resolves(promise: Promise<any>): Promise<void> {
5
+ return promise.then(
6
+ v => v,
7
+ error => {
8
+ throw new AssertionError(notResolvedMessage(error), { ssf: resolves })
9
+ }
10
+ )
9
11
  }
@@ -1,31 +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
- * @param expected All properties can be a value which will be compared to the same property in `actual`, RegExp, or a predicate function that will be used to check against the property.
28
+ * @param expected All properties can be a value which will be compared to the same property in `actual`,
29
+ * RegExp, or a predicate function that will be used to check against the property.
25
30
  */
26
- export function satisfies<Actual, Expected extends Actual>(actual: Actual, expected: SatisfyExpectation<Expected>) {
27
- const diff = createSatisfier(expected as any).exec(actual)
28
- if (diff) {
29
- throw new AssertionError(notSatisfiedMessage(diff), { ssf: satisfies })
30
- }
31
+ export function satisfies<Actual, Expected extends Actual>(
32
+ actual: Actual,
33
+ expected: SatisfyExpectation<Expected>
34
+ ): void {
35
+ const diff = createSatisfier(expected as any).exec(actual)
36
+ if (diff) {
37
+ throw new AssertionError(notSatisfiedMessage(diff), { ssf: satisfies })
38
+ }
31
39
  }