mirror of
https://github.com/onkelbeh/cheatsheets.git
synced 2026-08-16 05:54:53 +02:00
expectjs: improve some more
This commit is contained in:
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user