-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
jest-expect.test.ts
464 lines (402 loc) · 13.1 KB
/
jest-expect.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* eslint-disable comma-spacing */
/* eslint-disable no-sparse-arrays */
import { describe, expect, it } from 'vitest'
class TestError extends Error {}
// For expect.extend
interface CustomMatchers<R = unknown> {
toBeDividedBy(divisor: number): R
}
declare global {
namespace Vi {
interface JestAssertion extends CustomMatchers {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
}
describe('jest-expect', () => {
it('basic', () => {
expect(1).toBe(1)
expect(null).toBeNull()
expect(1).not.toBeNull()
expect(null).toBeDefined()
expect(undefined).not.toBeDefined()
expect(undefined).toBeUndefined()
expect(null).not.toBeUndefined()
expect([]).toBeTruthy()
expect(0).toBeFalsy()
expect('Hello').toMatch(/llo/)
expect('Hello').toMatch('llo')
expect('Hello').toContain('llo')
expect(['Hello']).toContain('Hello')
expect([{ text: 'Hello' }]).toContainEqual({ text: 'Hello' })
expect([{ text: 'Bye' }]).not.toContainEqual({ text: 'Hello' })
expect(1).toBeGreaterThan(0)
expect(BigInt(1)).toBeGreaterThan(BigInt(0))
expect(1).toBeGreaterThan(BigInt(0))
expect(BigInt(1)).toBeGreaterThan(0)
expect(1).toBeGreaterThanOrEqual(1)
expect(1).toBeGreaterThanOrEqual(0)
expect(BigInt(1)).toBeGreaterThanOrEqual(BigInt(1))
expect(BigInt(1)).toBeGreaterThanOrEqual(BigInt(0))
expect(BigInt(1)).toBeGreaterThanOrEqual(1)
expect(1).toBeGreaterThanOrEqual(BigInt(1))
expect(0).toBeLessThan(1)
expect(BigInt(0)).toBeLessThan(BigInt(1))
expect(BigInt(0)).toBeLessThan(1)
expect(1).toBeLessThanOrEqual(1)
expect(0).toBeLessThanOrEqual(1)
expect(BigInt(1)).toBeLessThanOrEqual(BigInt(1))
expect(BigInt(0)).toBeLessThanOrEqual(BigInt(1))
expect(BigInt(1)).toBeLessThanOrEqual(1)
expect(1).toBeLessThanOrEqual(BigInt(1))
expect(() => {
throw new Error('this is the error message')
}).toThrow('this is the error message')
expect(() => {}).not.toThrow()
expect(() => {
throw new TestError('error')
}).toThrow(TestError)
const err = new Error('hello world')
expect(() => {
throw err
}).toThrow(err)
expect(() => {
throw new Error('message')
}).toThrow(expect.objectContaining({
message: expect.stringContaining('mes'),
}))
expect([1, 2, 3]).toHaveLength(3)
expect('abc').toHaveLength(3)
expect('').not.toHaveLength(5)
expect({ length: 3 }).toHaveLength(3)
expect(0.2 + 0.1).not.toBe(0.3)
expect(0.2 + 0.1).toBeCloseTo(0.3, 5)
expect(0.2 + 0.1).not.toBeCloseTo(0.3, 100) // expect.closeTo will fail in chai
})
it('asymmetric matchers (jest style)', () => {
expect({ foo: 'bar' }).toEqual({ foo: expect.stringContaining('ba') })
expect('bar').toEqual(expect.stringContaining('ba'))
expect(['bar']).toEqual([expect.stringContaining('ba')])
expect(new Set(['bar'])).toEqual(new Set([expect.stringContaining('ba')]))
expect({ foo: 'bar' }).not.toEqual({ foo: expect.stringContaining('zoo') })
expect('bar').not.toEqual(expect.stringContaining('zoo'))
expect(['bar']).not.toEqual([expect.stringContaining('zoo')])
expect({ foo: 'bar', bar: 'foo', hi: 'hello' }).toEqual({
foo: expect.stringContaining('ba'),
bar: expect.stringContaining('fo'),
hi: 'hello',
})
expect(0).toEqual(expect.anything())
expect({}).toEqual(expect.anything())
expect('string').toEqual(expect.anything())
expect(null).not.toEqual(expect.anything())
expect(undefined).not.toEqual(expect.anything())
expect({ a: 0, b: 0 }).toEqual(expect.objectContaining({ a: 0 }))
expect({ a: 0, b: 0 }).not.toEqual(expect.objectContaining({ z: 0 }))
expect(0).toEqual(expect.any(Number))
expect('string').toEqual(expect.any(String))
expect('string').not.toEqual(expect.any(Number))
expect(['Bob', 'Eve']).toEqual(expect.arrayContaining(['Bob']))
expect(['Bob', 'Eve']).not.toEqual(expect.arrayContaining(['Mohammad']))
expect([
{ name: 'Bob' },
{ name: 'Eve' },
]).toEqual(expect.arrayContaining<{ name: string }>([
{ name: 'Bob' },
]))
expect([
{ name: 'Bob' },
{ name: 'Eve' },
]).not.toEqual(expect.arrayContaining<{ name: string }>([
{ name: 'Mohammad' },
]))
expect('Mohammad').toEqual(expect.stringMatching(/Moh/))
expect('Mohammad').not.toEqual(expect.stringMatching(/jack/))
// TODO: support set
// expect(new Set(['bar'])).not.toEqual(new Set([expect.stringContaining('zoo')]))
})
it('asymmetric matchers negate', () => {
expect('bar').toEqual(expect.not.stringContaining('zoo'))
expect('bar').toEqual(expect.not.stringMatching(/zoo/))
expect({ bar: 'zoo' }).toEqual(expect.not.objectContaining({ zoo: 'bar' }))
expect(['Bob', 'Eve']).toEqual(expect.not.arrayContaining(['Steve']))
})
it('expect.extend', () => {
expect.extend({
toBeDividedBy(received, divisor) {
const pass = received % divisor === 0
if (pass) {
return {
message: () =>
`expected ${received} not to be divisible by ${divisor}`,
pass: true,
}
}
else {
return {
message: () =>
`expected ${received} to be divisible by ${divisor}`,
pass: false,
}
}
},
})
expect(5).toBeDividedBy(5)
expect(5).not.toBeDividedBy(4)
expect({ one: 1, two: 2 }).toEqual({
one: expect.toBeDividedBy(1),
two: expect.not.toBeDividedBy(5),
})
})
it('object', () => {
expect({}).toEqual({})
expect({ apples: 13 }).toEqual({ apples: 13 })
expect({}).toStrictEqual({})
expect({}).not.toBe({})
const foo = {}
const complex = {
'foo': 1,
'foo.bar[0]': 'baz',
'bar': {
foo: 'foo',
bar: 100,
arr: ['first', { zoo: 'monkey' }],
},
}
expect(foo).toBe(foo)
expect(foo).toStrictEqual(foo)
expect(complex).toMatchObject({})
expect(complex).toMatchObject({ foo: 1 })
expect([complex]).toMatchObject([{ foo: 1 }])
expect(complex).not.toMatchObject({ foo: 2 })
expect(complex).toMatchObject({ bar: { bar: 100 } })
expect(complex).toMatchObject({ foo: expect.any(Number) })
expect(complex).toHaveProperty('foo')
expect(complex).toHaveProperty('foo', 1)
expect(complex).toHaveProperty('bar.foo', 'foo')
expect(complex).toHaveProperty('bar.arr[0]')
expect(complex).toHaveProperty('bar.arr[1].zoo', 'monkey')
expect(complex).toHaveProperty('bar.arr.0')
expect(complex).toHaveProperty('bar.arr.1.zoo', 'monkey')
expect(complex).toHaveProperty(['bar', 'arr', '1', 'zoo'], 'monkey')
expect(complex).toHaveProperty(['foo.bar[0]'], 'baz')
})
it('assertions', () => {
expect(1).toBe(1)
expect(1).toBe(1)
expect(1).toBe(1)
expect.assertions(3)
})
it('assertions with different order', () => {
expect.assertions(3)
expect(1).toBe(1)
expect(1).toBe(1)
expect(1).toBe(1)
})
it('assertions when asynchronous code', async() => {
expect.assertions(3)
await Promise.all([
expect(1).toBe(1),
expect(1).toBe(1),
expect(1).toBe(1),
])
})
it.fails('assertions when asynchronous code', async() => {
// Error: expected number of assertions to be 2, but got 3
expect.assertions(2)
await Promise.all([
expect(1).toBe(1),
expect(1).toBe(1),
expect(1).toBe(1),
])
})
it.fails('has assertions', () => {
expect.hasAssertions()
})
it('has assertions', () => {
expect(1).toBe(1)
expect.hasAssertions()
})
it('has assertions with different order', () => {
expect.hasAssertions()
expect(1).toBe(1)
})
it.fails('toBe with null/undefined values', () => {
expect(undefined).toBe(true)
expect(null).toBe(true)
})
// https://jestjs.io/docs/expect#tostrictequalvalue
class LaCroix {
constructor(public flavor: any) {}
}
describe('the La Croix cans on my desk', () => {
it('are not semantically the same', () => {
expect(new LaCroix('lemon')).toEqual({ flavor: 'lemon' })
expect(new LaCroix('lemon')).not.toStrictEqual({ flavor: 'lemon' })
})
})
it('array', () => {
expect([]).toEqual([])
expect([]).not.toBe([])
expect([]).toStrictEqual([])
const foo: any[] = []
expect(foo).toBe(foo)
expect(foo).toStrictEqual(foo)
const complex = [
{
foo: 1,
bar: { foo: 'foo', bar: 100, arr: ['first', { zoo: 'monkey' }] },
},
]
expect(complex).toStrictEqual([
{
foo: 1,
bar: { foo: 'foo', bar: 100, arr: ['first', { zoo: 'monkey' }] },
},
])
})
})
describe('.toStrictEqual()', () => {
class TestClassA {
constructor(public a: any, public b: any) {}
}
class TestClassB {
constructor(public a: any, public b: any) {}
}
const TestClassC = class Child extends TestClassA {
constructor(a: any, b: any) {
super(a, b)
}
}
const TestClassD = class Child extends TestClassB {
constructor(a: any, b: any) {
super(a, b)
}
}
it('does not ignore keys with undefined values', () => {
expect({
a: undefined,
b: 2,
}).not.toStrictEqual({ b: 2 })
})
it('does not ignore keys with undefined values inside an array', () => {
expect([{ a: undefined }]).not.toStrictEqual([{}])
})
it('does not ignore keys with undefined values deep inside an object', () => {
expect([{ a: [{ a: undefined }] }]).not.toStrictEqual([{ a: [{}] }])
})
it('does not consider holes as undefined in sparse arrays', () => {
expect([, , , 1, , ,]).not.toStrictEqual([, , , 1, undefined, ,])
})
it('passes when comparing same type', () => {
expect({
test: new TestClassA(1, 2),
}).toStrictEqual({ test: new TestClassA(1, 2) })
})
it('does not pass for different types', () => {
expect({
test: new TestClassA(1, 2),
}).not.toStrictEqual({ test: new TestClassB(1, 2) })
})
it('does not simply compare constructor names', () => {
const c = new TestClassC(1, 2)
const d = new TestClassD(1, 2)
expect(c.constructor.name).toEqual(d.constructor.name)
expect({ test: c }).not.toStrictEqual({ test: d })
})
it('passes for matching sparse arrays', () => {
expect([, 1]).toStrictEqual([, 1])
})
it('does not pass when sparseness of arrays do not match', () => {
expect([, 1]).not.toStrictEqual([undefined, 1])
expect([undefined, 1]).not.toStrictEqual([, 1])
expect([, , , 1]).not.toStrictEqual([, 1])
})
it('does not pass when equally sparse arrays have different values', () => {
expect([, 1]).not.toStrictEqual([, 2])
})
it('does not pass when ArrayBuffers are not equal', () => {
expect(Uint8Array.from([1, 2]).buffer).not.toStrictEqual(
Uint8Array.from([0, 0]).buffer,
)
expect(Uint8Array.from([2, 1]).buffer).not.toStrictEqual(
Uint8Array.from([2, 2]).buffer,
)
expect(Uint8Array.from([]).buffer).not.toStrictEqual(
Uint8Array.from([1]).buffer,
)
})
it('passes for matching buffers', () => {
expect(Uint8Array.from([1]).buffer).toStrictEqual(
Uint8Array.from([1]).buffer,
)
expect(Uint8Array.from([]).buffer).toStrictEqual(
Uint8Array.from([]).buffer,
)
expect(Uint8Array.from([9, 3]).buffer).toStrictEqual(
Uint8Array.from([9, 3]).buffer,
)
})
})
describe('toBeTypeOf()', () => {
it.each([
[1n, 'bigint'],
[true, 'boolean'],
[false, 'boolean'],
[() => {}, 'function'],
[function() {}, 'function'],
[1, 'number'],
[Infinity, 'number'],
[NaN, 'number'],
[0, 'number'],
[{}, 'object'],
[[], 'object'],
[null, 'object'],
['', 'string'],
['test', 'string'],
[Symbol('test'), 'symbol'],
[undefined, 'undefined'],
] as const)('pass with typeof %s === %s', (actual, expected) => {
expect(actual).toBeTypeOf(expected)
})
it('pass with negotiation', () => {
expect('test').not.toBeTypeOf('number')
})
})
describe('async expect', () => {
it('resolves', async() => {
await expect((async() => 'true')()).resolves.toBe('true')
await expect((async() => 'true')()).resolves.not.toBe('true22')
})
it.fails('failed to resolve', async() => {
await expect((async() => {
throw new Error('err')
})()).resolves.toBe('true')
})
it('rejects', async() => {
await expect((async() => {
throw new Error('err')
})()).rejects.toStrictEqual(new Error('err'))
await expect((async() => {
throw new Error('err')
})()).rejects.toThrow('err')
expect((async() => {
throw new TestError('error')
})()).rejects.toThrow(TestError)
const err = new Error('hello world')
expect((async() => {
throw err
})()).rejects.toThrow(err)
expect((async() => {
throw new Error('message')
})()).rejects.toThrow(expect.objectContaining({
message: expect.stringContaining('mes'),
}))
await expect((async() => {
throw new Error('err')
})()).rejects.not.toStrictEqual(new Error('fake err'))
})
it.fails('failed to reject', async() => {
await expect((async() => 'test')()).rejects.toBe('test')
})
})
it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))