expectjs: update
This commit is contained in:
parent
a8b8296a68
commit
a53adf0973
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
title: expect.js
|
||||
category: Hidden
|
||||
redirect_to: /expectjs
|
||||
---
|
39
expectjs.md
39
expectjs.md
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue