Skip to content

Unit test for page components fail when layout is used #293

@fago

Description

@fago

A unit test like this fails

import { describe, it, expect } from 'vitest'
import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
import App from '~/app.vue'

describe('Testing routes', () => {
  registerEndpoint('/api/drupal-ce/prenode', () => ({
    content: {
      element: 'drupal-markup',
      body: '<p>Some <b>example</b> body.</p>'
    },
    local_tasks: {},
    messages: [],
    metatags: { meta: [], link: [], jsonld: [] }
  }))

  registerEndpoint('/api/menu/api/menu_items/main', () => ([
    { title: 'Test page', relative: '/node/1' }
  ]))

  it('renders preview/node with direct backend request, skipping drupal-ce-proxy', async () => {
    const component = await mountSuspended(App, {
      route: '/prenode'
    })
    expect(component.html()).toContain('<p>Some <b>example</b> body.</p>')
  })

})

when the default [...[.vue is used with some layout like

<template>
  <div>
    <NuxtLayout name="default">
      <main>
        <component :is="renderCustomElements(page.content)" />
      </main>
    </NuxtLayout>
  </div>
</template>

<script lang="ts" setup>
const { fetchPage, renderCustomElements } = useDrupalCe()
const page = await fetchPage(useRoute().path, { query: useRoute().query })

</script>

When the NuxtLayout component is removed, it works. Also when the component WITH the NuxtLayout is copied to prenode.vue page component, such that the fallback is not used, then it works also! It seems the mocking of routes of nuxt test utils somehow fails when the fallback component is in use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions