-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjasmine_assertions.javascript.txt
172 lines (127 loc) · 9.26 KB
/
jasmine_assertions.javascript.txt
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
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ JASMINE ASSERTIONS ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━┛
ALTERNATIVES ==> #See chai
VERSION ==> #Part of Jasmine (see its doc)
#Generic assertions
┌────────────┐
│ CONFIG │
└────────────┘
--fail-spec-with-no-expectations
CONF.failSpecWithNoExpectations #BOOL (def: false). Do not allow tests with no assertions
JASMINE.debugLog(STR) #Like console.log() if test succeeds, console.trace() if fails
┌─────────────┐
│ GENERIC │
└─────────────┘
expect(VAL) #EXPECT
EXPECT.withContext('MESSAGE') #Add ': MESSAGE' to error message
throwUnless[Async](VAL) #Like expect[Async](VAL) but throws directly
#Meant for integration with other libraries like `testing-library`
EXPECT.not... #Invert assertion
EXPECT.nothing() #Noop
EXPECT.toBe(VAL2) #=== VAL2
EXPECT.toEqual(VAL2) #== VAL2
#For OBJ, compare own enumerated by copy
┌────────────────────┐
│ NULL|UNDEFINED │
└────────────────────┘
EXPECT.toBeDefined|Undefined() #=== undefined
EXPECT.toBeNull() #=== null
EXPECT.toBeNullish() #=== undefined|null
┌─────────────┐
│ BOOLEAN │
└─────────────┘
EXPECT_BOOL.toBeTruthy|Falsy() #== true|false
EXPECT_BOOL.toBeTrue|False() #=== true|false
┌────────────┐
│ NUMBER │
└────────────┘
EXPECT_NUM.toBeNaN() #Object.is(NaN)
EXPECT_NUM.toBePositive|NegativeInfinity() #=== Infinity|-Infinity
EXPECT_NUM.toBeLess|GreaterThan[OrEqual](NUM) #< > <= >= NUM
EXPECT_NUM.toBeCloseTo(NUM[, NUM2]) #== Math.round(NUM, NUM2)
#Def NUM2: 2
┌────────────┐
│ STRING │
└────────────┘
EXPECT_STR.toHaveSize(NUM) #
EXPECT_STR.toMatch(REGEXP) #REGEXP.test(STR)
EXPECT_STR.toContain(STR2) #Is substring
┌──────────────────┐
│ ARRAY|OBJECT │
└──────────────────┘
EXPECT_ARR|OBJ|[WEAK]MAP|SET|DATAVIEW.
toHaveSize(NUM) #
EXPECT_ARR.toContain(VAL) #Any ARR element deep equal
EXPECT_OBJ.toBeInstanceOf(FUNC) #instanceof
┌──────────────┐
│ FUNCTION │
└──────────────┘
EXPECT_FUNC.toThrow() #
EXPECT_FUNC.toThrow(VAL) #Using toEqual()
EXPECT_FUNC.toThrowError([TYPE], [STR|REGEXP]) #Def TYPE is Error
EXPECT_FUNC.toThrowMatching
(FUNC(ERROR)->BOOL) #
┌─────────────┐
│ PROMISE │
└─────────────┘
expectAsync(PROMISE).toBePending( ) #PROMISE not resolved|rejected
expectAsync(PROMISE).toBeResolved() #PROMISE resolved
expectAsync(PROMISE).toBeResolvedTo(VAL) #PROMISE resolved with VAL (deep equal)
expectAsync(PROMISE).toBeRejected() #PROMISE rejected
expectAsync(PROMISE).toBeRejectedWith(VAL) #PROMISE rejected with VAL (deep equal)
expectAsync(PROMISE).toBeRejectedWithError
(ERROR[, MESSAGE_REGEXP|STR]) #PROMISE rejected with ERROR with that ERROR.message
expectAsync(PROMISE).already.* #Same as expectAsync(PROMISE).* but fails if PROMISE still pending
┌────────────────────┐
│ DEEP ASSERTION │
└────────────────────┘
DEEP ASSERTIONS ==> #Assertions that can either be used:
# - top-level, e.g. EXPECT.toEqual(JASMINE.any(TYPE))
# - nested level, e.g. EXPECT.toEqual({ ..., VAR: JASMINE.any(TYPE) })
JASMINE.addCustomObjectFormatter(FUNC(VAL)->STR)#Customize key used in deep equal
EXPECT.toEqual #FUNC(VAL)->BOOL
({ asymmetricMatch: FUNC, ... }) #`this` is { ... } (must not use arrow functions)
#Can also do the reverse (i.e. assymetricMatch(...) on the expected value) but not both.
EXPECT.toEqual(JASMINE.is(VAL)) #===
EXPECT.toEqual(JASMINE.anything()) #!== undefined|null
EXPECT.toEqual(JASMINE.any(TYPE)) #instanceof TYPE
EXPECT_BOOL.toEqual(JASMINE.falsy|truthy()) #== false|true
EXPECT_STR.toEqual
(JASMINE.stringMatching(REGEXP|'REGEXP')) #REGEXP.test(STR)
EXPECT_STR.toEqual
(JASMINE.stringContaining(STR)) #STR2.includes(STR)
EXPECT_STR.toEqual(JASMINE.[not]empty()) #=== ''
EXPECT_ARR.toEqual(JASMIN.arrayContaining(ARR2))#ARR2 is a subset of ARR (using toEqual())
#Duplicates are removed. Order does not matter.
EXPECT_ARR.toEqual #ARR2 has exact same elements as ARR (using toEqual())
(JASMINE.arrayWithExactContents(ARR2)) #Order does not matter.
EXPECT_OBJ.toEqual(JASMN.objectContaining(OBJ2))#OBJ2 is a subset of OBJ (using toEqual())
EXPECT_MAP.toEqual(JASMINE.mapContaining(MAP2)) #Same for MAP
EXPECT_SET.toEqual(JASMINE.setContaining(SET2)) #Same for SET
EXPECT_OBJ|ARR.toEqual(JASMINE.[not]empty()) #=== {}|[]
┌──────────┐
│ HTML │
└──────────┘
EXPECT_ELEM.toHaveClass('CLASS')
EXPECT_ELEM.toHaveClasses('CLASS'_ARR) #Check HTML class
┌────────────────┐
│ EXTENSIONS │
└────────────────┘
JASMINE.addCustomEqualityTester #Will be used in toEqual(), providing it does not return undefined.
(FUNC(VAL, VAL2)->BOOL|undefined) #Must be done in beforeAll()
MATCHER #FUNC(UTIL, TESTERS)->OBJ:
# - compare(VAL[, ARG...])->OBJ2:
# - pass BOOL
# - message STR:
# - "Expected VAL [not] to be ..."
# - def. ...: use CUSTOM by adding space between camelcase
# - must be set even if pass true, but using "not"
# - negativeCompare(...):
# - same but used when "not" is used
# - def: just inverse pass BOOL
JASMINE.addMatchers({ CUSTOM: MATCHER ... }) #Add custom assertion EXPECT.CUSTOM([ARG...]):
#Must be in beforeAll()
JASMINE.addAsyncMatchers({CUSTOM: MATCHER ...}) #Same but compare|negativeCompare() can be async
UTIL.equals(VAL, VAL2, TESTERS)->BOOL #Like toEqual() (including using deep assertions)
UTIL.contains(ARR|STR, VAL|STR2, TESTERS)->BOOL #Like toContain()