---
source: https://iinventedlight.com/library/notes/vitest-mocking.md
title: "Vitest mocking builds fake stand-ins through the vi helper"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (vitest-dev/vitest docs and source); prose restates cited source lines in own words"
license_family: vitest-mit
attribution: "Summary by iinventedlight (AI-assisted). Sources: Vitest docs and Vitest source (vitest-dev/vitest), MIT. Notices: Vitest documentation and source (vitest-dev/vitest): Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors; MIT (https://raw.githubusercontent.com/vitest-dev/vitest/main/LICENSE)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-11
status: published
approved_by: "Cole Withington, 2026-11-11 17:50 PT, Librarian chat widget t44s275: \"Yes, publish all 8 gap drafts\""
approved_by: "Cole Withington, 2026-11-11 17:59 PT, Engineer chat widget t716s3194: \"Yes, publish all 8 gap notes live\""
checked_on: 2026-11-11
as_of: 2026-11-11
confidence: medium
untested: "No Vitest project was run for this note; only the fetched documentation and source files were read."
category: coding-references
topic_area: "code-web: Vitest mocking"
tags: [vitest, mocking, spy, vi]
client: Librarian
sources:
  - id: S1
    title: "Mocking"
    publisher: Vitest docs
    url: https://vitest.dev/guide/mocking
    accessed: 2026-11-11
    license: "MIT"
  - id: S2
    title: "packages/spy"
    publisher: Vitest source
    url: https://github.com/vitest-dev/vitest/blob/main/packages/spy/src/index.ts
    accessed: 2026-11-11
    license: "MIT"
---
# Vitest mocking builds fake stand-ins through the vi helper

**Answer:** Mocking means creating a fake version of an internal or external service. Vitest exposes that work through the `vi` helper, which you import from `vitest` or use globally when `global` is enabled. If you are not familiar with `vi.fn`, `vi.mock` or `vi.spyOn` methods, the API section covers them.

## Resetting and limits

Clear or restore mocks before or after each test so mock state does not leak between runs. A `vi.mock` call is hoisted to the top of the file and runs before imports. Some `vi.spyOn` patterns on module exports do not work in browser mode.

## The spy helpers in source

`fn` wraps a function in a mock instance, and if you pass a value that is already a mock it returns that same function. `isMockFunction` is true when `fn` is a function, `'_isMockFunction'` is in `fn`, and `fn._isMockFunction === true`. `spyOn` requires a defined object as its first argument.

## See also

- [vitest-test-runner](https://iinventedlight.com/library/notes/vitest-test-runner/)
- [vitest-expect-assertions](https://iinventedlight.com/library/notes/vitest-expect-assertions/)

## Claims

- id: C1
  claim: A fake stand-in for a service is commonly called mocking.
  source: S1
  evidence: "This is commonly referred to as **mocking**."

- id: C2
  claim: Vitest provides mocking utilities through its vi helper.
  source: S1
  evidence: "Vitest provides utility functions to help you out through its `vi` helper."

- id: C3
  claim: fn returns the same function if it is already a mock.
  source: S2
  evidence: "if the function is already a mock, just return the same function,"

- id: C4
  claim: isMockFunction is true when the function has the _isMockFunction flag.
  source: S2
  evidence: "typeof fn === 'function' && '_isMockFunction' in fn && fn._isMockFunction === true"

## Sources

- [S1] Mocking. Vitest docs. https://vitest.dev/guide/mocking . License: MIT. Accessed 2026-11-11.

- [S2] packages/spy. Vitest source. https://github.com/vitest-dev/vitest/blob/main/packages/spy/src/index.ts . License: MIT. Accessed 2026-11-11.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: Vitest documentation and source (vitest-dev/vitest): Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors; MIT (https://raw.githubusercontent.com/vitest-dev/vitest/main/LICENSE). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
