expectjs: update

This commit is contained in:
Rico Sta. Cruz 2017-09-02 04:43:07 +08:00
parent a8b8296a68
commit a53adf0973
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
2 changed files with 38 additions and 6 deletions

5
expect.js.md Normal file
View File

@ -0,0 +1,5 @@
---
title: expect.js
category: Hidden
redirect_to: /expectjs
---

View File

@ -1,44 +1,71 @@
---
title: expect.js
category: JavaScript libraries
layout: 2017/sheet
---
### Expectations
```js
expect(x).toBe(y)
.toBe(val)
.toEqual(val)
.toThrow(err)
.toExist /* aka: toBeTruthy */
.toNotExist /* aka: toBeFalsy */
.toExist() // aka: toBeTruthy()
.toNotExist() // aka: toBeFalsy()
.toBeA(constructor)
.toBeA('string')
.toMatch(/expr/)
.toBeLessThan(n)
.toBeGreaterThan(n)
.toInclude(val) /* aka: toContain */
.toBeLessThanOrEqualTo(n)
.toBeGreaterThanOrEqualTo(n)
.toInclude(val) // aka: toContain(val)
.toExclude(val)
/* also: toNotBe, toNotEqual, etc */
.toIncludeKey(key)
.toExcludeKey(key)
```
Also: `toNotBe`, `toNotEqual`, etc for negatives.
### Chaining assertions
```js
expect(3.14)
.toExist()
.toBeLessThan(4)
.toBeGreaterThan(3)
```
Assertions can be chained.
### Spies
```js
spy = expect.spyOn(video, 'play')
```
```js
spy = expect.spyOn(...)
.andCallThrough() /* pass through */
.andCall(fn)
.andThrow(exception)
.andReturn(value)
```
```js
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].context).toBe(video)
expect(spy.calls[0].arguments).toEqual([ 'some', 'args' ])
expect(spy.getLastCall().arguments).toEqual(...)
```
```js
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledWith('some', 'args')
```
https://www.npmjs.com/package/expect
### References
- <https://www.npmjs.com/package/expect>
- <https://github.com/mjackson/expect>