2015-07-04 04:01:04 +00:00
|
|
|
---
|
|
|
|
title: expect.js
|
2015-11-24 05:02:17 +00:00
|
|
|
category: JavaScript libraries
|
2017-09-01 20:48:13 +00:00
|
|
|
updated: 2017-09-02
|
|
|
|
weight: -1
|
2015-07-04 04:01:04 +00:00
|
|
|
---
|
|
|
|
|
2017-09-01 20:48:13 +00:00
|
|
|
### 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
|
2017-09-01 20:43:07 +00:00
|
|
|
|
2015-07-04 04:01:04 +00:00
|
|
|
```js
|
|
|
|
expect(x).toBe(y)
|
|
|
|
.toBe(val)
|
|
|
|
.toEqual(val)
|
|
|
|
.toThrow(err)
|
2017-09-01 20:43:07 +00:00
|
|
|
.toExist() // aka: toBeTruthy()
|
|
|
|
.toNotExist() // aka: toBeFalsy()
|
2015-07-04 04:01:04 +00:00
|
|
|
.toBeA(constructor)
|
|
|
|
.toBeA('string')
|
|
|
|
.toMatch(/expr/)
|
|
|
|
.toBeLessThan(n)
|
|
|
|
.toBeGreaterThan(n)
|
2017-09-01 20:43:07 +00:00
|
|
|
.toBeLessThanOrEqualTo(n)
|
|
|
|
.toBeGreaterThanOrEqualTo(n)
|
|
|
|
.toInclude(val) // aka: toContain(val)
|
2015-07-04 04:01:04 +00:00
|
|
|
.toExclude(val)
|
2017-09-01 20:43:07 +00:00
|
|
|
.toIncludeKey(key)
|
|
|
|
.toExcludeKey(key)
|
|
|
|
```
|
|
|
|
|
|
|
|
Also: `toNotBe`, `toNotEqual`, etc for negatives.
|
|
|
|
|
|
|
|
### Chaining assertions
|
2015-07-16 20:19:25 +00:00
|
|
|
|
2017-09-01 20:43:07 +00:00
|
|
|
```js
|
|
|
|
expect(3.14)
|
|
|
|
.toExist()
|
|
|
|
.toBeLessThan(4)
|
|
|
|
.toBeGreaterThan(3)
|
2015-07-04 04:01:04 +00:00
|
|
|
```
|
|
|
|
|
2017-09-01 20:43:07 +00:00
|
|
|
Assertions can be chained.
|
|
|
|
|
2015-07-16 20:19:25 +00:00
|
|
|
### Spies
|
|
|
|
|
2017-09-01 20:48:13 +00:00
|
|
|
```js
|
|
|
|
const video = {
|
|
|
|
play: function () { ··· }
|
|
|
|
}
|
|
|
|
```
|
|
|
|
{: .-setup}
|
|
|
|
|
2015-07-04 04:01:04 +00:00
|
|
|
```js
|
|
|
|
spy = expect.spyOn(video, 'play')
|
2017-09-01 20:43:07 +00:00
|
|
|
```
|
2015-07-16 20:19:25 +00:00
|
|
|
|
2017-09-01 20:43:07 +00:00
|
|
|
```js
|
2017-09-01 20:48:13 +00:00
|
|
|
spy = expect.spyOn(···)
|
|
|
|
.andCallThrough() // pass through
|
2015-07-16 20:19:25 +00:00
|
|
|
.andCall(fn)
|
|
|
|
.andThrow(exception)
|
|
|
|
.andReturn(value)
|
2017-09-01 20:43:07 +00:00
|
|
|
```
|
2015-07-16 20:19:25 +00:00
|
|
|
|
2017-09-01 20:48:13 +00:00
|
|
|
### Assertions on spies
|
|
|
|
|
2017-09-01 20:43:07 +00:00
|
|
|
```js
|
2015-07-16 20:19:25 +00:00
|
|
|
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(...)
|
2017-09-01 20:43:07 +00:00
|
|
|
```
|
2015-07-16 20:19:25 +00:00
|
|
|
|
2017-09-01 20:43:07 +00:00
|
|
|
```js
|
2015-07-16 20:19:25 +00:00
|
|
|
expect(spy).toHaveBeenCalled()
|
|
|
|
expect(spy).toHaveBeenCalledWith('some', 'args')
|
2015-07-04 04:01:04 +00:00
|
|
|
```
|
|
|
|
|
2017-09-01 20:43:07 +00:00
|
|
|
### References
|
|
|
|
|
|
|
|
- <https://www.npmjs.com/package/expect>
|
|
|
|
- <https://github.com/mjackson/expect>
|