OSDN Git Service

Regular updates
[twpd/master.git] / chai.md
1 ---
2 title: Chai.js
3 category: JavaScript libraries
4 layout: 2017/sheet
5 weight: -3
6 updated: 2018-06-25
7 version: chai v4.x
8 description: |
9   expect(x).to.be.equal(y) 〉 assert.equal(x, y) 〉 .to.be.true 〉 jQuery, assertions, TDD and BDD, and other Chai examples.
10 ---
11
12 ### Assert
13
14 ```js
15 const { assert } = require('chai')
16 ```
17 {: .-setup}
18
19 ```js
20 assert(val)
21 assert.fail(actual, expected)
22 assert.ok(val)                        // is truthy
23 assert.equal(actual, expected)        // compare with ==
24 assert.strictEqual(actual, expected)  // compare with ===
25 assert.deepEqual(actual, expected)    // deep equal check
26 ```
27
28 ```js
29 assert.isTrue(val)
30 assert.isFalse(val)
31 ```
32
33 ```js
34 assert.isNull(val)
35 assert.isNotNull(val)
36 assert.isUndefined(val)
37 assert.isDefined(val)
38 assert.isFunction(val)
39 assert.isObject(val)
40 assert.isArray(val)
41 assert.isString(val)
42 assert.isNumber(val)
43 assert.isBoolean(val)
44 ```
45
46 ```js
47 assert.typeOf(/tea/, 'regexp') // Object.prototype.toString()
48 assert.instanceOf(chai, Tea)
49 assert.include([ a,b,c ], a)
50 assert.match(val, /regexp/)
51 assert.property(obj, 'tea') // 'tea' in object
52 assert.deepProperty(obj, 'tea.green')
53 assert.propertyVal(person, 'name', 'John')
54 assert.deepPropertyVal(post, 'author.name', 'John')
55 ```
56
57 ```js
58 assert.lengthOf(object, 3)
59 assert.throws(function() { ... })
60 assert.throws(function() { ... }, /reference error/)
61 assert.doesNotThrow
62 ```
63
64 ```js
65 assert.operator(1, '<', 2)
66 assert.closeTo(actual, expected)
67 ```
68
69 See: [Assert API](http://chaijs.com/api/assert/) _(chaijs.com)_
70
71 ### BDD syntax
72
73 ```js
74 const { expect } = require('chai')
75 ```
76 {: .-setup}
77
78 ```js
79 expect(object)
80   .to.equal(expected)
81   .to.eql(expected)        // deep equality
82   .to.deep.equal(expected) // same as .eql
83   .to.be.a('string')
84   .to.include(val)
85 ```
86
87 ```js
88   .be.ok(val)
89   .be.true
90   .be.false
91   .to.exist
92 ```
93
94 ```js
95   .to.be.null
96   .to.be.undefined
97   .to.be.empty
98   .to.be.arguments
99   .to.be.function
100   .to.be.instanceOf
101 ```
102
103 ```js
104   .to.be.gt(5)  // aka: .above .greaterThan
105   .to.be.gte(5) // aka: .at.least
106   .to.be.lt(5)  // aka: .below
107 ```
108
109 ```js
110   .to.respondTo('bar')
111   .to.satisfy((n) => n > 0)
112 ```
113
114 ```js
115   .to.have.members([2, 3, 4])
116   .to.have.keys(['foo'])
117   .to.have.key('foo')
118   .to.have.lengthOf(3)
119 ```
120
121 ```js
122 expect(() => { ··· })
123   .to.throw(/not a function/)
124 ```
125
126 See: [BDD](http://chaijs.com/api/bdd/) _(chaijs.com)_
127
128 ### Should: chains
129
130     .to .be .been .is .that .and .have .with .at .of .same
131
132 These don't do anything and can be chained.
133
134 ### Should not
135
136 ```js
137 expect(object).not.equal('x')
138 ```
139
140 ## Chai with jQuery
141
142 ### Using chai-jquery
143
144 ```js
145 global.jQuery = ···
146 chai.use(require('chai-jquery'))
147 ```
148 {: .-setup}
149
150 ```js
151 expect($body)
152   .have.attr('foo')
153   .have.prop('disabled')
154   .have.css('background')
155   .have.css('background-color', '#ffffff')
156   .have.data('foo')
157 ```
158
159 ```js
160   .have.class('active')
161   .have.id('id')
162 ```
163
164 ```js
165   .have.html('<em>hi</em>')
166   .have.text('hello')
167   .have.value('2013')
168 ```
169
170 ### Continued
171
172 ```js
173 expect($body)
174 ```
175
176 ```js
177   .be.visible
178   .be.hidden
179 ```
180
181 ```js
182   .be.checked
183   .be.selected
184 ```
185
186 ```js
187   .be.enabled
188   .be.disabled
189 ```
190
191 ```js
192   .be.empty
193   .to.exist
194   .to.contain('text')
195   .to.have('.selector')
196 ```