3 category: JavaScript libraries
9 A quick overview to [Jest](https://facebook.github.io/jest/), a test framework for Node.js. This guide targets Jest v20.
20 npm install --save-dev jest babel-jest
25 /* Add to package.json */
36 See: [Getting started](http://facebook.github.io/jest/docs/en/getting-started.html)
41 describe('My work', () => {
48 See: [describe()](http://facebook.github.io/jest/docs/en/api.html#describename-fn), [test()](http://facebook.github.io/jest/docs/en/api.html#testname-fn), [expect()](http://facebook.github.io/jest/docs/en/expect.html#content)
53 describe('My work', () => {
60 `it` is an alias for `test`.
61 See: [test()](http://facebook.github.io/jest/docs/en/api.html#testname-fn)
66 beforeEach(() => { ... })
67 afterEach(() => { ... })
71 beforeAll(() => { ... })
72 afterAll(() => { ... })
75 See: [afterAll() and more](http://facebook.github.io/jest/docs/en/api.html#afterallfn)
81 it.only(···) // alias: fit()
84 See: [test.only](http://facebook.github.io/jest/docs/en/api.html#testonlyname-fn)
91 it.skip(···) // alias: xit()
94 See: [test.skip](http://facebook.github.io/jest/docs/en/api.html#testskipname-fn)
99 | Flag | Description |
100 | --------------------- | ---------------------------------------- |
101 | `--coverage` | See a summary of test coverage |
102 | `--detectOpenHandles` | See a summary of ports that didn't close |
103 | `--runInBand` | Run all tests one after the other |
109 ### Basic expectations
119 Note that `toEqual` is a deep equality check.
120 See: [expect()](http://facebook.github.io/jest/docs/en/expect.html#expectvalue)
127 .toMatchInlineSnapshot()
130 Note that `toMatchInlineSnapshot()` requires Prettier to be set up for the project.
131 See: [Inline snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots)
138 .toThrowErrorMatchingSnapshot()
156 .toBeCloseTo(number, numDigits)
157 .toBeGreaterThan(number)
158 .toBeGreaterThanOrEqual(number)
159 .toBeLessThan(number)
160 .toBeLessThanOrEqual(number)
167 .toBeInstanceOf(Class)
168 .toMatchObject(object)
169 .toHaveProperty(keyPath, value)
177 .toContainEqual(item)
178 .toHaveLength(number)
185 .toMatch(regexpOrString)
191 expect.extend(matchers)
192 expect.any(constructor)
193 expect.addSnapshotSerializer(serializer)
201 ### Asynchronous tests
204 test('works with promises', () => {
205 return new Promise((resolve, reject) => {
213 test('works with async/await', async () => {
214 const hello = await foo()
220 Return promises, or use async/await.
221 See: [Async tutorial](http://facebook.github.io/jest/docs/en/tutorial-async.html)
227 const output = something()
228 expect(output).toMatchSnapshot()
233 First run creates a snapshot. Subsequent runs match the saved snapshot.
234 See: [Snapshot testing](http://facebook.github.io/jest/docs/en/snapshot-testing.html)
236 ### React test renderer
239 import renderer from 'react-test-renderer'
245 const tree = renderer.create(
246 <Link page="http://www.facebook.com">Facebook</Link>
249 expect(tree).toMatchSnapshot()
252 {: data-line="2,3,4"}
254 React's test renderer can be used for Jest snapshots.
255 See: [Snapshot test](http://facebook.github.io/jest/docs/en/tutorial-react-native.html#snapshot-test)
265 jest.runOnlyPendingTimers()
266 jest.runTimersToTime(1000)
271 See: [Timer Mocks](http://facebook.github.io/jest/docs/en/timer-mocks.html)
282 const fn = jest.fn(n => n * n)
285 See: [Mock functions](http://facebook.github.io/jest/docs/en/mock-functions.html#using-a-mock-function)
292 .toHaveBeenCalledTimes(number)
293 .toHaveBeenCalledWith(arg1, arg2, ...)
294 .toHaveBeenLastCalledWith(arg1, arg2, ...)
299 .toHaveBeenCalledWith(expect.anything())
300 .toHaveBeenCalledWith(expect.any(constructor))
301 .toHaveBeenCalledWith(expect.arrayContaining([ values ]))
302 .toHaveBeenCalledWith(expect.objectContaining({ props }))
303 .toHaveBeenCalledWith(expect.stringContaining(string))
304 .toHaveBeenCalledWith(expect.stringMatching(regexp))
322 See: [.mock property](http://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
333 fn.mock.calls.length // → 2
334 fn.mock.calls[0][0] // → 123
335 fn.mock.calls[1][0] // → 456
337 {: data-line="1,2,3"}
339 See: [.mock property](http://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
344 const fn = jest.fn(() => 'hello')
350 jest.fn().mockReturnValue('hello')
351 jest.fn().mockReturnValueOnce('hello')
354 ### Mock implementations
358 .mockImplementationOnce(() => 1)
359 .mockImplementationOnce(() => 2)
371 - <http://facebook.github.io/jest/>