expectjs: improve some more
This commit is contained in:
parent
a53adf0973
commit
1c778d88aa
40
expectjs.md
40
expectjs.md
|
@ -2,9 +2,34 @@
|
||||||
title: expect.js
|
title: expect.js
|
||||||
category: JavaScript libraries
|
category: JavaScript libraries
|
||||||
layout: 2017/sheet
|
layout: 2017/sheet
|
||||||
|
updated: 2017-09-02
|
||||||
|
weight: -1
|
||||||
---
|
---
|
||||||
|
|
||||||
### Expectations
|
### Using
|
||||||
|
|
||||||
|
```
|
||||||
|
npm install --save-dev expect
|
||||||
|
```
|
||||||
|
{: .-setup}
|
||||||
|
|
||||||
|
```js
|
||||||
|
// using ES6 modules
|
||||||
|
import expect, { createSpy, spyOn, isSpy } from 'expect'
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
// using CommonJS modules
|
||||||
|
var expect = require('expect')
|
||||||
|
var createSpy = expect.createSpy
|
||||||
|
var spyOn = expect.spyOn
|
||||||
|
var isSpy = expect.isSpy
|
||||||
|
```
|
||||||
|
|
||||||
|
Expect is a library for assertions in tests.
|
||||||
|
See: [mjackson/expect](https://github.com/mjackson/expect)
|
||||||
|
|
||||||
|
### Assertions
|
||||||
|
|
||||||
```js
|
```js
|
||||||
expect(x).toBe(y)
|
expect(x).toBe(y)
|
||||||
|
@ -41,18 +66,27 @@ Assertions can be chained.
|
||||||
|
|
||||||
### Spies
|
### Spies
|
||||||
|
|
||||||
|
```js
|
||||||
|
const video = {
|
||||||
|
play: function () { ··· }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
{: .-setup}
|
||||||
|
|
||||||
```js
|
```js
|
||||||
spy = expect.spyOn(video, 'play')
|
spy = expect.spyOn(video, 'play')
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```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)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Assertions on spies
|
||||||
|
|
||||||
```js
|
```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)
|
||||||
|
|
Loading…
Reference in New Issue