Yuri
The test ignores useNuxtApp
@kapa.ai
import {describe, it, expect, vi} from 'vitest'
import {mockNuxtImport, mountSuspended} from '@nuxt/test-utils/runtime'
import LocationSelection from '../LocationSelection.vue'
import {flushPromises} from "@vue/test-utils";
describe('LocationSelection.vue', () => {
it('test', async () => {
// Mock the $api method to return a resolved promise
const mockApi = vi.fn().mockResolvedValue({ data: 'mocked data' })
const wrapper = await mountSuspended(LocationSelection, {
globals: {
provide: {
nuxtApp: {
$api: mockApi
}
}
}
})
await wrapper.vm.$nextTick()
await flushPromises()
console.log(wrapper.html())
expect(wrapper.text()).toContain('333')
})
})
Not working
import { describe, it, expect } from 'vitest'
import {mockNuxtImport, mountSuspended} from '@nuxt/test-utils/runtime'
import LocationSelection from '../LocationSelection.vue'
describe('LocationSelection.vue', () => {
mockNuxtImport('useNuxtApp', () => {
return () => ({
$api: () => Promise.resolve() // Mock the $api method
})
})
it('test', async () => {
const wrapper = await mountSuspended(LocationSelection)
await wrapper.vm.$nextTick()
console.log(wrapper.html())
expect(wrapper.text()).toContain('333')
})
})
Not working
13 replies
useNuxtApp && mountSuspended
@kapa.ai with first solution I get errors:
[Vue warn]: Component is missing template or render function: { name: 'MountSuspendedHelper', setup: [AsyncFunction: setup] }
at <MountSuspendedHelper>
at <Anonymous ref="VTU_COMPONENT" >
at <VTUROOT>
❯ components/Location/tests/LocationSelection.nuxt.spec.ts (1)
❯ LocationSelection.vue (1)
× test
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL components/Location/tests/LocationSelection.nuxt.spec.ts > LocationSelection.vue > test
TypeError: nuxtApp.deferHydration is not a function
❯ Object.setup node_modules/nuxt/dist/app/components/nuxt-root.vue:32:27
30|
31| const IslandRenderer = import.meta.server && componentIslands
32| ? defineAsyncComponent(() => import('./island-renderer').then(r => r.default || r))
| ^
33| : () => null
34|
❯ setup node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs:107:50
❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:200:19
❯ setupStatefulComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7788:25
❯ setupComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7749:36
❯ mountComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5104:7
❯ processComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5070:9
❯ patch node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:4599:11
❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5214:11
❯ ReactiveEffect.run node_modules/@vue/runtime-core/node_modules/@vue/reactivity/dist/reactivity.cjs.js:226:19
10 replies
Unit Test OnMounted
@kapa.ai
My current test:
But I receive error:
TypeError: nuxtApp.deferHydration is not a function
❯ Object.setup node_modules/nuxt/dist/app/components/nuxt-root.vue:32:27
30|
31| const IslandRenderer = import.meta.server && componentIslands
32| ? defineAsyncComponent(() => import('./island-renderer').then(r => r.default || r))
| ^
33| : () => null
34|
❯ setup node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs:107:50
❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:200:19
❯ setupStatefulComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7788:25
❯ setupComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7749:36
Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
29 replies