Predict toHaveBeenCalledWith
Predict Output Medium
Question
Will this test pass or fail?
Code
const mockFn = jest.fn();
mockFn('hello', 'world');
test('called with args', () => {
expect(mockFn).toHaveBeenCalledWith('hello', 'world');
});
Hint
The mock was called with 'hello' and 'world'.
Explanation
The test passes. toHaveBeenCalledWith('hello', 'world') checks that the mock was called with exactly those arguments at least once. Since mockFn('hello', 'world') was called with those exact arguments, the assertion succeeds.
Related Resources
Related Lessons
- Mocking
Using mocks to isolate code.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.