From 4ff1a954b3d9fc79fe2c20490f1e31025d232dc5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 26 Feb 2022 04:14:53 +0800 Subject: [PATCH] test: add more tests (#3392) --- packages/test-utils/src/server.ts | 2 +- packages/test-utils/src/setup/index.ts | 3 +++ packages/test-utils/src/types.ts | 2 ++ test/basic.test.ts | 28 +++++++++++++++++++++-- test/fixtures/basic/composables/foo.ts | 3 +++ test/fixtures/basic/composables/useBar.ts | 3 +++ test/fixtures/basic/pages/index.vue | 9 ++++++-- 7 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/basic/composables/foo.ts create mode 100644 test/fixtures/basic/composables/useBar.ts diff --git a/packages/test-utils/src/server.ts b/packages/test-utils/src/server.ts index 562700b8d8..6520947860 100644 --- a/packages/test-utils/src/server.ts +++ b/packages/test-utils/src/server.ts @@ -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') ], { diff --git a/packages/test-utils/src/setup/index.ts b/packages/test-utils/src/setup/index.ts index 743d09e387..addfafcfe0 100644 --- a/packages/test-utils/src/setup/index.ts +++ b/packages/test-utils/src/setup/index.ts @@ -23,6 +23,9 @@ export function createTest (options: Partial): TestHooks { } const afterAll = async () => { + if (ctx.serverProcess) { + ctx.serverProcess.kill() + } if (ctx.nuxt && ctx.nuxt.options.dev) { await ctx.nuxt.close() } diff --git a/packages/test-utils/src/types.ts b/packages/test-utils/src/types.ts index b9e854b6b5..cca1c3b7a2 100644 --- a/packages/test-utils/src/types.ts +++ b/packages/test-utils/src/types.ts @@ -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 { diff --git a/test/basic.test.ts b/test/basic.test.ts index 17295938e2..f937d7e5d0 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -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 components + expect(index).toContain('Basic fixture') + // 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') }) }) diff --git a/test/fixtures/basic/composables/foo.ts b/test/fixtures/basic/composables/foo.ts new file mode 100644 index 0000000000..597405af68 --- /dev/null +++ b/test/fixtures/basic/composables/foo.ts @@ -0,0 +1,3 @@ +export function useFoo () { + return 'auto imported from ~/components/foo.ts' +} diff --git a/test/fixtures/basic/composables/useBar.ts b/test/fixtures/basic/composables/useBar.ts new file mode 100644 index 0000000000..4775ea5727 --- /dev/null +++ b/test/fixtures/basic/composables/useBar.ts @@ -0,0 +1,3 @@ +export default function useFoo () { + return 'auto imported from ~/components/useBar.ts' +} diff --git a/test/fixtures/basic/pages/index.vue b/test/fixtures/basic/pages/index.vue index bf4b2a5e17..49bdab3c3b 100644 --- a/test/fixtures/basic/pages/index.vue +++ b/test/fixtures/basic/pages/index.vue @@ -4,11 +4,16 @@ Basic fixture

Hello Nuxt 3!

-
Config: {{ $config.testConfig }}
+
RuntimeConfig: {{ config.testConfig }}
+
{{ foo }}
+
{{ bar }}