常见问题
使用 Vitest 模拟日期和计时器
Vue 的调度器依赖于系统时间。请务必在调用 vi.setSystemTime 后再挂载组件,因为 Vue 依赖于其副作用。在调用 vi.setSystemTime 之前挂载组件可能会破坏其响应性。
Vue warn: Failed setting prop
[Vue warn]: Failed setting prop "prefix" on <component-stub>: value foo is invalid.
TypeError: Cannot set property prefix of #<Element> which has only a getter当您使用 shallowMount 或具有与 Element 共享的属性名称的 stubs 时,会显示此警告。
与 Element 共享的常见属性名称:
attributeschildrenprefix
请参阅:https://developer.mozilla.org/zh-CN/docs/Web/API/Element
可能的解决方案
- 使用
mount而不是shallowMount进行不带测试替身的渲染 - 通过模拟
console.warn来忽略警告 - 重命名 prop,以避免与
Element属性冲突