From 80a11210127f1f993a7d9d0204d4b6a3b6bba98d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 25 Apr 2022 15:20:15 +0800 Subject: [PATCH 01/11] feat: nuxi test supports setting up apps globally --- examples/advanced/testing/package.json | 4 ++- examples/advanced/testing/tests/basic.test.ts | 10 ++----- packages/nuxi/src/commands/test.ts | 8 +++--- packages/test-utils/build.config.ts | 3 ++- packages/test-utils/package.json | 3 ++- packages/test-utils/src/context.ts | 13 ++++++++++ packages/test-utils/src/dirs.ts | 5 ++++ packages/test-utils/src/run.ts | 26 +++++++++++++++++-- .../test-utils/src/runtime/global-setup.ts | 12 +++++++++ packages/test-utils/src/setup/index.ts | 2 +- yarn.lock | 10 +++++-- 11 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 packages/test-utils/src/dirs.ts create mode 100644 packages/test-utils/src/runtime/global-setup.ts diff --git a/examples/advanced/testing/package.json b/examples/advanced/testing/package.json index f61395d1bbf..c9f57d36f29 100644 --- a/examples/advanced/testing/package.json +++ b/examples/advanced/testing/package.json @@ -4,9 +4,11 @@ "scripts": { "build": "nuxi build", "dev": "nuxi dev", - "start": "nuxi preview" + "start": "nuxi preview", + "test": "nuxi test" }, "devDependencies": { + "@nuxt/test-utils": "npm:@nuxt/test-utils-edge@latest", "nuxt": "npm:nuxt3@latest" } } diff --git a/examples/advanced/testing/tests/basic.test.ts b/examples/advanced/testing/tests/basic.test.ts index 9768bca1bd8..696e3ec9404 100644 --- a/examples/advanced/testing/tests/basic.test.ts +++ b/examples/advanced/testing/tests/basic.test.ts @@ -1,13 +1,7 @@ -import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' -import { setup, $fetch, isDev } from '@nuxt/test-utils' - -describe('example', async () => { - await setup({ - rootDir: fileURLToPath(new URL('..', import.meta.url)), - server: true - }) +import { $fetch, isDev } from '@nuxt/test-utils' +describe('example', () => { it('Renders Hello Nuxt', async () => { expect(await $fetch('/')).toMatch('Hello Nuxt!') }) diff --git a/packages/nuxi/src/commands/test.ts b/packages/nuxi/src/commands/test.ts index fd5196a8f62..da80ac1405f 100644 --- a/packages/nuxi/src/commands/test.ts +++ b/packages/nuxi/src/commands/test.ts @@ -16,10 +16,6 @@ export default defineNuxtCommand({ dev: !!args.dev, watch: !!args.watch }) - - if (args.watch) { - return 'wait' - } } }) @@ -33,7 +29,9 @@ async function importTestUtils (): Promise { throw new Error('Invalid version of `@nuxt/test-utils` is installed!') } return exports - } catch (_err) { err = _err } + } catch (_err) { + err = _err + } } console.error(err) throw new Error('`@nuxt/test-utils-edge` seems missing. Run `npm i -D @nuxt/test-utils-edge` or `yarn add -D @nuxt/test-utils-edge` to install.') diff --git a/packages/test-utils/build.config.ts b/packages/test-utils/build.config.ts index 038925e9197..c61d0d97a2d 100644 --- a/packages/test-utils/build.config.ts +++ b/packages/test-utils/build.config.ts @@ -3,7 +3,8 @@ import { defineBuildConfig } from 'unbuild' export default defineBuildConfig({ declaration: true, entries: [ - 'src/index' + 'src/index', + { input: 'src/runtime/', outDir: 'dist/runtime', format: 'esm' } ], dependencies: [ ], diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index c978ae38633..d5be0831332 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -19,7 +19,8 @@ "execa": "^6.1.0", "get-port-please": "^2.5.0", "jiti": "^1.13.0", - "ohmyfetch": "^0.4.15" + "ohmyfetch": "^0.4.15", + "pathe": "^0.2.0" }, "devDependencies": { "playwright": "^1.21.1", diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index 85bff696d2f..62f6abc3903 100644 --- a/packages/test-utils/src/context.ts +++ b/packages/test-utils/src/context.ts @@ -26,6 +26,7 @@ export function createTestContext (options: Partial): TestContext { } export function useTestContext (): TestContext { + recoverContextFromEnv() if (!currentContext) { throw new Error('No context is available. (Forgot calling setup or createContext?)') } @@ -41,3 +42,15 @@ export function isDev () { const ctx = useTestContext() return ctx.options.dev } + +export function recoverContextFromEnv () { + if (process.env.NUXT_TEST_CONTEXT && !currentContext) { + setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT)) + } +} + +export function exposeContextToEnv () { + const ctx = { ...currentContext } + delete ctx.nuxt + process.env.NUXT_TEST_CONTEXT = JSON.stringify(ctx) +} diff --git a/packages/test-utils/src/dirs.ts b/packages/test-utils/src/dirs.ts new file mode 100644 index 00000000000..6eacc10603d --- /dev/null +++ b/packages/test-utils/src/dirs.ts @@ -0,0 +1,5 @@ +import { fileURLToPath } from 'node:url' +import { dirname, resolve } from 'pathe' + +export const distDir = dirname(fileURLToPath(import.meta.url)) +export const pkgDir = resolve(distDir, '..') diff --git a/packages/test-utils/src/run.ts b/packages/test-utils/src/run.ts index 6e0b2735b6c..eead252ac83 100644 --- a/packages/test-utils/src/run.ts +++ b/packages/test-utils/src/run.ts @@ -1,12 +1,17 @@ +import { resolve } from 'pathe' +import { distDir } from './dirs' + export interface RunTestOptions { rootDir: string, dev?: boolean, watch?: boolean runner?: 'vitest' + globalSetup?: boolean } const RunTestDefaults: Partial = { - runner: 'vitest' + runner: 'vitest', + globalSetup: true } export async function runTests (opts: RunTestOptions) { @@ -21,18 +26,35 @@ export async function runTests (opts: RunTestOptions) { process.env.NUXT_TEST_DEV = 'true' } + process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts) + const { startVitest } = await import('vitest/dist/node.js') const succeeded = await startVitest( [] /* argv */, // Vitest options { - root: opts.rootDir, run: !opts.watch }, // Vite options { esbuild: { tsconfigRaw: '{}' + }, + test: { + dir: opts.rootDir, + deps: { + inline: [ + distDir, + '@nuxt/test-utils', + '@nuxt/test-utils-edge' + ] + }, + globals: true, + globalSetup: [ + opts.globalSetup + ? resolve(distDir, './runtime/global-setup.mjs') + : undefined + ] } } ) diff --git a/packages/test-utils/src/runtime/global-setup.ts b/packages/test-utils/src/runtime/global-setup.ts new file mode 100644 index 00000000000..43db31325d6 --- /dev/null +++ b/packages/test-utils/src/runtime/global-setup.ts @@ -0,0 +1,12 @@ +import { createTest, exposeContextToEnv } from '@nuxt/test-utils' + +const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS)) + +export const setup = async () => { + await hooks.setup() + exposeContextToEnv() +} + +export const teardown = async () => { + await hooks.afterAll() +} diff --git a/packages/test-utils/src/setup/index.ts b/packages/test-utils/src/setup/index.ts index 5b33673c87f..aa0b368cbb8 100644 --- a/packages/test-utils/src/setup/index.ts +++ b/packages/test-utils/src/setup/index.ts @@ -67,7 +67,7 @@ export function createTest (options: Partial): TestHooks { } } -export async function setup (options: Partial) { +export async function setup (options: Partial = {}) { const hooks = createTest(options) const setupFn = setupMaps[hooks.ctx.options.runner] diff --git a/yarn.lock b/yarn.lock index 66d15f0c50d..07667b6f772 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1548,6 +1548,12 @@ __metadata: languageName: node linkType: hard +"@nuxt/test-utils@link:./packages/test-utils::locator=nuxt-framework%40workspace%3A.": + version: 0.0.0-use.local + resolution: "@nuxt/test-utils@link:./packages/test-utils::locator=nuxt-framework%40workspace%3A." + languageName: node + linkType: soft + "@nuxt/test-utils@workspace:packages/test-utils": version: 0.0.0-use.local resolution: "@nuxt/test-utils@workspace:packages/test-utils" @@ -1559,6 +1565,7 @@ __metadata: get-port-please: ^2.5.0 jiti: ^1.13.0 ohmyfetch: ^0.4.15 + pathe: ^0.2.0 playwright: ^1.21.1 unbuild: latest vitest: ^0.9.4 @@ -6568,6 +6575,7 @@ __metadata: version: 0.0.0-use.local resolution: "example-testing@workspace:examples/advanced/testing" dependencies: + "@nuxt/test-utils": "npm:@nuxt/test-utils-edge@latest" nuxt: "npm:nuxt3@latest" languageName: unknown linkType: soft @@ -10227,8 +10235,6 @@ __metadata: "nuxt-playground@workspace:playground": version: 0.0.0-use.local resolution: "nuxt-playground@workspace:playground" - dependencies: - nuxt: "npm:nuxt3@latest" languageName: unknown linkType: soft From 2fd0dccf2994a8246f09a1d74cca2a3ee0b3ad69 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 25 Apr 2022 15:23:02 +0800 Subject: [PATCH 02/11] chore: expose types --- packages/test-utils/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 55d9ebae535..c1c81a66432 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -4,3 +4,4 @@ export * from './nuxt' export * from './server' export * from './setup' export * from './run' +export * from './types' From fac79fc200f9075cc134191c01c4376cbbc39472 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 25 Apr 2022 16:46:12 +0800 Subject: [PATCH 03/11] chore: lock --- yarn.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/yarn.lock b/yarn.lock index 07667b6f772..ff32bf6db1a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10235,6 +10235,8 @@ __metadata: "nuxt-playground@workspace:playground": version: 0.0.0-use.local resolution: "nuxt-playground@workspace:playground" + dependencies: + nuxt: "npm:nuxt3@latest" languageName: unknown linkType: soft From 630218531c1297c344253daa34b161afe4575464 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 27 Apr 2022 19:02:41 +0800 Subject: [PATCH 04/11] chore: update --- packages/nuxi/src/commands/test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/nuxi/src/commands/test.ts b/packages/nuxi/src/commands/test.ts index da80ac1405f..1761dc634f2 100644 --- a/packages/nuxi/src/commands/test.ts +++ b/packages/nuxi/src/commands/test.ts @@ -16,6 +16,10 @@ export default defineNuxtCommand({ dev: !!args.dev, watch: !!args.watch }) + + if (args.watch) { + return 'wait' as const + } } }) From fb2047c933e21d35e19d1cd391d0d78180fe22de Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 13 Jun 2022 11:22:13 +0800 Subject: [PATCH 05/11] fix: only keep neccessary keys --- packages/test-utils/src/context.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index 62f6abc3903..6f82a144f7c 100644 --- a/packages/test-utils/src/context.ts +++ b/packages/test-utils/src/context.ts @@ -50,7 +50,14 @@ export function recoverContextFromEnv () { } export function exposeContextToEnv () { - const ctx = { ...currentContext } - delete ctx.nuxt - process.env.NUXT_TEST_CONTEXT = JSON.stringify(ctx) + const { + options, + browser, + url + } = currentContext + process.env.NUXT_TEST_CONTEXT = JSON.stringify({ + options, + browser, + url + }) } From 86744171490afd3a20ab630bed00b726c46d527e Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 12 Jul 2022 13:36:36 +0800 Subject: [PATCH 06/11] chore: update lock --- yarn.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/yarn.lock b/yarn.lock index d5ed35c04cb..54d8d1e68ff 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1,3 +1,6 @@ +# This file is generated by running "yarn install" inside your project. +# Manual changes might be lost - proceed with caution! + __metadata: version: 6 cacheKey: 8 @@ -1724,6 +1727,7 @@ __metadata: get-port-please: ^2.5.0 jiti: ^1.14.0 ohmyfetch: ^0.4.18 + pathe: ^0.3.2 playwright: ^1.23.2 unbuild: latest vitest: ^0.18.0 From e51c6e2ebbdaecbce5d71c5bf23dceb8836daeff Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 22 Sep 2022 12:20:03 +0800 Subject: [PATCH 07/11] chore: update --- packages/test-utils/src/context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index fbdf0011733..117cd058bcf 100644 --- a/packages/test-utils/src/context.ts +++ b/packages/test-utils/src/context.ts @@ -58,7 +58,7 @@ export function exposeContextToEnv () { options, browser, url - } = currentContext + } = currentContext! process.env.NUXT_TEST_CONTEXT = JSON.stringify({ options, browser, From 67d6c2acdd0f0aa3273e8c6d6283bbeaccf6ce52 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 22 Sep 2022 12:46:06 +0800 Subject: [PATCH 08/11] chore: update --- packages/test-utils/src/runtime/global-setup.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-utils/src/runtime/global-setup.ts b/packages/test-utils/src/runtime/global-setup.ts index 43db31325d6..5b904ef069c 100644 --- a/packages/test-utils/src/runtime/global-setup.ts +++ b/packages/test-utils/src/runtime/global-setup.ts @@ -1,6 +1,6 @@ import { createTest, exposeContextToEnv } from '@nuxt/test-utils' -const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS)) +const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS!)) export const setup = async () => { await hooks.setup() From 7ec5bc9d6c10c015ade17280ec847ebe15a1f0d9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 10 Nov 2022 15:38:31 +0100 Subject: [PATCH 09/11] update lock --- pnpm-lock.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5d993709b88..9e74ab5a57d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -557,6 +557,7 @@ importers: get-port-please: ^2.6.1 jiti: ^1.16.0 ohmyfetch: ^0.4.21 + pathe: ^0.3.8 playwright: ^1.27.1 unbuild: ^0.9.4 vitest: ^0.25.1 @@ -569,6 +570,7 @@ importers: get-port-please: 2.6.1 jiti: 1.16.0 ohmyfetch: 0.4.21 + pathe: 0.3.9 devDependencies: playwright: 1.27.1 unbuild: 0.9.4 From da0b0aa6c4f3d7d55db58bfd57159871332c17ac Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 10 Nov 2022 15:51:03 +0100 Subject: [PATCH 10/11] small updates --- packages/test-utils/src/context.ts | 14 +++----------- packages/test-utils/src/run.ts | 5 ++--- packages/test-utils/src/runtime/global-setup.ts | 2 +- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index 1cbeef23f27..cb4ff2b9689 100644 --- a/packages/test-utils/src/context.ts +++ b/packages/test-utils/src/context.ts @@ -48,20 +48,12 @@ export function isDev () { } export function recoverContextFromEnv () { - if (process.env.NUXT_TEST_CONTEXT && !currentContext) { + if (!currentContext && process.env.NUXT_TEST_CONTEXT) { setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT)) } } export function exposeContextToEnv () { - const { - options, - browser, - url - } = currentContext! - process.env.NUXT_TEST_CONTEXT = JSON.stringify({ - options, - browser, - url - }) + const { options, browser, url } = currentContext! + process.env.NUXT_TEST_CONTEXT = JSON.stringify({ options, browser, url }) } diff --git a/packages/test-utils/src/run.ts b/packages/test-utils/src/run.ts index 90df06f159d..6229ef0f3b4 100644 --- a/packages/test-utils/src/run.ts +++ b/packages/test-utils/src/run.ts @@ -26,6 +26,7 @@ export async function runTests (opts: RunTestOptions) { process.env.NUXT_TEST_DEV = 'true' } + // Consumed by recoverContextFromEnv() process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts) const { startVitest } = await import('vitest/node') @@ -56,9 +57,7 @@ export async function runTests (opts: RunTestOptions) { }, globals: true, globalSetup: [ - opts.globalSetup - ? resolve(distDir, './runtime/global-setup.mjs') - : undefined + ...opts.globalSetup ? [resolve(distDir, './runtime/global-setup')] : [] ] } } diff --git a/packages/test-utils/src/runtime/global-setup.ts b/packages/test-utils/src/runtime/global-setup.ts index 5b904ef069c..5dcfed31592 100644 --- a/packages/test-utils/src/runtime/global-setup.ts +++ b/packages/test-utils/src/runtime/global-setup.ts @@ -1,6 +1,6 @@ import { createTest, exposeContextToEnv } from '@nuxt/test-utils' -const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS!)) +const hooks = createTest(JSON.parse(process.env.NUXT_TEST_OPTIONS || '{}')) export const setup = async () => { await hooks.setup() From 71957ac6b7c9eac0ecfd6d52c2254cdbfadd3c61 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 10 Nov 2022 15:51:45 +0100 Subject: [PATCH 11/11] add json parse format --- packages/test-utils/src/context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index cb4ff2b9689..849ff4d1591 100644 --- a/packages/test-utils/src/context.ts +++ b/packages/test-utils/src/context.ts @@ -49,7 +49,7 @@ export function isDev () { export function recoverContextFromEnv () { if (!currentContext && process.env.NUXT_TEST_CONTEXT) { - setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT)) + setTestContext(JSON.parse(process.env.NUXT_TEST_CONTEXT || '{}')) } }