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