chai: update

This commit is contained in:
Rico Sta. Cruz 2017-08-30 14:20:27 +08:00
parent 3afc3ad383
commit 0fd0fde9ba
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 143 additions and 102 deletions

205
chai.md
View File

@ -1,131 +1,172 @@
--- ---
title: Chai.js title: Chai.js
category: JavaScript libraries category: JavaScript libraries
layout: default-ad layout: 2017/sheet
weight: -3
--- ---
### Assert ### Assert
assert(val) ```js
assert.fail(actual, expected) assert(val)
assert.ok(val) // is truthy assert.fail(actual, expected)
assert.equal(actual, expected) // 'compare with ==' assert.ok(val) // is truthy
assert.strictEqual assert.equal(actual, expected) // compare with ==
assert.deepEqual assert.strictEqual(actual, expected) // compare with ===
assert.deepEqual(actual, expected) // deep equal check
```
assert.isTrue ```js
assert.isFalse assert.isTrue(val)
assert.isFalse(val)
```
assert.isNull ```js
assert.isNotNull assert.isNull(val)
assert.isUndefined assert.isNotNull(val)
assert.isDefined assert.isUndefined(val)
assert.isFunction assert.isDefined(val)
assert.isObject assert.isFunction(val)
assert.isArray assert.isObject(val)
assert.isString assert.isArray(val)
assert.isNumber assert.isString(val)
assert.isBoolean assert.isNumber(val)
assert.isBoolean(val)
```
assert.typeOf(/tea/, 'regexp') // Object.prototype.toString() ```js
assert.instanceOf(chai, Tea) assert.typeOf(/tea/, 'regexp') // Object.prototype.toString()
assert.include([ a,b,c ], a) assert.instanceOf(chai, Tea)
assert.match(val, /regexp/) assert.include([ a,b,c ], a)
assert.property(obj, 'tea') // 'tea' in object assert.match(val, /regexp/)
assert.deepProperty(obj, 'tea.green') assert.property(obj, 'tea') // 'tea' in object
assert.propertyVal(person, 'name', 'John') assert.deepProperty(obj, 'tea.green')
assert.deepPropertyVal(post, 'author.name', 'John') assert.propertyVal(person, 'name', 'John')
assert.deepPropertyVal(post, 'author.name', 'John')
```
assert.lengthOf(object, 3) ```js
assert.throws(function() { ... }) assert.lengthOf(object, 3)
assert.throws(function() { ... }, /reference error/) assert.throws(function() { ... })
assert.doesNotThrow assert.throws(function() { ... }, /reference error/)
assert.doesNotThrow
```
assert.operator(1, '<', 2) ```js
assert.closeTo(actual, expected) assert.operator(1, '<', 2)
assert.closeTo(actual, expected)
```
See: [Assert API](http://chaijs.com/api/assert/) _(chaijs.com)_
### BDD syntax
```js
expect(object)
.to.equal(expected)
.to.eql(expected) // deep equality
.to.deep.equal(expected) // same as .eql
.to.be.a('string')
.to.include(val)
```
```js
.be.ok(val)
.be.true
.be.false
.to.exist
```
```js
.to.be.null
.to.be.undefined
.to.be.empty
.to.be.arguments
.to.be.function
.to.be.instanceOf
```
```js
.to.be.gt(5) // aka: .above .greaterThan
.to.be.gte(5) // aka: .at.least
.to.be.lt(5) // aka: .below
```
```js
.to.respondTo('bar')
.to.satisfy((n) => n > 0)
```
```js
.to.have.members([2, 3, 4])
.to.have.keys(['foo'])
.to.have.key('foo')
```
```js
expect(() => { ··· })
.to.throw(/not a function/)
```
See: [BDD](http://chaijs.com/api/bdd/) _(chaijs.com)_
### Should: chains ### Should: chains
.to .be .been .is .that .and .have .with .at .of .same .to .be .been .is .that .and .have .with .at .of .same
These don't do anything and can be chained.
### Should not ### Should not
expect(object).not.equal('x') ```js
expect(object).not.equal('x')
### Expectations ```
expect(object)
.equal(expected)
.eql
.deep.equal(expected) // same as .eql
.be.a('string')
.include(val)
.be.ok(val)
.be.true
.be.false
.be.null
.be.undefined
.be.empty
.be.arguments
.be.function
.be.instanceOf
.gt(5) # or .above .greaterThan
.gte # or .at.least
.lt(5) # or .below
.respondTo('bar')
.satisfy (n) -> n > 0
.have.members([2, 3, 4])
.have.keys(['foo'])
.have.key('foo')
.exist
expect(-> ...)
.throw /not a function/
### Chai-jQuery ### Chai-jQuery
global.jQuery = ...; ```js
chai.use(require('chai-jquery')); global.jQuery = ···
chai.use(require('chai-jquery'))
expect($body) ```
```js
expect($body)
.have.attr('foo') .have.attr('foo')
.have.prop('disabled') .have.prop('disabled')
.have.css('background') .have.css('background')
.have.css('background-color', '#ffffff') .have.css('background-color', '#ffffff')
.have.data('foo') .have.data('foo')
```
```js
.have.class('active') .have.class('active')
.have.id('id') .have.id('id')
```
```js
.have.html('<em>hi</em>') .have.html('<em>hi</em>')
.have.text('hello') .have.text('hello')
.have.value('2013') .have.value('2013')
```
```js
.be.visible .be.visible
.be.hidden .be.hidden
```
```js
.be.checked .be.checked
.be.selected .be.selected
```
```js
.be.enabled .be.enabled
.be.disabled .be.disabled
```
```js
.be.empty .be.empty
.to.exist .to.exist
.to.contain('text') .to.contain('text')
.to.have('.selector') .to.have('.selector')
```
### References
* http://chaijs.com/api/assert/