Skip to content

Commit 2c41fd6

Browse files
committed
test: adding tests for Nuxt Content prose components and useRuntimeConfig
1 parent af1265c commit 2c41fd6

File tree

9 files changed

+527
-19
lines changed

9 files changed

+527
-19
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<ProseH3>{{ heading }}</ProseH3>
3+
<ProseP>{{ content }}</ProseP>
4+
</template>
5+
6+
<script lang="ts" setup>
7+
defineProps<{
8+
heading: string
9+
content: string
10+
}>()
11+
</script>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<h1>Use Runtime Config</h1>
3+
<pre>{{ config }}</pre>
4+
</template>
5+
6+
<script lang="ts" setup>
7+
const config = useRuntimeConfig()
8+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>This is an auto-imported component</div>
3+
</template>

examples/content/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
{
2+
"name": "example-app-vitest-content",
23
"private": true,
34
"type": "module",
45
"scripts": {
56
"build": "nuxt build",
67
"dev": "nuxt dev",
78
"dev:prepare": "nuxt prepare",
89
"generate": "nuxt generate",
9-
"test": "vitest run",
10+
"test": "pnpm test:nuxt-environment && pnpm test:browser",
11+
"test:browser": "VITEST_BROWSER_ENABLED=true vitest run",
12+
"test:nuxt-environment": "vitest run",
1013
"preview": "nuxt preview"
1114
},
1215
"devDependencies": {
13-
"@nuxt/content": "3.5.1",
16+
"@nuxt/content": "^3.5.1",
1417
"@nuxt/test-utils": "latest",
15-
"nuxt": "3.17.2",
16-
"vitest": "3.1.2"
18+
"nuxt": "3.17.1",
19+
"vitest": "3.1.2",
20+
"@vitest/browser": "3.1.2",
21+
"vitest-browser-vue": "^0.2.0"
1722
}
1823
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime'
3+
import { render } from 'vitest-browser-vue'
4+
import App from '~/app.vue'
5+
6+
// Example usage: queryCollection(pages).path('/').first()
7+
mockNuxtImport('queryCollection', () => (_id: string) => ({
8+
path: (_path: string) => ({
9+
first: () => ({
10+
title: 'My page',
11+
}),
12+
}),
13+
}))
14+
15+
describe('App', () => {
16+
it.skip('works with mountSuspended', async () => {
17+
const component = await mountSuspended(App)
18+
expect(component.html()).toMatchInlineSnapshot(`"<div>Index page</div><div>title: My page</div>"`)
19+
})
20+
21+
it.skip('works with vitest-browser-vue', async () => {
22+
const { getByText } = await render(App)
23+
expect(getByText('Index page')).toBeInTheDocument()
24+
expect(getByText('title: My page')).toBeInTheDocument()
25+
})
26+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect, it } from 'vitest'
2+
import { render } from 'vitest-browser-vue'
3+
import { SomeComponent, ProseH3, ProseP, ComponentWithUseRuntimeConfig, ComponentWithProse } from '#components'
4+
5+
it('should render any component', async () => {
6+
const { getByText } = await render(SomeComponent)
7+
expect(getByText('This is an auto-imported component')).toBeInTheDocument()
8+
})
9+
10+
it('should render a ProseP component', async () => {
11+
const { getByText } = await render(ProseP, { slots: { default: 'This is a Prose Paragraph component' } })
12+
expect(getByText('This is a Prose Paragraph component')).toBeInTheDocument()
13+
})
14+
15+
it.skip('should render a ProseH3 component', async () => {
16+
const { getByText } = await render(ProseH3, { slots: { default: 'This is a Prose heading component' } })
17+
expect(getByText('This is a Prose heading component')).toBeInTheDocument()
18+
})
19+
20+
it.skip('should render a ComponentWithUseRuntimeConfig component', async () => {
21+
const { getByText } = await render(ComponentWithUseRuntimeConfig)
22+
expect(getByText('Use Runtime Config')).toBeInTheDocument()
23+
})
24+
25+
it.skip('should render a ComponentWithProse component', async () => {
26+
const headingText = 'Text rendered within ProseH3'
27+
const paragraphText = 'Text rendered within p'
28+
const { getByText } = await render(ComponentWithProse, { props: { heading: headingText, content: paragraphText } })
29+
expect(getByText(headingText)).toBeInTheDocument()
30+
expect(getByText(paragraphText)).toBeInTheDocument()
31+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect, it } from 'vitest'
2+
import { mountSuspended } from '@nuxt/test-utils/runtime'
3+
import { ComponentWithUseRuntimeConfig, ComponentWithProse, SomeComponent, ProseH3, ProseP } from '#components'
4+
5+
it('should render any component', async () => {
6+
const component = await mountSuspended(SomeComponent)
7+
expect(component.html()).toContain('This is an auto-imported component')
8+
})
9+
10+
it('should render a ProseH3 component', async () => {
11+
const headingText = 'This is a Prose Heading component'
12+
const component = await mountSuspended(ProseH3, { slots: { default: headingText } })
13+
expect(component.html()).toContain(headingText)
14+
})
15+
16+
it('should render a ProseP component', async () => {
17+
const paragraphText = 'This is a Prose Paragraph component'
18+
const component = await mountSuspended(ProseP, { slots: { default: paragraphText } })
19+
expect(component.html()).toContain(paragraphText)
20+
})
21+
22+
it('should render a ComponentWithUseRuntimeConfig component', async () => {
23+
const component = await mountSuspended(ComponentWithUseRuntimeConfig)
24+
expect(component.html()).toContain('Use Runtime Config')
25+
})
26+
27+
it('should render a ComponentWithProse component', async () => {
28+
const headingText = 'Text rendered within ProseH3'
29+
const paragraphText = 'Text rendered within p'
30+
const component = await mountSuspended(ComponentWithProse, { props: { heading: headingText, content: paragraphText } })
31+
expect(component.html()).toContain(headingText)
32+
expect(component.html()).toContain(paragraphText)
33+
})

examples/content/vitest.config.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { defineVitestConfig } from '@nuxt/test-utils/config'
22

3+
const browserConfig = {
4+
browser: {
5+
enabled: true,
6+
provider: 'playwright',
7+
instances: [{ browser: 'chromium' }],
8+
},
9+
include: ['tests/browser/**/*.spec.ts'],
10+
setupFiles: ['vitest-browser-vue'],
11+
}
12+
13+
const defaultConfig = {
14+
environment: 'nuxt',
15+
exclude: ['tests/browser/**/*.spec.ts', 'node_modules/**', 'dist/**', '.data/**'],
16+
}
17+
318
export default defineVitestConfig({
419
test: {
5-
environment: 'nuxt',
20+
...(process.env.VITEST_BROWSER_ENABLED === 'true' ? browserConfig : defaultConfig),
621
},
722
})

0 commit comments

Comments
 (0)