testhelper.ts 964 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { globalComponents } from '@/components'
  2. import router from '@/router'
  3. import { ComponentMountingOptions, mount } from '@vue/test-utils'
  4. import { createTestingPinia, TestingOptions } from '@pinia/testing'
  5. import { ComponentPublicInstance, Plugin } from 'vue'
  6. import { createPinia, setActivePinia } from 'pinia'
  7. export function mountComponent<T extends ComponentPublicInstance>(
  8. component: T,
  9. options: ComponentMountingOptions<T> = { shallow: false },
  10. mockStore?: TestingOptions,
  11. useRouter = false
  12. ) {
  13. const pinia = mockStore
  14. ? createTestingPinia({ createSpy: vi.fn, ...mockStore })
  15. : createPinia()
  16. setActivePinia(pinia)
  17. const plugins: Array<Plugin> = [pinia, globalComponents]
  18. if (useRouter) {
  19. plugins.push(router)
  20. }
  21. if (options.global?.plugins) {
  22. options.global.plugins.push(...plugins)
  23. } else {
  24. options.global = {
  25. ...(options.global || {}),
  26. plugins
  27. }
  28. }
  29. return mount(component, options)
  30. }