-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
jest-expect.ts
78 lines (75 loc) · 2.7 KB
/
jest-expect.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
import { ChaiPlugin } from './types'
// Jest Expect Compact
// TODO: add more https://jestjs.io/docs/expect
export function JestChaiExpect(): ChaiPlugin {
return (chai, utils) => {
const proto = chai.Assertion.prototype
utils.addMethod(proto, 'toEqual', function(this: Chai.Assertion, expected: any) {
return this.eql(expected)
})
utils.addMethod(proto, 'toStrictEqual', function(this: Chai.Assertion, expected: any) {
return this.equal(expected)
})
utils.addMethod(proto, 'toBe', function(this: Chai.Assertion, expected: any) {
return this.equal(expected)
})
utils.addMethod(proto, 'toMatchObject', function(this: Chai.Assertion, expected: any) {
return this.containSubset(expected)
})
utils.addMethod(proto, 'toMatch', function(this: Chai.Assertion, expected: string | RegExp) {
if (typeof expected === 'string')
return this.include(expected)
else
return this.match(expected)
})
utils.addMethod(proto, 'toContain', function(this: Chai.Assertion, item: any) {
return this.contain(item)
})
utils.addMethod(proto, 'toBeTruthy', function(this: Chai.AssertionStatic) {
const obj = utils.flag(this, 'object')
this.assert(
Boolean(obj),
'expected #{this} to be truthy',
'expected #{this} to not be truthy',
obj,
)
})
utils.addMethod(proto, 'toBeFalsy', function(this: Chai.AssertionStatic) {
const obj = utils.flag(this, 'object')
this.assert(
!obj,
'expected #{this} to be falsy',
'expected #{this} to not be falsy',
obj,
)
})
utils.addMethod(proto, 'toBeNaN', function(this: Chai.Assertion) {
return this.be.NaN
})
utils.addMethod(proto, 'toBeUndefined', function(this: Chai.Assertion) {
return this.be.undefined
})
utils.addMethod(proto, 'toBeNull', function(this: Chai.Assertion) {
return this.be.null
})
utils.addMethod(proto, 'toBeDefined', function(this: Chai.Assertion) {
return this.not.be.undefined
})
utils.addMethod(proto, 'toBeInstanceOf', function(this: Chai.Assertion, obj: any) {
return this.instanceOf(obj)
})
// mock
utils.addMethod(proto, 'toHaveBeenCalledTimes', function(this: Chai.Assertion, number: number) {
return this.callCount(number)
})
utils.addMethod(proto, 'toHaveBeenCalledOnce', function(this: Chai.Assertion) {
return this.callCount(1)
})
utils.addMethod(proto, 'toHaveBeenCalled', function(this: Chai.Assertion) {
return this.called
})
utils.addMethod(proto, 'toHaveBeenCalledWith', function(this: Chai.Assertion, ...args: any[]) {
return this.calledWith(...args)
})
}
}