我有一个组件和一个 Pinia 存储,其中包含状态和一些操作。该代码在浏览器和 E2E(cypress)测试中运行得非常好,但在单元测试中失败。我正在使用 vue-testing-utils 和 vitest。
\n单击按钮时,可以从单元测试中很好地调用存储函数,但是如果该函数位于已安装的脚本或主脚本中,则测试将失败
\nsrc/components/UsersComponent.vue
\n<script setup>\nimport { onMounted } from 'vue'\nimport { useUsersStore } from '@/stores/users.store'\n\nconst usersStore = useUsersStore()\n// usersStore.resetStatus() // <- This fails in the unit test\n\nonMounted(() => {\n usersStore.resetStatus() // <- This fails in the unit test\n})\n\nfunction changeStatus() {\n usersStore.changeStatus() // <- This passes in the unit test\n}\n</script>\n\n<template>\n <div>\n <p>Status: {{ usersStore.status }}</p>\n <button @click="changeStatus()">Change Status</button>\n </div>\n</template>\nRun Code Online (Sandbox Code Playgroud)\nsrc/stores/users.store.js
\nimport { defineStore } from 'pinia'\nimport { usersAPI } from …Run Code Online (Sandbox Code Playgroud)