title | category | layout | updated | weight | tags | intro | |
---|---|---|---|---|---|---|---|
Jest |
JavaScript libraries |
2017/sheet |
2020-06-17 |
-3 |
|
A quick overview to [Jest](https://facebook.github.io/jest/), a test framework for Node.js. This guide targets Jest v20.
|
{: .-three-column}
{: .-prime}
npm install --save-dev jest babel-jest
{: data-line="1"}
/* Add to package.json */
"scripts": {
"test": "jest"
}
# Run your tests
npm test -- --watch
See: Getting started
describe('My work', () => {
test('works', () => {
expect(2).toEqual(2)
})
})
See: describe(), test(), expect()
describe('My work', () => {
it('works', () => {
···
})
})
it
is an alias for test
.
See: test()
beforeEach(() => { ... })
afterEach(() => { ... })
beforeAll(() => { ... })
afterAll(() => { ... })
See: afterAll() and more
describe.only(···)
it.only(···) // alias: fit()
See: test.only
describe.skip(···)
it.skip(···) // alias: xit()
See: test.skip
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 |
{: .-three-column}
expect(value)
.not
.toBe(value)
.toEqual(value)
.toBeTruthy()
Note that toEqual
is a deep equality check.
See: expect()
expect(value)
.toMatchSnapshot()
.toMatchInlineSnapshot()
Note that toMatchInlineSnapshot()
requires Prettier to be set up for the project.
See: Inline snapshots
expect(value)
.toThrow(error)
.toThrowErrorMatchingSnapshot()
expect(value)
.toBeFalsy()
.toBeNull()
.toBeTruthy()
.toBeUndefined()
.toBeDefined()
expect(value)
.toBeCloseTo(number, numDigits)
.toBeGreaterThan(number)
.toBeGreaterThanOrEqual(number)
.toBeLessThan(number)
.toBeLessThanOrEqual(number)
expect(value)
.toBeInstanceOf(Class)
.toMatchObject(object)
.toHaveProperty(keyPath, value)
expect(value)
.toContain(item)
.toContainEqual(item)
.toHaveLength(number)
expect(value)
.toMatch(regexpOrString)
expect.extend(matchers)
expect.any(constructor)
expect.addSnapshotSerializer(serializer)
expect.assertions(1)
test('works with promises', () => {
return new Promise((resolve, reject) => {
···
})
})
{: data-line="2"}
test('works with async/await', async () => {
const hello = await foo()
···
})
{: data-line="2"}
Return promises, or use async/await. See: Async tutorial
it('works', () => {
const output = something()
expect(output).toMatchSnapshot()
})
{: data-line="3"}
First run creates a snapshot. Subsequent runs match the saved snapshot. See: Snapshot testing
import renderer from 'react-test-renderer'
{: .-setup}
it('works', () => {
const tree = renderer.create(
<Link page="http://www.facebook.com">Facebook</Link>
).toJSON()
expect(tree).toMatchSnapshot()
})
{: data-line="2,3,4"}
React's test renderer can be used for Jest snapshots. See: Snapshot test
jest.useFakeTimers()
it('works', () => {
jest.runOnlyPendingTimers()
jest.runTimersToTime(1000)
jest.runAllTimers()
})
See: Timer Mocks
const fn = jest.fn()
const fn = jest.fn(n => n * n)
See: Mock functions
expect(fn)
.toHaveBeenCalled()
.toHaveBeenCalledTimes(number)
.toHaveBeenCalledWith(arg1, arg2, ...)
.toHaveBeenLastCalledWith(arg1, arg2, ...)
expect(fn)
.toHaveBeenCalledWith(expect.anything())
.toHaveBeenCalledWith(expect.any(constructor))
.toHaveBeenCalledWith(expect.arrayContaining([ values ]))
.toHaveBeenCalledWith(expect.objectContaining({ props }))
.toHaveBeenCalledWith(expect.stringContaining(string))
.toHaveBeenCalledWith(expect.stringMatching(regexp))
const Fn = jest.fn()
a = new Fn()
b = new Fn()
Fn.mock.instances
// → [a, b]
{: data-line="1"}
See: .mock property
const fn = jest.fn()
fn(123)
fn(456)
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
const fn = jest.fn(() => 'hello')
jest.fn().mockReturnValue('hello')
jest.fn().mockReturnValueOnce('hello')
const fn = jest.fn()
.mockImplementationOnce(() => 1)
.mockImplementationOnce(() => 2)
{: data-line="2,3"}
fn() // → 1
fn() // → 2
{: .-one-column}
- http://facebook.github.io/jest/ {: .-also-see}