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.
Files changed (126) 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.map +1 -1
  5. package/cjs/assert-order/InvalidOrder.js +4 -2
  6. package/cjs/assert-order/InvalidOrder.js.map +1 -1
  7. package/cjs/assert-order/StateMachine.d.ts.map +1 -1
  8. package/cjs/assert-order/StateMachine.js +17 -8
  9. package/cjs/assert-order/StateMachine.js.map +1 -1
  10. package/cjs/assert-order/types.d.ts.map +1 -1
  11. package/cjs/assertron/assertron.d.ts.map +1 -1
  12. package/cjs/assertron/assertron.js.map +1 -1
  13. package/cjs/assertron/falsy.d.ts.map +1 -1
  14. package/cjs/assertron/falsy.js.map +1 -1
  15. package/cjs/assertron/isInstanceof.d.ts.map +1 -1
  16. package/cjs/assertron/isInstanceof.js.map +1 -1
  17. package/cjs/assertron/pathEqual.js.map +1 -1
  18. package/cjs/assertron/rejects.d.ts.map +1 -1
  19. package/cjs/assertron/rejects.js +3 -1
  20. package/cjs/assertron/rejects.js.map +1 -1
  21. package/cjs/assertron/repeat.d.ts.map +1 -1
  22. package/cjs/assertron/repeat.js +3 -1
  23. package/cjs/assertron/repeat.js.map +1 -1
  24. package/cjs/assertron/resolves.d.ts.map +1 -1
  25. package/cjs/assertron/resolves.js +3 -1
  26. package/cjs/assertron/resolves.js.map +1 -1
  27. package/cjs/assertron/satisfies.d.ts.map +1 -1
  28. package/cjs/assertron/satisfies.js.map +1 -1
  29. package/cjs/assertron/throws.d.ts.map +1 -1
  30. package/cjs/assertron/throws.js +7 -3
  31. package/cjs/assertron/throws.js.map +1 -1
  32. package/cjs/assertron/truthy.d.ts.map +1 -1
  33. package/cjs/assertron/truthy.js.map +1 -1
  34. package/cjs/errors.d.ts.map +1 -1
  35. package/cjs/errors.js.map +1 -1
  36. package/cjs/package.json +1 -1
  37. package/cjs/types.d.ts.map +1 -1
  38. package/cjs/utils/index.js.map +1 -1
  39. package/cjs/utils/isErrorConstructor.js.map +1 -1
  40. package/cjs/utils/notEqualMessage.js.map +1 -1
  41. package/cjs/utils/notSatisfiedMessage.d.ts.map +1 -1
  42. package/cjs/utils/notSatisfiedMessage.js +8 -3
  43. package/cjs/utils/notSatisfiedMessage.js.map +1 -1
  44. package/cjs/utils/notThrownMessage.js.map +1 -1
  45. package/cjs/utils/promiseMessages.d.ts.map +1 -1
  46. package/cjs/utils/promiseMessages.js +8 -4
  47. package/cjs/utils/promiseMessages.js.map +1 -1
  48. package/cjs/utils/unexpectedErrorMessage.d.ts.map +1 -1
  49. package/cjs/utils/unexpectedErrorMessage.js +1 -2
  50. package/cjs/utils/unexpectedErrorMessage.js.map +1 -1
  51. package/esm/assert-order/AssertOrder.d.ts.map +1 -1
  52. package/esm/assert-order/AssertOrder.js +3 -1
  53. package/esm/assert-order/AssertOrder.js.map +1 -1
  54. package/esm/assert-order/InvalidOrder.d.ts.map +1 -1
  55. package/esm/assert-order/InvalidOrder.js +4 -2
  56. package/esm/assert-order/InvalidOrder.js.map +1 -1
  57. package/esm/assert-order/StateMachine.d.ts.map +1 -1
  58. package/esm/assert-order/StateMachine.js +17 -8
  59. package/esm/assert-order/StateMachine.js.map +1 -1
  60. package/esm/assert-order/types.d.ts.map +1 -1
  61. package/esm/assertron/assertron.d.ts.map +1 -1
  62. package/esm/assertron/assertron.js.map +1 -1
  63. package/esm/assertron/falsy.d.ts.map +1 -1
  64. package/esm/assertron/falsy.js.map +1 -1
  65. package/esm/assertron/isInstanceof.d.ts.map +1 -1
  66. package/esm/assertron/isInstanceof.js.map +1 -1
  67. package/esm/assertron/pathEqual.js.map +1 -1
  68. package/esm/assertron/rejects.d.ts.map +1 -1
  69. package/esm/assertron/rejects.js +3 -1
  70. package/esm/assertron/rejects.js.map +1 -1
  71. package/esm/assertron/repeat.d.ts.map +1 -1
  72. package/esm/assertron/repeat.js +3 -1
  73. package/esm/assertron/repeat.js.map +1 -1
  74. package/esm/assertron/resolves.d.ts.map +1 -1
  75. package/esm/assertron/resolves.js +3 -1
  76. package/esm/assertron/resolves.js.map +1 -1
  77. package/esm/assertron/satisfies.d.ts.map +1 -1
  78. package/esm/assertron/satisfies.js.map +1 -1
  79. package/esm/assertron/throws.d.ts.map +1 -1
  80. package/esm/assertron/throws.js +7 -3
  81. package/esm/assertron/throws.js.map +1 -1
  82. package/esm/assertron/truthy.d.ts.map +1 -1
  83. package/esm/assertron/truthy.js.map +1 -1
  84. package/esm/errors.d.ts.map +1 -1
  85. package/esm/errors.js.map +1 -1
  86. package/esm/types.d.ts.map +1 -1
  87. package/esm/utils/index.js.map +1 -1
  88. package/esm/utils/isErrorConstructor.js.map +1 -1
  89. package/esm/utils/notEqualMessage.js.map +1 -1
  90. package/esm/utils/notSatisfiedMessage.d.ts.map +1 -1
  91. package/esm/utils/notSatisfiedMessage.js +8 -3
  92. package/esm/utils/notSatisfiedMessage.js.map +1 -1
  93. package/esm/utils/notThrownMessage.js.map +1 -1
  94. package/esm/utils/promiseMessages.d.ts.map +1 -1
  95. package/esm/utils/promiseMessages.js +8 -4
  96. package/esm/utils/promiseMessages.js.map +1 -1
  97. package/esm/utils/unexpectedErrorMessage.d.ts.map +1 -1
  98. package/esm/utils/unexpectedErrorMessage.js +1 -2
  99. package/esm/utils/unexpectedErrorMessage.js.map +1 -1
  100. package/package.json +1 -1
  101. package/ts/assert-order/AssertOrder.ts +138 -143
  102. package/ts/assert-order/InvalidOrder.ts +22 -18
  103. package/ts/assert-order/StateMachine.ts +95 -88
  104. package/ts/assert-order/index.ts +0 -1
  105. package/ts/assert-order/internalInterfaces.ts +3 -3
  106. package/ts/assert-order/types.ts +5 -5
  107. package/ts/assertron/assertron.ts +38 -31
  108. package/ts/assertron/falsy.ts +1 -2
  109. package/ts/assertron/isInstanceof.ts +6 -3
  110. package/ts/assertron/pathEqual.ts +2 -2
  111. package/ts/assertron/rejects.ts +6 -4
  112. package/ts/assertron/repeat.ts +27 -21
  113. package/ts/assertron/resolves.ts +6 -4
  114. package/ts/assertron/satisfies.ts +27 -20
  115. package/ts/assertron/throws.ts +75 -54
  116. package/ts/assertron/truthy.ts +1 -4
  117. package/ts/errors.ts +7 -7
  118. package/ts/testUtils.ts +51 -54
  119. package/ts/types.ts +0 -1
  120. package/ts/utils/index.ts +1 -1
  121. package/ts/utils/isErrorConstructor.ts +1 -1
  122. package/ts/utils/notEqualMessage.ts +1 -1
  123. package/ts/utils/notSatisfiedMessage.ts +17 -13
  124. package/ts/utils/notThrownMessage.ts +1 -1
  125. package/ts/utils/promiseMessages.ts +12 -5
  126. 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>(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
  }
@@ -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> = 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
3
  import type { ErrorConstructor, ErrorValidator } from '../types.js'
4
- import { isErrorConstructor, notRejectedMessage, notThrownMessage, unexpectedErrorMessage } from '../utils/index.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
  }
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
- constructor(message: string, options: AssertionError.Options) {
10
- super('assertron', message, options)
11
- }
9
+ constructor(message: string, options: AssertionError.Options) {
10
+ super('assertron', message, options)
11
+ }
12
12
  }
13
13
 
14
14
  export namespace AssertionError {
15
- export type Options = RequiredPick<ModuleError.Options, 'ssf'>
15
+ export type Options = RequiredPick<ModuleError.Options, 'ssf'>
16
16
  }
17
17
 
18
18
  export class FailOnOccurrence extends AssertionError {
19
- constructor(public occurrence: number, public error: any, options: AssertionError.Options) {
20
- super(failOnOccurrence(occurrence, error), options)
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
- return new Promise((resolve, reject) => {
11
- setTimeout(() => {
12
- try {
13
- resolve(fn())
14
- }
15
- catch (err) {
16
- reject(err)
17
- }
18
- }, 1)
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
- return new Promise((resolve, reject) => {
24
- setTimeout(() => {
25
- try {
26
- let i = 0
27
- for (; i < fns.length - 1; i++) {
28
- fns[i]()
29
- }
30
- resolve(fns[i]())
31
- }
32
- catch (err) {
33
- reject(err)
34
- }
35
- }, 1)
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
- return Promise.all(fns.map(fn => runAsync(fn)))
38
+ return Promise.all(fns.map(fn => runAsync(fn)))
41
39
  }
42
40
 
43
-
44
41
  export function assertIsPromise(promiseError: Promise<any>) {
45
- t(isPromise(promiseError))
42
+ t(isPromise(promiseError))
46
43
  }
47
44
 
48
45
  export function assertIsError(err: Error) {
49
- t(isError(err))
46
+ t(isError(err))
50
47
  }
51
48
 
52
49
  export function assertThrows<E extends Error>(fn: AnyFunction, ErrorType?: ErrorConstructor<E>): E {
53
- try {
54
- fn()
55
- }
56
- catch (e: any) {
57
- if (ErrorType && !(e instanceof ErrorType)) {
58
- throw new Error(`thrown error ${e} is not of expected type ${ErrorType}`)
59
- }
60
- return e
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>(fn: AnyFunction, ErrorType?: ErrorConstructor<E>): Promise<E> {
66
- try {
67
- await fn()
68
- }
69
- catch (e: any) {
70
- // console.log('catch clause', e)
71
- if (ErrorType && !(e instanceof ErrorType)) {
72
- throw new Error(`thrown error ${e} is not of expected type ${ErrorType}`)
73
- }
74
- // console.log('returning e')
75
- return e
76
- }
77
- throw new Error(`${fn} does not throw as expected`)
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
- if (err.stack && RegExp(escapeRegExp(filename)).test(err.stack))
83
- throw new Error(`contains internal stack: \n${err.stack}`)
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
@@ -1,3 +1,2 @@
1
-
2
1
  export type ErrorValidator = (value: any) => boolean
3
2
  export type ErrorConstructor<E extends Error> = new (...args: any[]) => E
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
- return `Failed on ${occurrence} occurrence: ${tersify(error, { maxLength: Infinity })}`
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
- return Object.prototype.isPrototypeOf.call(Error.prototype, validator.prototype)
4
+ return Object.prototype.isPrototypeOf.call(Error.prototype, validator.prototype)
5
5
  }
@@ -1,3 +1,3 @@
1
1
  export function notEqualMessage(actual: any, expected: any) {
2
- return `Expected value to equal ${expected}, but received ${actual}`
2
+ return `Expected value to equal ${expected}, but received ${actual}`
3
3
  }
@@ -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
- return entries.map(e => {
6
- const path = formatPath(e.path)
7
- return `Expect ${path} to satisfy ${formatValue(e.expected)}, but received ${formatValue(e.actual)}`
8
- }).join('\n')
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
- if (entries.length === 0)
13
- return 'actual'
14
- const nodes = [...entries]
15
- if (isIndice(nodes[0])) {
16
- nodes.unshift('actual')
17
- }
18
- return nodes.map(n => isIndice(n) ? `[${n}]` : '.' + n).join('').slice(1)
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
- return typeof node === 'number'
26
+ return typeof node === 'number'
23
27
  }
24
28
 
25
29
  function formatValue(value: any) {
26
- return tersify(value)
30
+ return tersify(value)
27
31
  }
@@ -1,5 +1,5 @@
1
1
  import { tersify } from 'tersify'
2
2
 
3
3
  export function notThrownMessage(value: any) {
4
- return `Expect function to throw, but it returned '${tersify(value)}' instead.`
4
+ return `Expect function to throw, but it returned '${tersify(value)}' instead.`
5
5
  }
@@ -1,12 +1,19 @@
1
1
  import { tersify } from 'tersify'
2
2
 
3
3
  export function notRejectedMessage(value: any) {
4
- return `Expected promise to reject, but it resolves with ` + (typeof value === 'string'
5
- ? `'${value}'` : tersify(value, { maxLength: Infinity }))
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
- return `Expected promise to resolve, but it rejects with ` + (typeof error === 'string'
10
- ? `'${error}'` : error instanceof Error ? error : tersify(error, { maxLength: Infinity }))
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
- return `Unexpected error. Expecting '${isErrorConstructor(expected) ?
7
- expected.name : tersify(expected)}' but received ${tersify(actual)}`
6
+ return `Unexpected error. Expecting '${
7
+ isErrorConstructor(expected) ? expected.name : tersify(expected)
8
+ }' but received ${tersify(actual)}`
8
9
  }