当前位置: 首页 > news >正文

[Vitest] mockClear, mockReset, mockRestore

mockClear

Clears all information about every call. After calling it, all properties on .mock will return to their initial state. This method does not reset

const person = {greet: (name: string) => `Hello ${name}`,
}
const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
expect(person.greet('Alice')).toBe('mocked')
expect(spy.mock.calls).toEqual([['Alice']])
expect(spy).toHaveBeenCalledOnce()// clear call history but keep mock implementation
spy.mockClear()
expect(spy.mock.calls).toEqual([])
expect(person.greet('Bob')).toBe('mocked')
expect(spy.mock.calls).toEqual([['Bob']])
expect(spy).toHaveBeenCalledOnce()

As if the spy never called previously

 

mockReset

Does what mockClear does and resets the mock implementation. This also resets all "once" implementations.

Note that resetting a mock from vi.fn() will set the implementation to an empty function that returns undefined. Resetting a mock from vi.fn(impl) will reset the implementation to impl.

This is useful when you want to reset a mock to its original state.

const person = {greet: (name: string) => `Hello ${name}`,
}
const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
expect(person.greet('Alice')).toBe('mocked')
expect(spy.mock.calls).toEqual([['Alice']])// clear call history and reset implementation, but method is still spied
spy.mockReset()
expect(spy.mock.calls).toEqual([])
expect(person.greet).toBe(spy)
expect(person.greet('Bob')).toBe('Hello Bob')
expect(spy.mock.calls).toEqual([['Bob']])

As if we never configure the spy implementation or return value

 

mockRestore

Does what mockReset does and restores the original descriptors of spied-on objects, if the mock was created with vi.spyOn.

mockRestore on a vi.fn() mock is identical to mockReset.

const person = {greet: (name: string) => `Hello ${name}`,
}
const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
expect(person.greet('Alice')).toBe('mocked')
expect(spy.mock.calls).toEqual([['Alice']])// clear call history and restore spied object method
spy.mockRestore()
expect(spy.mock.calls).toEqual([])
expect(person.greet).not.toBe(spy)
expect(person.greet('Bob')).toBe('Hello Bob')
expect(spy.mock.calls).toEqual([])

As if we first initlize spy with no extra confguration

 

Resetting Mocks and Spies Between Tests

beforeEach(() => {vi.clearAllMocks()vi.resetAllMocks()vi.restoreAllMocks()
})// or global
defineConfig({test: {...restoreMocks: true,clearMocks: true,mockReset: true}
})

 

http://www.jsqmd.com/news/425149/

相关文章:

  • 沙拉查词 + AnkiConnect 完整操作指南
  • 设计模式--装饰器模式
  • C++进阶之bind绑定:用法实例(四百四十二)
  • 初中数学基础差?2026实测4家靠谱线上机构,精准补漏不踩坑|家长收藏 - 品牌测评鉴赏家
  • 改进粒子群算法优化混合储能系统容量配置程序
  • 《从0到1!AI应用架构师对比学习实践的快速入门指南》
  • Tic Tac DREAMIN’
  • go基础之流程控制
  • 中考数学提分|实测4家主流线上机构,避坑不踩雷,直接抄作业 - 品牌测评鉴赏家
  • 2026青木川古镇民宿权威排名|青云客栈蝉联第一,自驾亲子首选(附避坑指南) - 一个呆呆
  • 初中数学线上培训机构推荐|4家实测不踩坑,适配不同基础孩子 - 品牌测评鉴赏家
  • if language is ONLY for the sounds for chating。
  • 初中数学培优选对线上机构,少走1年弯路!实测4家主流平台,家长直接抄作业 - 品牌测评鉴赏家
  • 小学数学培优|2026实测3家线上机构,家长闭眼冲不踩坑 - 品牌测评鉴赏家
  • 100种思维模型概念(多个角度分析问题)
  • 小学数学基础差?4家靠谱线上机构实测推荐!家长闭眼抄作业 - 品牌测评鉴赏家
  • 多项式和生成函
  • 背单词 纯英文 2026年03月
  • 冲刺中考数学哪家线上辅导班好?实测5家,家长闭眼冲不踩坑 - 品牌测评鉴赏家
  • .NET周刊【月第期 --】
  • 定速风电机组:老派硬核选手的倔强
  • 成人高考在2026年怎么选?主要类型与适配场景分析 - 速递信息
  • 2026年单北斗GNSS水库变形监测系统推荐排行榜
  • 初中数学基础差?3家靠谱线上机构实测!避坑不花冤枉钱 - 品牌测评鉴赏家
  • 一生一芯学习:PA:输入输出
  • 初中数学线上培训实测!4家机构盘点,提分不踩坑(家长必看) - 品牌测评鉴赏家
  • 孩子皮肤敏感易泛红适配面霜品牌推荐 - 速递信息
  • 中考数学冲刺|实测!不踩坑、真提分,家长直接抄作业 - 品牌测评鉴赏家
  • ZKEACMS:基于ASP.Net Core开发的开源免费内容管理系统
  • 基于MATLAB/Simulink的电动车制动能量回收控制策略搭建及整车参数分析