expectjs: improve some more

This commit is contained in:
Rico Sta. Cruz 2017-09-02 04:48:13 +08:00
parent a53adf0973
commit 1c778d88aa
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 37 additions and 3 deletions

View File

@ -2,9 +2,34 @@
title: expect.js
category: JavaScript libraries
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
expect(x).toBe(y)
@ -41,18 +66,27 @@ Assertions can be chained.
### Spies
```js
const video = {
play: function () { ··· }
}
```
{: .-setup}
```js
spy = expect.spyOn(video, 'play')
```
```js
spy = expect.spyOn(...)
.andCallThrough() /* pass through */
spy = expect.spyOn(···)
.andCallThrough() // pass through
.andCall(fn)
.andThrow(exception)
.andReturn(value)
```
### Assertions on spies
```js
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].context).toBe(video)