@plugjs/expect5 0.4.4 → 0.4.5

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 (90) hide show
  1. package/dist/cli.mjs +2 -2
  2. package/dist/cli.mjs.map +1 -1
  3. package/dist/execution/executor.cjs.map +1 -1
  4. package/dist/execution/executor.d.ts +1 -1
  5. package/dist/execution/executor.mjs +1 -1
  6. package/dist/execution/executor.mjs.map +1 -1
  7. package/dist/execution/setup.cjs.map +1 -1
  8. package/dist/execution/setup.d.ts +1 -1
  9. package/dist/execution/setup.mjs +1 -1
  10. package/dist/execution/setup.mjs.map +1 -1
  11. package/dist/expectation/async.cjs +58 -45
  12. package/dist/expectation/async.cjs.map +1 -1
  13. package/dist/expectation/async.d.ts +53 -52
  14. package/dist/expectation/async.mjs +59 -42
  15. package/dist/expectation/async.mjs.map +1 -1
  16. package/dist/expectation/diff.cjs.map +1 -1
  17. package/dist/expectation/diff.mjs +6 -1
  18. package/dist/expectation/diff.mjs.map +1 -1
  19. package/dist/expectation/expect.cjs +11 -165
  20. package/dist/expectation/expect.cjs.map +2 -2
  21. package/dist/expectation/expect.d.ts +10 -112
  22. package/dist/expectation/expect.mjs +12 -207
  23. package/dist/expectation/expect.mjs.map +2 -2
  24. package/dist/expectation/expectations.cjs +549 -0
  25. package/dist/expectation/expectations.cjs.map +6 -0
  26. package/dist/expectation/expectations.d.ts +454 -0
  27. package/dist/expectation/expectations.mjs +530 -0
  28. package/dist/expectation/expectations.mjs.map +6 -0
  29. package/dist/expectation/include.cjs +43 -41
  30. package/dist/expectation/include.cjs.map +1 -1
  31. package/dist/expectation/include.d.ts +3 -19
  32. package/dist/expectation/include.mjs +43 -41
  33. package/dist/expectation/include.mjs.map +1 -1
  34. package/dist/expectation/matchers.cjs +350 -0
  35. package/dist/expectation/matchers.cjs.map +6 -0
  36. package/dist/expectation/matchers.d.ts +375 -0
  37. package/dist/expectation/matchers.mjs +328 -0
  38. package/dist/expectation/matchers.mjs.map +6 -0
  39. package/dist/expectation/print.cjs.map +1 -1
  40. package/dist/expectation/print.d.ts +2 -2
  41. package/dist/expectation/print.mjs.map +1 -1
  42. package/dist/expectation/types.cjs +17 -23
  43. package/dist/expectation/types.cjs.map +1 -1
  44. package/dist/expectation/types.d.ts +7 -51
  45. package/dist/expectation/types.mjs +17 -22
  46. package/dist/expectation/types.mjs.map +1 -1
  47. package/dist/globals.d.ts +2 -2
  48. package/dist/index.cjs +5 -5
  49. package/dist/index.cjs.map +1 -1
  50. package/dist/index.d.ts +3 -4
  51. package/dist/index.mjs +11 -12
  52. package/dist/index.mjs.map +1 -1
  53. package/dist/test.cjs +34 -5
  54. package/dist/test.cjs.map +1 -1
  55. package/dist/test.d.ts +2 -2
  56. package/dist/test.mjs +35 -6
  57. package/dist/test.mjs.map +1 -1
  58. package/package.json +2 -2
  59. package/src/cli.mts +1 -1
  60. package/src/execution/executor.ts +1 -3
  61. package/src/execution/setup.ts +1 -3
  62. package/src/expectation/async.ts +116 -145
  63. package/src/expectation/diff.ts +7 -3
  64. package/src/expectation/expect.ts +22 -362
  65. package/src/expectation/expectations.ts +971 -0
  66. package/src/expectation/include.ts +59 -75
  67. package/src/expectation/matchers.ts +698 -0
  68. package/src/expectation/print.ts +8 -4
  69. package/src/expectation/types.ts +22 -94
  70. package/src/globals.ts +2 -2
  71. package/src/index.ts +17 -10
  72. package/src/test.ts +34 -10
  73. package/dist/expectation/basic.cjs +0 -188
  74. package/dist/expectation/basic.cjs.map +0 -6
  75. package/dist/expectation/basic.d.ts +0 -90
  76. package/dist/expectation/basic.mjs +0 -156
  77. package/dist/expectation/basic.mjs.map +0 -6
  78. package/dist/expectation/throwing.cjs +0 -58
  79. package/dist/expectation/throwing.cjs.map +0 -6
  80. package/dist/expectation/throwing.d.ts +0 -36
  81. package/dist/expectation/throwing.mjs +0 -32
  82. package/dist/expectation/throwing.mjs.map +0 -6
  83. package/dist/expectation/trivial.cjs +0 -96
  84. package/dist/expectation/trivial.cjs.map +0 -6
  85. package/dist/expectation/trivial.d.ts +0 -13
  86. package/dist/expectation/trivial.mjs +0 -61
  87. package/dist/expectation/trivial.mjs.map +0 -6
  88. package/src/expectation/basic.ts +0 -413
  89. package/src/expectation/throwing.ts +0 -106
  90. package/src/expectation/trivial.ts +0 -107
@@ -1,413 +0,0 @@
1
- import { diff } from './diff'
2
- import {
3
- assertContextType,
4
- ExpectationError,
5
- prefixType,
6
- stringifyConstructor,
7
- stringifyValue,
8
- typeOf,
9
- } from './types'
10
-
11
- import type { Expectations } from './expect'
12
- import type {
13
- AssertedType,
14
- AssertionFunction,
15
- Constructor,
16
- ExpectationsContext,
17
- JoinExpectations,
18
- TypeMappings,
19
- TypeName,
20
- } from './types'
21
-
22
- /* === TO BE A ============================================================== */
23
-
24
- /** Expects the value to be of the specified {@link TypeName type}. */
25
- function toBeA<N extends TypeName>(type: N): Expectations<TypeMappings[N]>
26
-
27
- /**
28
- * Expects the value to be of the specified {@link TypeName type}, and further
29
- * asserts it with the specified callback.
30
- */
31
- function toBeA<T, N extends TypeName, A extends AssertionFunction<TypeMappings[N]>>(
32
- this: T,
33
- type: N,
34
- assert: A,
35
- ): Expectations<TypeMappings[N] & AssertedType<A>>
36
-
37
- /* Overloaded function implementation */
38
- function toBeA(
39
- this: ExpectationsContext,
40
- type: TypeName,
41
- assert?: AssertionFunction,
42
- ): Expectations {
43
- const match = typeOf(this.value) === type
44
- if (match === this.negative) {
45
- throw new ExpectationError(this, `to be ${prefixType(type)}`)
46
- } else if (assert) {
47
- assert(this.expects)
48
- }
49
- return this.expects
50
- }
51
-
52
- /* === TO BE CLOSE TO ======================================================= */
53
-
54
- /**
55
- * Expects the value to be a `number` within a given +/- delta range of the
56
- * specified `number`.
57
- */
58
- function toBeCloseTo(value: number, delta: number): Expectations<number>
59
-
60
- /**
61
- * Expects the value to be a a `bigint` within a given +/- delta range of the
62
- * specified `bigint`.
63
- */
64
- function toBeCloseTo(value: bigint, delta: bigint): Expectations<bigint>
65
-
66
- /* Overloaded function implementation */
67
- function toBeCloseTo(
68
- this: ExpectationsContext,
69
- value: number | bigint,
70
- delta: number | bigint,
71
- ): Expectations {
72
- const min = (value as number) - (delta as number)
73
- const max = (value as number) + (delta as number)
74
- return this.negated.toBeWithinRange(min, max)
75
- }
76
-
77
- /* === TO BE ERROR ========================================================== */
78
-
79
- /** Expects the value to be an {@link Error}. */
80
- function toBeError(): Expectations<Error>
81
-
82
- /** Expects the value to be an {@link Error} with the specified message. */
83
- function toBeError(message: string): Expectations<Error>
84
-
85
- /**
86
- * Expects the value to be an {@link Error} with its message matching the
87
- * specified {@link RegExp}.
88
- */
89
- function toBeError(matcher: RegExp): Expectations<Error>
90
-
91
- /** Expects the value to be an {@link Error} of the specified _type_. */
92
- function toBeError<C extends Constructor<Error>>(constructor: C): Expectations<InstanceType<C>>
93
-
94
- /**
95
- * Expects the value to be an {@link Error} of the specified _type_ with the
96
- * specified message
97
- */
98
- function toBeError<C extends Constructor<Error>>(constructor: C, message: string): Expectations<InstanceType<C>>
99
-
100
- /**
101
- * Expects the value to be an {@link Error} of the specified _type_ with the
102
- * specified message
103
- */
104
- function toBeError<C extends Constructor<Error>>(constructor: C, matcher: RegExp): Expectations<InstanceType<C>>
105
-
106
- /* Overloaded function implementation */
107
- function toBeError(
108
- this: ExpectationsContext,
109
- ...args:
110
- | []
111
- | [ message: string | RegExp ]
112
- | [ constructor: Constructor<Error> ]
113
- | [ constructor: Constructor<Error>, message: string | RegExp ]
114
- ): Expectations {
115
- const [ constructor, message ] =
116
- typeof args[0] === 'function' ?
117
- [ args[0], args[1] ] :
118
- [ Error, args[0] ]
119
-
120
- this.negated.toBeInstanceOf(constructor)
121
-
122
- // if "not.toBeError" ignore the message
123
- if (this.negative || (message === undefined)) return this.expects
124
-
125
- return this.expects.toHaveProperty('message', (assert) => {
126
- assert.toBeA('string')
127
- return typeof message === 'string' ?
128
- assert.toStrictlyEqual(message) :
129
- assert.toMatch(message)
130
- })
131
- }
132
-
133
-
134
- /* === TO BE GREATER THAN =================================================== */
135
-
136
- /** Expects the value to be a `number` greater than the specified `number`. */
137
- function toBeGreaterThan(value: number): Expectations<number>
138
- /** Expects the value to be a `bigint` greater than the specified `bigint`. */
139
- function toBeGreaterThan(value: bigint): Expectations<bigint>
140
-
141
- /* Overloaded function implementation */
142
- function toBeGreaterThan(
143
- this: ExpectationsContext,
144
- value: number | bigint,
145
- ): Expectations {
146
- assertContextType(this, typeof value as 'number' | 'bigint')
147
- if ((this.value > value) !== this.negative) return this.expects
148
- throw new ExpectationError(this, `to be greater than ${stringifyValue(value)}`)
149
- }
150
-
151
- /* === TO BE GREATER THAN OR EQUAL ========================================== */
152
-
153
- /** Expects the value to be a `number` greater than or equal to the specified `number`. */
154
- function toBeGreaterThanOrEqual(value: number): Expectations<number>
155
- /** Expects the value to be a `bigint` greater than or equal to the specified `bigint`. */
156
- function toBeGreaterThanOrEqual(value: bigint): Expectations<bigint>
157
-
158
- /* Overloaded function implementation */
159
- function toBeGreaterThanOrEqual(
160
- this: ExpectationsContext,
161
- value: number | bigint,
162
- ): Expectations {
163
- assertContextType(this, typeof value as 'number' | 'bigint')
164
- if ((this.value >= value) !== this.negative) return this.expects
165
- throw new ExpectationError(this, `to be greater than or equal to ${stringifyValue(value)}`)
166
- }
167
-
168
- /* === TO BE INSTANCE OF ==================================================== */
169
-
170
- /** Expects the value to be an instance of the specified _type_. */
171
- function toBeInstanceOf<C extends Constructor>(value: C): Expectations<InstanceType<C>>
172
-
173
- /* Overloaded function implementation */
174
- function toBeInstanceOf(
175
- this: ExpectationsContext,
176
- value: Constructor,
177
- ): Expectations {
178
- const match = this.value instanceof value
179
- if (match !== this.negative) return this.expects
180
- throw new ExpectationError(this, `to be an instance of ${stringifyConstructor(value)}`)
181
- }
182
-
183
- /* === TO BE LESS THAN ====================================================== */
184
-
185
- /** Expects the value to be a `number` less than the specified `number`. */
186
- function toBeLessThan(value: number): Expectations<number>
187
- /** Expects the value to be a `bigint` less than the specified `bigint`. */
188
- function toBeLessThan(value: bigint): Expectations<bigint>
189
-
190
- /* Overloaded function implementation */
191
- function toBeLessThan(
192
- this: ExpectationsContext,
193
- value: number | bigint,
194
- ): Expectations {
195
- assertContextType(this, typeof value as 'number' | 'bigint')
196
- if ((this.value < value) !== this.negative) return this.expects
197
- throw new ExpectationError(this, `to be less than ${stringifyValue(value)}`)
198
- }
199
-
200
- /* === TO BE LESS THAN OR EQUAL ============================================= */
201
-
202
- /** Expects the value to be a `number` less than or equal to the specified `number`. */
203
- function toBeLessThanOrEqual(value: number): Expectations<number>
204
- /** Expects the value to be a `bigint` less than or equal to the specified `bigint`. */
205
- function toBeLessThanOrEqual(value: bigint): Expectations<bigint>
206
-
207
- /* Overloaded function implementation */
208
- function toBeLessThanOrEqual(
209
- this: ExpectationsContext,
210
- value: number | bigint,
211
- ): Expectations {
212
- assertContextType(this, typeof value as 'number' | 'bigint')
213
- if ((this.value <= value) !== this.negative) return this.expects
214
- throw new ExpectationError(this, `to be less than or equal to ${stringifyValue(value)}`)
215
- }
216
-
217
- /* === TO WITHIN RANGE ====================================================== */
218
-
219
- /** Expects the value to be a `number` within the specified range (inclusive). */
220
- function toBeWithinRange(min: number, max: number): Expectations<number>
221
- /** Expects the value to be a `bigint` within the specified range (inclusive). */
222
- function toBeWithinRange(min: bigint, max: bigint): Expectations<bigint>
223
-
224
- /* Overloaded function implementation */
225
- function toBeWithinRange(
226
- this: ExpectationsContext,
227
- min: number | bigint,
228
- max: number | bigint,
229
- ): Expectations {
230
- if (max < min) {
231
- const num = max
232
- max = min
233
- min = num
234
- }
235
-
236
- assertContextType(this, typeof min as 'number' | 'bigint')
237
- if (((this.value >= min) && (this.value <= max)) !== this.negative) return this.expects
238
- throw new ExpectationError(this, `to be within ${stringifyValue(min)}...${stringifyValue(max)}`)
239
- }
240
-
241
- /* === TO EQUAL ============================================================= */
242
-
243
- /** Expects the value to _deeply equal_ to the specified expected value. */
244
- function toEqual<T>(expected: T): Expectations<T>
245
-
246
- /* Overloaded function implementation */
247
- function toEqual(
248
- this: ExpectationsContext,
249
- expected: any,
250
- ): Expectations {
251
- const result = diff(this.value, expected)
252
- if (result.diff === this.negative) return this.expects
253
- throw new ExpectationError(this, `to equal ${stringifyValue(expected)}`, result)
254
- }
255
-
256
- /* === TO HAVE LENGTH ======================================================= */
257
-
258
- /** Expects the value to have a numerical `length` property with the specified value. */
259
- function toHaveLength<T, N extends number>(this: T, length: N): JoinExpectations<T, { length: N }>
260
-
261
- /* Overloaded function implementation */
262
- function toHaveLength(
263
- this: ExpectationsContext,
264
- length: number,
265
- ): Expectations {
266
- this.expects.toBeDefined()
267
-
268
- const actualLength = (this.value as any).length
269
- if (typeof actualLength !== 'number') {
270
- throw new ExpectationError(this, 'to have a numeric "length" property', false)
271
- }
272
-
273
- if ((actualLength === length) === this.negative) {
274
- throw new ExpectationError(this, `to have length ${stringifyValue(length)}`)
275
- }
276
-
277
- return this.expects
278
- }
279
-
280
- /* === TO HAVE PROPERTY ===================================================== */
281
-
282
- /** Expects the value to have a property. */
283
- function toHaveProperty<T, P extends string | number | symbol>(
284
- this: T,
285
- prop: P,
286
- ): JoinExpectations<T, { [key in P]: unknown }>
287
-
288
- /**
289
- * Expects the value to have a property, and further asserts the property
290
- * value with the specified callback.
291
- */
292
- function toHaveProperty<T, P extends string | number | symbol, A extends AssertionFunction>(
293
- this: T,
294
- prop: P,
295
- assert: A,
296
- ): JoinExpectations<T, { [key in P]: AssertedType<A> }>
297
-
298
- /* Overloaded function implementation */
299
- function toHaveProperty(
300
- this: ExpectationsContext,
301
- prop: string | symbol | number,
302
- assert?: AssertionFunction,
303
- ): Expectations {
304
- this.expects.toBeDefined()
305
-
306
- const match = (this.value as any)[prop] !== undefined
307
- if (match === this.negative) {
308
- throw new ExpectationError(this, `to have property "${String(prop)}"`)
309
- } else if (match && assert) {
310
- try {
311
- assert(this.forProperty(prop))
312
- } catch (error) {
313
- // any caught error difference gets remapped as a property diff
314
- if ((error instanceof ExpectationError) && (error.diff)) {
315
- error.diff = {
316
- diff: true,
317
- value: this.value,
318
- props: { [prop]: error.diff },
319
- }
320
- }
321
-
322
- // re-throw
323
- throw error
324
- }
325
- }
326
-
327
- return this.expects
328
- }
329
-
330
-
331
- /* === TO HAVE SIZE ========================================================= */
332
-
333
- /** Expects the value to have a numerical `size` property with the specified value. */
334
- function toHaveSize<T, N extends number>(this: T, size: N): JoinExpectations<T, { size: N }>
335
-
336
- /* Overloaded function implementation */
337
- function toHaveSize(
338
- this: ExpectationsContext,
339
- size: number,
340
- ): Expectations {
341
- this.expects.toBeDefined()
342
-
343
- const actualSize = (this.value as any).size
344
- if (typeof actualSize !== 'number') {
345
- throw new ExpectationError(this, 'to have a numeric "size" property', false)
346
- }
347
-
348
- if ((actualSize === size) === this.negative) {
349
- throw new ExpectationError(this, `to have size ${stringifyValue(size)}`)
350
- }
351
-
352
- return this.expects
353
- }
354
-
355
- /* === TO MATCH ============================================================= */
356
-
357
- /** Expects the value to be a `string` _matching_ the specified substring. */
358
- function toMatch(substring: string): Expectations<string>
359
- /** Expects the value to be a `string` _matching_ the specified {@link RegExp}. */
360
- function toMatch(expression: RegExp): Expectations<string>
361
-
362
- /* Overloaded function implementation */
363
- function toMatch(
364
- this: ExpectationsContext,
365
- expr: string | RegExp,
366
- ): Expectations {
367
- assertContextType(this, 'string')
368
-
369
- const match = !! this.value.match(expr)
370
- if (match !== this.negative) return this.expects
371
-
372
- throw new ExpectationError(this, `to match ${stringifyValue(expr)}`)
373
- }
374
-
375
-
376
- /* === TO STRICTLY EQUAL ==================================================== */
377
-
378
- /** Expects the value to _strictly equal_ to the specified expected value. */
379
- function toStrictlyEqual<T>(expected: T): Expectations<T>
380
-
381
- /* Overloaded function implementation */
382
- function toStrictlyEqual(
383
- this: ExpectationsContext,
384
- expected: any,
385
- ): Expectations {
386
- // const value = context.value
387
- const match = this.value === expected
388
- if (match !== this.negative) return this.expects
389
-
390
- const diff = this.negative ? undefined : { diff: true, value: this.value, expected }
391
- throw new ExpectationError(this, `to strictly equal ${stringifyValue(expected)}`, diff)
392
- }
393
-
394
- /* === EXPORTS ============================================================== */
395
-
396
- /* coverage ignore next */
397
- export {
398
- toBeA,
399
- toBeCloseTo,
400
- toBeError,
401
- toBeGreaterThan,
402
- toBeGreaterThanOrEqual,
403
- toBeInstanceOf,
404
- toBeLessThan,
405
- toBeLessThanOrEqual,
406
- toBeWithinRange,
407
- toEqual,
408
- toHaveLength,
409
- toHaveProperty,
410
- toHaveSize,
411
- toMatch,
412
- toStrictlyEqual,
413
- }
@@ -1,106 +0,0 @@
1
- import { ExpectationError, assertContextType } from './types'
2
-
3
- import type { Expectations } from './expect'
4
- import type {
5
- AssertionFunction,
6
- Constructor,
7
- ExpectationsContext,
8
- JoinExpectations,
9
- } from './types'
10
-
11
- /* === TO THROW ============================================================= */
12
-
13
- /** Expects the value to be a `function` throwing _anything_. */
14
- function toThrow<T>(this: T): JoinExpectations<T, Function>
15
-
16
- /**
17
- * Expects the value to be a `function` throwing, and asserts the
18
- * thrown value with the specified callback.
19
- */
20
- function toThrow<T>(this: T, assert: AssertionFunction): JoinExpectations<T, Function>
21
-
22
- /* Overloaded function implementation */
23
- function toThrow(
24
- this: ExpectationsContext,
25
- assert?: AssertionFunction,
26
- ): Expectations {
27
- assertContextType(this, 'function')
28
-
29
- let thrown: boolean
30
- let error: unknown
31
- try {
32
- this.value()
33
- thrown = false
34
- error = undefined
35
- } catch (caught) {
36
- thrown = true
37
- error = caught
38
- }
39
-
40
- if (thrown === this.negative) {
41
- throw new ExpectationError(this, 'to throw')
42
- } else if (thrown && assert) {
43
- assert(this.forValue(error))
44
- }
45
-
46
- return this.expects
47
- }
48
-
49
- /* === TO THROW ERROR ======================================================= */
50
-
51
- /** Expects the value to be a `function` throwing an {@link Error}. */
52
- function toThrowError<T>(this: T): JoinExpectations<T, Function>
53
-
54
- /**
55
- * Expects the value to be a `function` throwing an {@link Error} with the
56
- * specified _message_.
57
- */
58
- function toThrowError<T>(this: T, message: string): JoinExpectations<T, Function>
59
-
60
- /**
61
- * Expects the value to be a `function` throwing an {@link Error} with its
62
- * _message_ matching the specified {@link RegExp}.
63
- */
64
- function toThrowError<T>(this: T, expession: RegExp): JoinExpectations<T, Function>
65
-
66
- /**
67
- * Expects the value to be a `function` throwing an {@link Error} of the
68
- * specified _type_.
69
- */
70
- function toThrowError<T>(this: T, constructor: Constructor<Error>): JoinExpectations<T, Function>
71
-
72
- /**
73
- * Expects the value to be a `function` throwing an {@link Error} of the
74
- * specified _type_ with the specified _message_.
75
- */
76
- function toThrowError<T>(this: T, constructor: Constructor<Error>, message: string): JoinExpectations<T, Function>
77
-
78
- /**
79
- * Expects the value to be a `function` throwing an {@link Error} of the
80
- * specified _type_ with its _message_ matching the specified {@link RegExp}.
81
- */
82
- function toThrowError<T>(this: T, constructor: Constructor<Error>, expression: RegExp): JoinExpectations<T, Function>
83
-
84
- /* Overloaded function implementation */
85
- function toThrowError(
86
- this: ExpectationsContext,
87
- ...args:
88
- | []
89
- | [ string ]
90
- | [ RegExp ]
91
- | [ Constructor<Error> ]
92
- | [ Constructor<Error>, string ]
93
- | [ Constructor<Error>, RegExp ]
94
- ): Expectations {
95
- return this.negated.toThrow((assert) =>
96
- // @ts-ignore // can't reconcile the types with overloads...
97
- assert.toBeError(...args))
98
- }
99
-
100
- /* === EXPORTS ============================================================== */
101
-
102
- /* coverage ignore next */
103
- export {
104
- toThrow,
105
- toThrowError,
106
- }
@@ -1,107 +0,0 @@
1
- import { ExpectationError, stringifyValue } from './types'
2
-
3
- import type { Expectations } from './expect'
4
- import type { ExpectationsContext } from './types'
5
-
6
- /* Expects the value to be _defined_ (that is not `null` nor `undefined`). */
7
- function toBeDefined<T>(this: T): T
8
- function toBeDefined(this: ExpectationsContext): Expectations {
9
- return check(this, 'to be defined', (value) => (value !== null) && (value !== undefined))
10
- }
11
-
12
- /* Expects the value strictly equal to `false`. */
13
- function toBeFalse(): Expectations<false>
14
- function toBeFalse(this: ExpectationsContext): Expectations {
15
- return check(this, `to be ${stringifyValue(false)}`, (value) => value === false)
16
- }
17
-
18
- /* Expects the value to be _falsy_ (zero, empty string, `false`, ...). */
19
- function toBeFalsy<T>(this: T): T
20
- function toBeFalsy(this: ExpectationsContext): Expectations {
21
- return check(this, 'to be falsy', (value) => ! value)
22
- }
23
-
24
- /* Expects the value to be `NaN`. */
25
- function toBeNaN(): Expectations<number>
26
- function toBeNaN(this: ExpectationsContext): Expectations {
27
- return check(this, `to be ${stringifyValue(NaN)}`, (value) => (typeof value === 'number') && isNaN(value))
28
- }
29
-
30
- /* Expects the value to strictly equal to `-Infinity` (negative infinity). */
31
- function toBeNegativeInfinity(): Expectations<number>
32
- function toBeNegativeInfinity(this: ExpectationsContext): Expectations {
33
- return check(this, `to equal ${stringifyValue(Number.NEGATIVE_INFINITY)}`, (value) => value === Number.NEGATIVE_INFINITY)
34
- }
35
-
36
- /* Expects the value to strictly equal to `null`. */
37
- function toBeNull(): Expectations<null>
38
- function toBeNull(this: ExpectationsContext): Expectations {
39
- return check(this, `to be ${stringifyValue(null)}`, (value) => value === null)
40
- }
41
-
42
- /* Expects the value to strictly equal to `null` or `undefined`. */
43
- function toBeNullable(): Expectations<null | undefined>
44
- function toBeNullable(this: ExpectationsContext): Expectations {
45
- return check(
46
- this,
47
- `to be ${stringifyValue(null)} or ${stringifyValue(undefined)}`,
48
- (value) => ((value === null) || (value === undefined)))
49
- }
50
-
51
- /* Expects the value to strictly equal to `+Infinity` (positive infinity). */
52
- function toBePositiveInfinity(): Expectations<number>
53
- function toBePositiveInfinity(this: ExpectationsContext): Expectations {
54
- return check(this, `to equal ${stringifyValue(Number.POSITIVE_INFINITY)}`, (value) => value === Number.POSITIVE_INFINITY)
55
- }
56
-
57
- /* Expects the value to strictly equal to `true`. */
58
- function toBeTrue(): Expectations<true>
59
- function toBeTrue(this: ExpectationsContext): Expectations {
60
- return check(this, `to be ${stringifyValue(true)}`, (value) => value === true)
61
- }
62
-
63
- /* Expects the value to be _falsy_ (non-zero, non-empty string, `true`, ...). */
64
- function toBeTruthy<T>(this: T): T
65
- function toBeTruthy(this:ExpectationsContext): Expectations {
66
- return check(this, 'to be truthy', (value) => !! value)
67
- }
68
-
69
- /* Expects the value to strictly equal to `undefined`. */
70
- function toBeUndefined(): Expectations<undefined>
71
- function toBeUndefined(this: ExpectationsContext): Expectations {
72
- return check(this, `to be ${stringifyValue(undefined)}`, (value) => value === undefined)
73
- }
74
-
75
- /* === EXPORTS ============================================================== */
76
-
77
- /* coverage ignore next */
78
- export {
79
- toBeDefined,
80
- toBeFalse,
81
- toBeFalsy,
82
- toBeNaN,
83
- toBeNegativeInfinity,
84
- toBeNull,
85
- toBeNullable,
86
- toBePositiveInfinity,
87
- toBeTrue,
88
- toBeTruthy,
89
- toBeUndefined,
90
- }
91
-
92
- /* ========================================================================== *
93
- * INTERNALS *
94
- * ========================================================================== */
95
-
96
- function check(
97
- context: ExpectationsContext,
98
- details: string,
99
- cb: (value: unknown) => boolean,
100
- ): Expectations {
101
- const match = cb(context.value)
102
- if (match === context.negative) {
103
- throw new ExpectationError(context, details)
104
- } else {
105
- return context.expects
106
- }
107
- }