From 94513939896d4d2050c9405d2cefb48a976d9705 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 17 Dec 2023 08:36:35 +0000 Subject: [PATCH] test: add generate assertion and enable browser --- examples/app-jest/test/browser.e2e.spec.ts | 7 +++--- examples/app-vitest/package.json | 1 + .../{browser.e2e.ts => browser.e2e.spec.ts} | 8 +++--- .../test/{dev.e2e.test.ts => dev.e2e.spec.ts} | 2 +- examples/app-vitest/test/generate.e2e.spec.ts | 25 +++++++++++++++++++ examples/content/package.json | 2 +- pnpm-lock.yaml | 3 +++ 7 files changed, 39 insertions(+), 9 deletions(-) rename examples/app-vitest/test/{browser.e2e.ts => browser.e2e.spec.ts} (55%) rename examples/app-vitest/test/{dev.e2e.test.ts => dev.e2e.spec.ts} (90%) create mode 100644 examples/app-vitest/test/generate.e2e.spec.ts diff --git a/examples/app-jest/test/browser.e2e.spec.ts b/examples/app-jest/test/browser.e2e.spec.ts index 702b9dda4..b1e9dff8d 100644 --- a/examples/app-jest/test/browser.e2e.spec.ts +++ b/examples/app-jest/test/browser.e2e.spec.ts @@ -10,9 +10,8 @@ await setup({ describe('browser', () => { it('runs a test', async () => { const page = await createPage('/') - const text = await page.getByRole('heading',{ name: 'Welcome to Nuxt!' }).innerText() - expect(text).toContain( - 'Welcome to Nuxt!' - ) + const text = await page.getByRole('heading', { name: 'Welcome to Nuxt!' }).innerText() + expect(text).toContain('Welcome to Nuxt!') + await page.close() }) }) diff --git a/examples/app-vitest/package.json b/examples/app-vitest/package.json index 95808fc91..250a0e0c9 100644 --- a/examples/app-vitest/package.json +++ b/examples/app-vitest/package.json @@ -17,6 +17,7 @@ }, "devDependencies": { "@nuxt/test-utils": "latest", + "globby": "^14.0.0", "happy-dom": "^12.10.3", "typescript": "^5.2.2" } diff --git a/examples/app-vitest/test/browser.e2e.ts b/examples/app-vitest/test/browser.e2e.spec.ts similarity index 55% rename from examples/app-vitest/test/browser.e2e.ts rename to examples/app-vitest/test/browser.e2e.spec.ts index 0526ddfd5..b3210cedc 100644 --- a/examples/app-vitest/test/browser.e2e.ts +++ b/examples/app-vitest/test/browser.e2e.spec.ts @@ -7,9 +7,11 @@ await setup({ browser: true, }) -describe('browser', async () => { +describe('browser', () => { it('runs a test', async () => { - const page = await createPage('/') - expect(page.getByRole('heading').innerText()).toContain('Welcome to Nuxt!') + const page = await createPage('/') + const text = await page.getByRole('heading', { name: 'Welcome to Nuxt!' }).innerText() + expect(text).toContain('Welcome to Nuxt!') + await page.close() }) }) diff --git a/examples/app-vitest/test/dev.e2e.test.ts b/examples/app-vitest/test/dev.e2e.spec.ts similarity index 90% rename from examples/app-vitest/test/dev.e2e.test.ts rename to examples/app-vitest/test/dev.e2e.spec.ts index 9df78ec6d..fbc692c84 100644 --- a/examples/app-vitest/test/dev.e2e.test.ts +++ b/examples/app-vitest/test/dev.e2e.spec.ts @@ -7,7 +7,7 @@ await setup({ dev: true, }) -describe('server (dev)', async () => { +describe('server (dev)', () => { it('runs a test', async () => { const html = await $fetch('/') expect(html.slice(0, 15)).toMatchInlineSnapshot(` diff --git a/examples/app-vitest/test/generate.e2e.spec.ts b/examples/app-vitest/test/generate.e2e.spec.ts new file mode 100644 index 000000000..625cee24c --- /dev/null +++ b/examples/app-vitest/test/generate.e2e.spec.ts @@ -0,0 +1,25 @@ +import { fileURLToPath } from 'node:url' +import { resolve } from 'pathe' +import { globby } from 'globby' +import { setup, useTestContext } from '@nuxt/test-utils/e2e' +import { describe, expect, it } from 'vitest' + +await setup({ + rootDir: fileURLToPath(new URL('../', import.meta.url)), + nuxtConfig: { + nitro: { + prerender: { + routes: ['/test'] + } + } + } +}) + +describe('generate test', () => { + it('can assert files are prerendered', async () => { + const ctx = useTestContext() + const outputDir = resolve(ctx.nuxt!.options.nitro.output?.dir || '', 'public') + const files = await globby(outputDir) + expect(files).toContain(resolve(outputDir, 'test/index.html')) + }) +}) diff --git a/examples/content/package.json b/examples/content/package.json index b7c94e44f..4c2dabcef 100644 --- a/examples/content/package.json +++ b/examples/content/package.json @@ -6,7 +6,7 @@ "dev": "nuxt dev", "dev:prepare": "nuxt prepare", "generate": "nuxt generate", - "test": "vitest", + "test": "vitest run", "preview": "nuxt preview" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3f70bbd75..ccfa0c513 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -217,6 +217,9 @@ importers: '@nuxt/test-utils': specifier: workspace:* version: link:../.. + globby: + specifier: ^14.0.0 + version: 14.0.0 happy-dom: specifier: ^12.10.3 version: 12.10.3