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)
100 ### Basic expectations
110 Note that `toEqual` is a deep equality check.
111 See: [expect()](http://facebook.github.io/jest/docs/en/expect.html#expectvalue)
125 .toThrowErrorMatchingSnapshot()
143 .toBeCloseTo(number, numDigits)
144 .toBeGreaterThan(number)
145 .toBeGreaterThanOrEqual(number)
146 .toBeLessThan(number)
147 .toBeLessThanOrEqual(number)
154 .toBeInstanceOf(Class)
155 .toMatchObject(object)
156 .toHaveProperty(keyPath, value)
164 .toContainEqual(item)
165 .toHaveLength(number)
172 .toMatch(regexpOrString)
178 expect.extend(matchers)
179 expect.any(constructor)
180 expect.addSnapshotSerializer(serializer)
188 ### Asynchronous tests
191 test('works with promises', () => {
192 return new Promise((resolve, reject) => {
200 test('works with async/await', async () => {
201 const hello = await foo()
207 Return promises, or use async/await.
208 See: [Async tutorial](http://facebook.github.io/jest/docs/en/tutorial-async.html)
214 const output = something()
215 expect(output).toMatchSnapshot()
220 First run creates a snapshot. Subsequent runs match the saved snapshot.
221 See: [Snapshot testing](http://facebook.github.io/jest/docs/en/snapshot-testing.html)
223 ### React test renderer
226 import renderer from 'react-test-renderer'
232 const tree = renderer.create(
233 <Link page="http://www.facebook.com">Facebook</Link>
236 expect(tree).toMatchSnapshot()
239 {: data-line="2,3,4"}
241 React's test renderer can be used for Jest snapshots.
242 See: [Snapshot test](http://facebook.github.io/jest/docs/en/tutorial-react-native.html#snapshot-test)
252 jest.runOnlyPendingTimers()
253 jest.runTimersToTime(1000)
258 See: [Timer Mocks](http://facebook.github.io/jest/docs/en/timer-mocks.html)
269 const fn = jest.fn(n => n * n)
272 See: [Mock functions](http://facebook.github.io/jest/docs/en/mock-functions.html#using-a-mock-function)
279 .toHaveBeenCalledTimes(number)
280 .toHaveBeenCalledWith(arg1, arg2, ...)
281 .toHaveBeenLastCalledWith(arg1, arg2, ...)
286 .toHaveBeenCalledWith(expect.anything())
287 .toHaveBeenCalledWith(expect.any(constructor))
288 .toHaveBeenCalledWith(expect.arrayContaining([ values ]))
289 .toHaveBeenCalledWith(expect.objectContaining({ props }))
290 .toHaveBeenCalledWith(expect.stringContaining(string))
291 .toHaveBeenCalledWith(expect.stringMatching(regexp))
309 See: [.mock property](http://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
320 fn.mock.calls.length // → 2
321 fn.mock.calls[0][0] // → 123
322 fn.mock.calls[1][0] // → 456
324 {: data-line="1,2,3"}
326 See: [.mock property](http://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
331 const fn = jest.fn(() => 'hello')
337 jest.fn().mockReturnValue('hello')
338 jest.fn().mockReturnValueOnce('hello')
341 ### Mock implementations
345 .mockImplementationOnce(() => 1)
346 .mockImplementationOnce(() => 2)
358 - <http://facebook.github.io/jest/>