mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
test: add more tests (#3392)
This commit is contained in:
parent
73ba30fb69
commit
4ff1a954b3
@ -24,7 +24,7 @@ export async function listen () {
|
||||
const port = await getPort({ host, random: true })
|
||||
|
||||
ctx.url = 'http://localhost:' + port
|
||||
execa('node', [
|
||||
ctx.serverProcess = execa('node', [
|
||||
// @ts-ignore
|
||||
resolve(ctx.nuxt.options.nitro.output.dir, 'server/index.mjs')
|
||||
], {
|
||||
|
@ -23,6 +23,9 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
|
||||
}
|
||||
|
||||
const afterAll = async () => {
|
||||
if (ctx.serverProcess) {
|
||||
ctx.serverProcess.kill()
|
||||
}
|
||||
if (ctx.nuxt && ctx.nuxt.options.dev) {
|
||||
await ctx.nuxt.close()
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
|
||||
import type { ExecaChildProcess } from 'execa'
|
||||
import type { Browser, LaunchOptions } from 'playwright'
|
||||
|
||||
export type TestRunner = 'vitest' | 'jest'
|
||||
@ -28,6 +29,7 @@ export interface TestContext {
|
||||
nuxt?: Nuxt
|
||||
browser?: Browser
|
||||
url?: string
|
||||
serverProcess?: ExecaChildProcess
|
||||
}
|
||||
|
||||
export interface TestHooks {
|
||||
|
@ -7,7 +7,31 @@ describe('fixtures:basic', async () => {
|
||||
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
|
||||
server: true
|
||||
})
|
||||
it('Render hello world', async () => {
|
||||
expect(await $fetch('/')).to.contain('Hello Nuxt 3!')
|
||||
|
||||
it('server api', async () => {
|
||||
expect(await $fetch('/api/hello')).toBe('Hello API')
|
||||
expect(await $fetch('/api/hey')).toEqual({
|
||||
foo: 'bar',
|
||||
baz: 'qux'
|
||||
})
|
||||
})
|
||||
|
||||
it('render index.html', async () => {
|
||||
const index = await $fetch('/')
|
||||
|
||||
// Snapshot
|
||||
// expect(index).toMatchInlineSnapshot()
|
||||
|
||||
// should render text
|
||||
expect(index).toContain('Hello Nuxt 3!')
|
||||
// should render <Head> components
|
||||
expect(index).toContain('<title>Basic fixture</title>')
|
||||
// should inject runtime config
|
||||
expect(index).toContain('RuntimeConfig: 123')
|
||||
// should import components
|
||||
expect(index).toContain('This is a custom component with a named export.')
|
||||
// composables auto import
|
||||
expect(index).toContain('auto imported from ~/components/foo.ts')
|
||||
expect(index).toContain('auto imported from ~/components/useBar.ts')
|
||||
})
|
||||
})
|
||||
|
3
test/fixtures/basic/composables/foo.ts
vendored
Normal file
3
test/fixtures/basic/composables/foo.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export function useFoo () {
|
||||
return 'auto imported from ~/components/foo.ts'
|
||||
}
|
3
test/fixtures/basic/composables/useBar.ts
vendored
Normal file
3
test/fixtures/basic/composables/useBar.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default function useFoo () {
|
||||
return 'auto imported from ~/components/useBar.ts'
|
||||
}
|
9
test/fixtures/basic/pages/index.vue
vendored
9
test/fixtures/basic/pages/index.vue
vendored
@ -4,11 +4,16 @@
|
||||
<Title>Basic fixture</Title>
|
||||
</Head>
|
||||
<h1>Hello Nuxt 3!</h1>
|
||||
<div>Config: {{ $config.testConfig }}</div>
|
||||
<div>RuntimeConfig: {{ config.testConfig }}</div>
|
||||
<div>{{ foo }}</div>
|
||||
<div>{{ bar }}</div>
|
||||
<CustomComponent />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
const $config = useRuntimeConfig()
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const foo = useFoo()
|
||||
const bar = useBar()
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user