2

I'm busy implementing unit tests for our Nuxt application. Testing components that don't use any Nuxt modules run fine without any issues but testing a component with an installed Nuxt module throws Error: Nuxt instance is unavailable!

The test file

Logo.nuxt.test.ts

import { mountSuspended } from 'nuxt-vitest/utils'

import Logo from '~/components/product/Logo.vue'

const createWrapper = () => mountSuspended(Logo, {
  props: {
    alt: 'test',
    src: 'test',
    sizes: 'test',
    width: 20,
    height: 20,
    preset: 'card-list-logo',
  },
  global: {
    stubs: ['nuxt-img'],
  },
})

describe('Product list logo', () => {
  test('test', async () => {
    const wrapper = await createWrapper()
    console.log(wrapper.html())
  })
})

The component

<script setup lang="ts">
// https://image.nuxt.com/usage/nuxt-img

defineProps<{
  alt: string
  src: string
  sizes: string
  width: number
  height: number
  preset: 'card-list-logo' | 'product-detail-logo'
}>()
</script>

<template>
  <figure>
    <NuxtImg
      class="product-logo"
      :src="src"
      :alt="alt"
      :placeholder="[width, height]"
      :width="width"
      :height="height"
      :sizes="sizes"
      :preset="preset"
      loading="lazy"
    />
  </figure>
</template>

The Error

Error: Nuxt instance is unavailable!
 ❯ useNuxt ../node_modules/@nuxt/kit/dist/index.mjs:32:11
 ❯ installNuxtModule ../node_modules/nuxt/dist/chunks/features.mjs:41:16
 ❯ ../node_modules/nuxt/dist/index.mjs:1290:77

When I comment out NuxtImg it works fine. This is the @nuxt/image module.

NuxtLink tests without issues so I expect the issue to be inside of NuxtImg

Hoping someone can shine some light on this.

I also tried stubbing it out but that didn't work

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.