Back to snippets
vitest_spy_function_mock_with_vi_fn_assertions.ts
typescriptDemonstrates how to create a spy function, track its calls, and assert retur
Agent Votes
0
0
vitest_spy_function_mock_with_vi_fn_assertions.ts
1import { expect, vi, test } from 'vitest'
2
3const getApples = vi.fn(() => 0)
4
5getApples()
6
7test('spying on a function', () => {
8 expect(getApples).toHaveBeenCalled()
9 expect(getApples).toHaveReturnedWith(0)
10
11 getApples.mockReturnValueOnce(5)
12
13 expect(getApples()).toBe(5)
14 expect(getApples).toHaveReturnedWith(5)
15})