gpt4 book ai didi

javascript - 运行单元测试时,在 onMounted 中未定义 Vue Pinia 函数

转载 作者:行者123 更新时间:2023-12-05 00:33:22 27 4
gpt4 key购买 nike

我有一个组件和一个包含状态和一些操作的 Pinia 商店。该代码在浏览器和 E2E (cypress) 测试中运行良好,但在单元测试中失败。我正在使用 vue-testing-utils 和 vitest。
当单击按钮时,可以从单元测试中很好地调用 store 函数,但如果该函数在挂载或主脚本中,则测试失败
src/components/UsersComponent.vue

<script setup>
import { onMounted } from 'vue'
import { useUsersStore } from '@/stores/users.store'

const usersStore = useUsersStore()
// usersStore.resetStatus() // <- This fails in the unit test

onMounted(() => {
usersStore.resetStatus() // <- This fails in the unit test
})

function changeStatus() {
usersStore.changeStatus() // <- This passes in the unit test
}
</script>

<template>
<div>
<p>Status: {{ usersStore.status }}</p>
<button @click="changeStatus()">Change Status</button>
</div>
</template>
src/stores/users.store.js
import { defineStore } from 'pinia'
import { usersAPI } from '@/gateways'

export const useUsersStore = defineStore({
id: 'users',
persist: true,

state: () => ({
status: 'ready',
}),

getters: {},

actions: {
resetStatus() {
this.status = 'ready'
},
changeStatus() {
this.status = 'loading'
},
},
})
src/组件/测试 /UsersComponent.spec.js
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createTestingPinia } from '@pinia/testing'

import UsersComponent from '@/components/UsersComponent.vue'
import { useUsersStore } from '@/stores/users.store'

const wrapper = mount(UsersComponent, {
global: {
plugins: [createTestingPinia({ createSpy: vi.fn() })],
},
})
const usersStore = useUsersStore()

describe('UsersComponent', () => {
it('store function is called', async () => {
// arrange
const spy = vi.spyOn(usersStore, 'resetStatus')
const button = wrapper.find('button')

// act
await button.trigger('click')

// assert
expect(spy).toHaveBeenCalled()
})
})
单元测试返回 2 个不同的错误。第一个是函数尝试在 onMounted() 中运行时的控制台日志。第二个是 vitest 返回的内容。
stderr | unknown test
[Vue warn]: Unhandled error during execution of mounted hook
at <UsersComponent ref="VTU_COMPONENT" >
at <VTUROOT>
 FAIL  src/components/__tests__/UsersComponent.spec.js [ src/components/__tests__/UsersComponent.spec.js ]
TypeError: usersStore.resetStatus is not a function
❯ src/components/UsersComponent.vue:16:14
16|
17| <template>
18| <div>
| ^
19| <p>Status: {{ usersStore.status }}</p>
20| <button @click="changeStatus()">Change Status</button>
我知道这个例子有点基础,并没有真正的目的,但我想知道如何在 onMounted() 中拥有存储功能。 (或类似的地方)而不会破坏我所有的单元测试。

最佳答案

也许这对您有用:

describe('UsersComponent',  () => {
it('changeStatus function is called', async () => {
const wrapper = mount(UsersComponent, {
mounted: vi.fn(), // With this you mock the onMounted
global: {
plugins: [createTestingPinia({
initialState: { // Initialize the state
users: { status: 'ready' },
}
})]
}
})

// Spy the method you call...
const spy = vi.spyOn(wrapper.vm, 'changeStatus');

wrapper.vm.changeStatus()

expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
})
})

关于javascript - 运行单元测试时,在 onMounted 中未定义 Vue Pinia 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71746275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com