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