cheatsheets/jest.md

372 lines
6.3 KiB
Markdown
Raw Permalink Normal View History

2017-03-14 05:41:29 +00:00
---
title: Jest
category: JavaScript libraries
2020-07-04 13:33:09 +00:00
updated: 2020-06-17
2017-08-29 21:53:45 +00:00
weight: -3
tags: [Featurable]
2017-09-21 16:59:30 +00:00
intro: |
A quick overview to [Jest](https://facebook.github.io/jest/), a test framework for Node.js. This guide targets Jest v20.
2017-03-14 05:41:29 +00:00
---
2017-08-28 16:08:07 +00:00
Testing
-------
{: .-three-column}
### Quick start
{: .-prime}
```bash
npm install --save-dev jest babel-jest
```
2017-08-31 21:30:54 +00:00
{: data-line="1"}
2017-03-14 05:41:29 +00:00
```js
2017-08-28 16:08:07 +00:00
/* Add to package.json */
"scripts": {
"test": "jest"
}
```
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```bash
# Run your tests
npm test -- --watch
```
See: [Getting started](https://facebook.github.io/jest/docs/en/getting-started.html)
2017-08-28 16:08:07 +00:00
### Writing tests
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```js
2017-03-14 05:41:29 +00:00
describe('My work', () => {
test('works', () => {
expect(2).toEqual(2)
})
2017-08-28 16:08:07 +00:00
})
```
2017-03-14 05:41:29 +00:00
See: [describe()](https://facebook.github.io/jest/docs/en/api.html#describename-fn), [test()](https://facebook.github.io/jest/docs/en/api.html#testname-fn), [expect()](https://facebook.github.io/jest/docs/en/expect.html#content)
2017-08-31 21:46:19 +00:00
2017-08-28 16:08:07 +00:00
### BDD syntax
```js
describe('My work', () => {
it('works', () => {
···
2017-03-14 05:41:29 +00:00
})
2017-08-28 16:08:07 +00:00
})
```
`it` is an alias for `test`.
See: [test()](https://facebook.github.io/jest/docs/en/api.html#testname-fn)
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
### Setup
```js
beforeEach(() => { ... })
afterEach(() => { ... })
```
```js
beforeAll(() => { ... })
afterAll(() => { ... })
```
See: [afterAll() and more](https://facebook.github.io/jest/docs/en/api.html#afterallfn)
2017-08-28 16:08:07 +00:00
### Focusing tests
```js
describe.only(···)
it.only(···) // alias: fit()
```
See: [test.only](https://facebook.github.io/jest/docs/en/api.html#testonlyname-fn)
2017-08-28 16:08:07 +00:00
### Skipping tests
```js
describe.skip(···)
it.skip(···) // alias: xit()
```
See: [test.skip](https://facebook.github.io/jest/docs/en/api.html#testskipname-fn)
2017-08-28 16:08:07 +00:00
2020-06-11 23:59:02 +00:00
### Optional flags
| Flag | Description |
| --------------------- | ---------------------------------------- |
| `--coverage` | See a summary of test coverage |
| `--detectOpenHandles` | See a summary of ports that didn't close |
| `--runInBand` | Run all tests one after the other |
2017-08-28 16:08:07 +00:00
Expect
------
{: .-three-column}
### Basic expectations
2017-03-14 05:41:29 +00:00
```js
expect(value)
.not
.toBe(value)
.toEqual(value)
2017-08-31 21:20:31 +00:00
.toBeTruthy()
2017-08-28 16:08:07 +00:00
```
2017-08-31 21:20:31 +00:00
Note that `toEqual` is a deep equality check.
See: [expect()](https://facebook.github.io/jest/docs/en/expect.html#expectvalue)
2017-08-28 16:08:07 +00:00
### Snapshots
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toMatchSnapshot()
2020-05-05 09:25:10 +00:00
.toMatchInlineSnapshot()
2017-08-28 16:08:07 +00:00
```
2017-03-14 05:41:29 +00:00
2020-06-17 13:23:34 +00:00
Note that `toMatchInlineSnapshot()` requires Prettier to be set up for the project.
See: [Inline snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots)
2020-05-05 09:25:10 +00:00
2017-08-28 16:08:07 +00:00
### Errors
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toThrow(error)
.toThrowErrorMatchingSnapshot()
2017-08-28 16:08:07 +00:00
```
### Booleans
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toBeFalsy()
.toBeNull()
.toBeTruthy()
.toBeUndefined()
.toBeDefined()
2017-08-28 16:08:07 +00:00
```
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
### Numbers
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toBeCloseTo(number, numDigits)
.toBeGreaterThan(number)
.toBeGreaterThanOrEqual(number)
.toBeLessThan(number)
.toBeLessThanOrEqual(number)
2017-08-28 16:08:07 +00:00
```
### Objects
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toBeInstanceOf(Class)
.toMatchObject(object)
.toHaveProperty(keyPath, value)
2017-08-28 16:08:07 +00:00
```
### Objects
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toContain(item)
.toContainEqual(item)
.toHaveLength(number)
2017-08-28 16:08:07 +00:00
```
### Strings
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```js
expect(value)
2017-03-14 05:41:29 +00:00
.toMatch(regexpOrString)
```
2017-08-28 16:08:07 +00:00
### Others
2017-03-14 05:41:29 +00:00
```js
expect.extend(matchers)
expect.any(constructor)
expect.addSnapshotSerializer(serializer)
expect.assertions(1)
```
2017-08-28 16:08:07 +00:00
More features
-------------
2017-03-14 05:41:29 +00:00
2017-08-31 21:21:54 +00:00
### Asynchronous tests
```js
test('works with promises', () => {
return new Promise((resolve, reject) => {
···
})
})
```
2017-08-31 21:44:27 +00:00
{: data-line="2"}
2017-08-31 21:21:54 +00:00
```js
test('works with async/await', async () => {
const hello = await foo()
···
})
```
2017-08-31 21:44:27 +00:00
{: data-line="2"}
2017-08-31 21:21:54 +00:00
Return promises, or use async/await.
See: [Async tutorial](https://facebook.github.io/jest/docs/en/tutorial-async.html)
2017-08-31 21:21:54 +00:00
2017-08-28 16:08:07 +00:00
### Snapshots
```jsx
2017-08-31 21:28:18 +00:00
it('works', () => {
const output = something()
expect(output).toMatchSnapshot()
})
2017-08-28 16:08:07 +00:00
```
2017-08-31 21:28:18 +00:00
{: data-line="3"}
First run creates a snapshot. Subsequent runs match the saved snapshot.
See: [Snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html)
2017-08-31 21:28:18 +00:00
### React test renderer
2017-03-14 05:41:29 +00:00
2017-08-28 16:08:07 +00:00
```jsx
2017-08-31 21:28:18 +00:00
import renderer from 'react-test-renderer'
2017-08-28 16:08:07 +00:00
```
2017-08-31 21:28:18 +00:00
{: .-setup}
2017-03-14 05:41:29 +00:00
2017-08-31 21:28:18 +00:00
```jsx
it('works', () => {
const tree = renderer.create(
<Link page="https://www.facebook.com">Facebook</Link>
2017-08-31 21:28:18 +00:00
).toJSON()
expect(tree).toMatchSnapshot()
})
2017-03-14 05:41:29 +00:00
```
2017-08-31 21:28:18 +00:00
{: data-line="2,3,4"}
React's test renderer can be used for Jest snapshots.
See: [Snapshot test](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#snapshot-test)
2017-03-14 05:41:29 +00:00
2017-08-31 21:44:27 +00:00
### Timers
```js
jest.useFakeTimers()
```
```js
it('works', () => {
jest.runOnlyPendingTimers()
jest.runTimersToTime(1000)
jest.runAllTimers()
})
```
See: [Timer Mocks](https://facebook.github.io/jest/docs/en/timer-mocks.html)
2017-08-31 21:44:27 +00:00
## Mock functions
### Mock functions
2017-03-14 05:41:29 +00:00
```js
const fn = jest.fn()
2017-08-31 21:44:27 +00:00
```
```js
2017-03-14 05:41:29 +00:00
const fn = jest.fn(n => n * n)
```
See: [Mock functions](https://facebook.github.io/jest/docs/en/mock-functions.html#using-a-mock-function)
2017-08-31 21:44:27 +00:00
### Assertions
2017-03-14 05:41:29 +00:00
```js
expect(fn)
.toHaveBeenCalled()
.toHaveBeenCalledTimes(number)
.toHaveBeenCalledWith(arg1, arg2, ...)
.toHaveBeenLastCalledWith(arg1, arg2, ...)
2017-08-31 21:44:27 +00:00
```
2017-03-14 05:41:29 +00:00
2017-08-31 21:44:27 +00:00
```js
expect(fn)
2017-03-14 05:41:29 +00:00
.toHaveBeenCalledWith(expect.anything())
.toHaveBeenCalledWith(expect.any(constructor))
.toHaveBeenCalledWith(expect.arrayContaining([ values ]))
.toHaveBeenCalledWith(expect.objectContaining({ props }))
.toHaveBeenCalledWith(expect.stringContaining(string))
.toHaveBeenCalledWith(expect.stringMatching(regexp))
```
2017-08-31 21:44:27 +00:00
### Instances
2017-08-23 21:18:38 +00:00
```js
2017-08-31 21:44:27 +00:00
const Fn = jest.fn()
2017-08-23 21:18:38 +00:00
2017-08-31 21:44:27 +00:00
a = new Fn()
b = new Fn()
2017-08-23 21:18:38 +00:00
```
2017-08-31 21:44:27 +00:00
```js
Fn.mock.instances
// → [a, b]
```
{: data-line="1"}
See: [.mock property](https://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
2017-08-31 21:44:27 +00:00
### Calls
```js
const fn = jest.fn()
fn(123)
fn(456)
```
```js
fn.mock.calls.length // → 2
fn.mock.calls[0][0] // → 123
fn.mock.calls[1][0] // → 456
```
{: data-line="1,2,3"}
See: [.mock property](https://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
2017-08-31 21:44:27 +00:00
### Return values
```js
const fn = jest.fn(() => 'hello')
```
2017-09-21 16:59:30 +00:00
#### or:
2017-08-31 21:44:27 +00:00
```js
jest.fn().mockReturnValue('hello')
jest.fn().mockReturnValueOnce('hello')
```
### Mock implementations
```js
const fn = jest.fn()
.mockImplementationOnce(() => 1)
.mockImplementationOnce(() => 2)
```
{: data-line="2,3"}
```js
fn() // → 1
fn() // → 2
```
2017-08-28 16:08:07 +00:00
2017-03-14 05:41:29 +00:00
## References
2017-08-28 16:08:07 +00:00
{: .-one-column}
2017-03-14 05:41:29 +00:00
- <https://facebook.github.io/jest/>
2017-08-28 16:08:07 +00:00
{: .-also-see}