Skip to content

常见问题

¥FAQ

[[目录]]

¥[[toc]]

使用 Vitest 模拟日期和计时器

¥Mocking Dates and Timers with Vitest

Vue 的调度程序取决于系统时间。确保在调用 vi.setSystemTime 之后挂载组件,因为 Vue 依赖于它的副作用。在调用 vi.setSystemTime 之前挂载组件可能会导致反应性中断。

¥Vue's scheduler depends on the system time. Make sure to mount components after calling vi.setSystemTime, since Vue depends on its side effects. Mounting components before calling vi.setSystemTime may cause breaks in reactivity.

参见 vuejs/测试工具#2074

¥See vuejs/test-utils#2074.

Vue 警告:设置属性失败

¥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

如果你使用 shallowMountstubs 的属性名称与 Element 共享,则会显示此警告。

¥This warning is shown in case you are using shallowMount or stubs with a property name that is shared with Element.

Element 共享的公共属性名称:

¥Common property names that are shared with Element:

  • attributes

  • children

  • prefix

看:https://web.nodejs.cn/en-US/docs/Web/API/Element

¥See: https://web.nodejs.cn/en-US/docs/Web/API/Element

可能的解决方案

¥Possible solutions

  1. 使用 mount 而不是 shallowMount 进行无存根渲染

    ¥Use mount instead of shallowMount to render without stubs

  2. 通过模拟 console.warn 来忽略警告

    ¥Ignore the warning by mocking console.warn

  3. 重命名属性以免与 Element 属性冲突

    ¥Rename the prop to not clash with Element properties

VueTestUtils v2.4 中文网 - 粤ICP备13048890号