Jest Globals¶
Reference for all global functions and objects available in tests.
Test Structure¶
describe(name, fn)¶
Create a test suite:
Nested describes:
describe('Calculator', () => {
describe('add', () => {
test('adds positive numbers', () => {});
});
describe('subtract', () => {
test('subtracts numbers', () => {});
});
});
test(name, fn, timeout?)¶
Define a test:
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
// With custom timeout (milliseconds)
test('slow operation', async () => {
await slowOperation();
}, 10000);
it(name, fn, timeout?)¶
Alias for test:
test.skip(name, fn) / it.skip(name, fn)¶
Skip a test:
test.only(name, fn) / it.only(name, fn)¶
Run only this test:
test.todo(name)¶
Mark a test as todo:
Lifecycle Hooks¶
beforeAll(fn, timeout?)¶
Run once before all tests in a describe block:
afterAll(fn, timeout?)¶
Run once after all tests in a describe block:
describe('database tests', () => {
afterAll(async () => {
await db.disconnect();
});
// tests...
});
beforeEach(fn, timeout?)¶
Run before each test:
describe('user tests', () => {
let user;
beforeEach(() => {
user = createTestUser();
});
test('user has name', () => {
expect(user.name).toBeDefined();
});
});
afterEach(fn, timeout?)¶
Run after each test:
Expect¶
expect(value)¶
Create an expectation:
See Matchers Guide for all available matchers.
Jest Object¶
jest.fn(implementation?)¶
Create a mock function:
jest.spyOn(object, method)¶
Spy on an object method:
const spy = jest.spyOn(console, 'log');
console.log('hello');
expect(spy).toHaveBeenCalledWith('hello');
spy.mockRestore();
jest.mock(moduleName, factory?)¶
Mock a module:
jest.unmock(moduleName)¶
Remove a module mock:
jest.useFakeTimers()¶
Enable fake timers:
jest.useFakeTimers();
setTimeout(() => callback(), 1000);
jest.advanceTimersByTime(1000);
expect(callback).toHaveBeenCalled();
jest.useRealTimers()¶
Restore real timers:
jest.advanceTimersByTime(ms)¶
Advance fake timers:
jest.runAllTimers()¶
Run all pending timers:
jest.clearAllMocks()¶
Clear all mock call history:
jest.resetAllMocks()¶
Reset all mocks to initial state:
jest.restoreAllMocks()¶
Restore all mocks to original implementations:
Mock Function Methods¶
mockFn.mockReturnValue(value)¶
Set return value:
mockFn.mockReturnValueOnce(value)¶
Set return value for next call only:
const mock = jest.fn()
.mockReturnValueOnce(1)
.mockReturnValueOnce(2)
.mockReturnValue(3);
expect(mock()).toBe(1);
expect(mock()).toBe(2);
expect(mock()).toBe(3);
expect(mock()).toBe(3);
mockFn.mockResolvedValue(value)¶
Return a resolved promise:
mockFn.mockRejectedValue(error)¶
Return a rejected promise:
const mock = jest.fn().mockRejectedValue(new Error('fail'));
await expect(mock()).rejects.toThrow('fail');
mockFn.mockImplementation(fn)¶
Set mock implementation:
mockFn.mockClear()¶
Clear call history:
mock();
mock();
expect(mock).toHaveBeenCalledTimes(2);
mock.mockClear();
expect(mock).toHaveBeenCalledTimes(0);
mockFn.mockReset()¶
Clear calls and reset return value:
mockFn.mockRestore()¶
Restore original (for spies):
mockFn.mock.calls¶
Array of call arguments:
mockFn.mock.results¶
Array of return values: