test: add more tests (#3392)

This commit is contained in:
Anthony Fu 2022-02-26 04:14:53 +08:00 committed by GitHub
parent 73ba30fb69
commit 4ff1a954b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 5 deletions

View File

@ -24,7 +24,7 @@ export async function listen () {
const port = await getPort({ host, random: true }) const port = await getPort({ host, random: true })
ctx.url = 'http://localhost:' + port ctx.url = 'http://localhost:' + port
execa('node', [ ctx.serverProcess = execa('node', [
// @ts-ignore // @ts-ignore
resolve(ctx.nuxt.options.nitro.output.dir, 'server/index.mjs') resolve(ctx.nuxt.options.nitro.output.dir, 'server/index.mjs')
], { ], {

View File

@ -23,6 +23,9 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
} }
const afterAll = async () => { const afterAll = async () => {
if (ctx.serverProcess) {
ctx.serverProcess.kill()
}
if (ctx.nuxt && ctx.nuxt.options.dev) { if (ctx.nuxt && ctx.nuxt.options.dev) {
await ctx.nuxt.close() await ctx.nuxt.close()
} }

View File

@ -1,4 +1,5 @@
import type { Nuxt, NuxtConfig } from '@nuxt/schema' import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import type { ExecaChildProcess } from 'execa'
import type { Browser, LaunchOptions } from 'playwright' import type { Browser, LaunchOptions } from 'playwright'
export type TestRunner = 'vitest' | 'jest' export type TestRunner = 'vitest' | 'jest'
@ -28,6 +29,7 @@ export interface TestContext {
nuxt?: Nuxt nuxt?: Nuxt
browser?: Browser browser?: Browser
url?: string url?: string
serverProcess?: ExecaChildProcess
} }
export interface TestHooks { export interface TestHooks {

View File

@ -7,7 +7,31 @@ describe('fixtures:basic', async () => {
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)), rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
server: true 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')
}) })
}) })

View File

@ -0,0 +1,3 @@
export function useFoo () {
return 'auto imported from ~/components/foo.ts'
}

View File

@ -0,0 +1,3 @@
export default function useFoo () {
return 'auto imported from ~/components/useBar.ts'
}

View File

@ -4,11 +4,16 @@
<Title>Basic fixture</Title> <Title>Basic fixture</Title>
</Head> </Head>
<h1>Hello Nuxt 3!</h1> <h1>Hello Nuxt 3!</h1>
<div>Config: {{ $config.testConfig }}</div> <div>RuntimeConfig: {{ config.testConfig }}</div>
<div>{{ foo }}</div>
<div>{{ bar }}</div>
<CustomComponent /> <CustomComponent />
</div> </div>
</template> </template>
<script setup> <script setup>
const $config = useRuntimeConfig() const config = useRuntimeConfig()
const foo = useFoo()
const bar = useBar()
</script> </script>