npm install --save-dev jest babel-jest
/* Add to package.json */
"scripts": {
"test": "jest"
}
# Run your tests
npm test -- --watch
describe.skip(···)
it.skip(···) // alias: xit()
describe('My work', () => {
test('works', () => {
expect(2).toEqual(2)
})
})
describe('My work', () => {
it('works', () => {
···
})
})
beforeEach(() => { ... })
afterEach(() => { ... })
beforeAll(() => { ... })
afterAll(() => { ... })
describe.only(···)
it.only(···) // alias: fit()
test('works with promises', () => {
return new Promise((resolve, reject) => {
···
})
})
test('works with async/await', async () => {
const hello = await foo()
···
})
it('works', () => {
jest.runOnlyPendingTimers()
jest.runTimersToTime(1000)
jest.runAllTimers()
})
First run creates a snapshot. Subsequent runs match the saved snapshot
it('works', () => {
const output = something()
expect(output).toMatchSnapshot()
})
React’s test renderer can be used for Jest snapshots.
import renderer from 'react-test-renderer'
it('works', () => {
const tree = renderer.create(
<Link page="http://www.facebook.com">Facebook</Link>
).toJSON()
expect(tree).toMatchSnapshot()
})
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))