NuxtN
Nuxt13mo ago
Yuri

The test ignores useNuxtApp

Hello, everyone!
I need help with unit-testing.
My component LocationSelection.vue:
<template>
<div>{{test}}</div>
</template>

<script lang="ts" setup>
const test = ref('111')
const fetchCities = async () => {
  test.value = '222';
  const nuxtApp = useNuxtApp()
  await nuxtApp.$api(apiPath.location.suggest.city)
  test.value = '333';
}

onMounted(async () => {
  await fetchCities()
})
</script>

My LocationSelection.nuxt.spec.ts:
import { describe, it, expect } from 'vitest'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import LocationSelection from '../LocationSelection.vue'

describe('LocationSelection.vue', () => {
  it('test', async () => {
    const wrapper = await mountSuspended(LocationSelection)
    await wrapper.vm.$nextTick()
    console.log(wrapper.html())
    expect(wrapper.text()).toContain('333')
  })
})

But why do I receive 222 instead 333?
AssertionError: expected '222' to contain '333'
Was this page helpful?