From d7c15c1a5dc750c00f953df5174d021930991d7b Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sat, 2 Dec 2023 14:57:53 +1100 Subject: [PATCH 1/5] fix: use `buildDir` option --- src/core/nuxt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/nuxt.ts b/src/core/nuxt.ts index c5f557d5a..b6ef9c637 100644 --- a/src/core/nuxt.ts +++ b/src/core/nuxt.ts @@ -43,7 +43,7 @@ export async function loadFixture () { if (!ctx.options.dev) { const randomId = Math.random().toString(36).slice(2, 8) - const buildDir = resolve(ctx.options.rootDir, '.nuxt', randomId) + const buildDir = ctx.options.buildDir || resolve(ctx.options.rootDir, '.nuxt', randomId) ctx.options.nuxtConfig = defu(ctx.options.nuxtConfig, { buildDir, nitro: { From d7d9e4b6020fd4f40801665fd85dfaf744ab88bc Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sat, 2 Dec 2023 15:01:53 +1100 Subject: [PATCH 2/5] fix: build dirs never cleaned up --- src/core/nuxt.ts | 10 ++++++++-- src/core/setup/index.ts | 2 ++ src/core/types.ts | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core/nuxt.ts b/src/core/nuxt.ts index b6ef9c637..500f81b73 100644 --- a/src/core/nuxt.ts +++ b/src/core/nuxt.ts @@ -43,7 +43,7 @@ export async function loadFixture () { if (!ctx.options.dev) { const randomId = Math.random().toString(36).slice(2, 8) - const buildDir = ctx.options.buildDir || resolve(ctx.options.rootDir, '.nuxt', randomId) + const buildDir = ctx.options.buildDir || resolve(ctx.options.rootDir, '.nuxt', 'test', randomId) ctx.options.nuxtConfig = defu(ctx.options.nuxtConfig, { buildDir, nitro: { @@ -61,7 +61,13 @@ export async function loadFixture () { configFile: ctx.options.configFile }) - await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true }) + // avoid deleting build dirs we didn't create - avoids misconfiguration deletes + if (!existsSync(ctx.nuxt.options.buildDir)) { + await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true }) + // clean up after ourselves, + ctx.sideEffects = ctx.sideEffects || [] + ctx.sideEffects.push(() => fsp.rm(ctx.nuxt.options.buildDir, { recursive: true, force: true })) + } } export async function buildFixture () { diff --git a/src/core/setup/index.ts b/src/core/setup/index.ts index 6fc205cc6..41a1ddab5 100644 --- a/src/core/setup/index.ts +++ b/src/core/setup/index.ts @@ -34,6 +34,8 @@ export function createTest (options: Partial): TestHooks { if (ctx.browser) { await ctx.browser.close() } + // clear side effects + await Promise.all((ctx.sideEffects || []).map(fn => fn())) } const setup = async () => { diff --git a/src/core/types.ts b/src/core/types.ts index e8847091b..0c2412b97 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -33,6 +33,7 @@ export interface TestContext { url?: string serverProcess?: ExecaChildProcess mockFn?: Function + sideEffects?: (() => void)[] } export interface TestHooks { From 82f617fec933451e1d531f5dec9b0f68ec96eac2 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sat, 2 Dec 2023 15:09:39 +1100 Subject: [PATCH 3/5] chore: doc --- src/core/nuxt.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/nuxt.ts b/src/core/nuxt.ts index 500f81b73..1631a33b6 100644 --- a/src/core/nuxt.ts +++ b/src/core/nuxt.ts @@ -61,10 +61,9 @@ export async function loadFixture () { configFile: ctx.options.configFile }) - // avoid deleting build dirs we didn't create - avoids misconfiguration deletes + // avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes if (!existsSync(ctx.nuxt.options.buildDir)) { await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true }) - // clean up after ourselves, ctx.sideEffects = ctx.sideEffects || [] ctx.sideEffects.push(() => fsp.rm(ctx.nuxt.options.buildDir, { recursive: true, force: true })) } From f487040d6b1790e0b1290f69296c12891af6a983 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sat, 2 Dec 2023 15:11:35 +1100 Subject: [PATCH 4/5] chore: fix type issue --- src/core/nuxt.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/nuxt.ts b/src/core/nuxt.ts index 1631a33b6..840de6ee0 100644 --- a/src/core/nuxt.ts +++ b/src/core/nuxt.ts @@ -61,11 +61,12 @@ export async function loadFixture () { configFile: ctx.options.configFile }) + const buildDir = ctx.nuxt.options.buildDir // avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes - if (!existsSync(ctx.nuxt.options.buildDir)) { - await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true }) + if (!existsSync(buildDir)) { + await fsp.mkdir(buildDir, { recursive: true }) ctx.sideEffects = ctx.sideEffects || [] - ctx.sideEffects.push(() => fsp.rm(ctx.nuxt.options.buildDir, { recursive: true, force: true })) + ctx.sideEffects.push(() => fsp.rm(buildDir, { recursive: true, force: true })) } } From 37315314b413f11f8b7f8affc8300a07ee566408 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sat, 2 Dec 2023 18:34:26 +1100 Subject: [PATCH 5/5] refactor: prefer `teardown` --- src/core/nuxt.ts | 4 ++-- src/core/setup/index.ts | 2 +- src/core/types.ts | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/nuxt.ts b/src/core/nuxt.ts index 840de6ee0..ba8a063a4 100644 --- a/src/core/nuxt.ts +++ b/src/core/nuxt.ts @@ -65,8 +65,8 @@ export async function loadFixture () { // avoid creating / deleting build dirs that already exist - avoids misconfiguration deletes if (!existsSync(buildDir)) { await fsp.mkdir(buildDir, { recursive: true }) - ctx.sideEffects = ctx.sideEffects || [] - ctx.sideEffects.push(() => fsp.rm(buildDir, { recursive: true, force: true })) + ctx.teardown = ctx.teardown || [] + ctx.teardown.push(() => fsp.rm(buildDir, { recursive: true, force: true })) } } diff --git a/src/core/setup/index.ts b/src/core/setup/index.ts index 41a1ddab5..f03a94868 100644 --- a/src/core/setup/index.ts +++ b/src/core/setup/index.ts @@ -35,7 +35,7 @@ export function createTest (options: Partial): TestHooks { await ctx.browser.close() } // clear side effects - await Promise.all((ctx.sideEffects || []).map(fn => fn())) + await Promise.all((ctx.teardown || []).map(fn => fn())) } const setup = async () => { diff --git a/src/core/types.ts b/src/core/types.ts index 0c2412b97..5e7f22e7f 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -33,7 +33,11 @@ export interface TestContext { url?: string serverProcess?: ExecaChildProcess mockFn?: Function - sideEffects?: (() => void)[] + /** + * Functions to run on the vitest `afterAll` hook. + * Useful for removing anything created during the test. + */ + teardown?: (() => void)[] } export interface TestHooks {