Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: teardown test build directories #597

Merged
merged 6 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'test', randomId)
ctx.options.nuxtConfig = defu(ctx.options.nuxtConfig, {
buildDir,
nitro: {
Expand All @@ -61,7 +61,13 @@ export async function loadFixture () {
configFile: ctx.options.configFile
})

await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true })
const buildDir = ctx.nuxt.options.buildDir
// 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 }))
}
}

export async function buildFixture () {
Expand Down
2 changes: 2 additions & 0 deletions src/core/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export function createTest (options: Partial<TestOptions>): TestHooks {
if (ctx.browser) {
await ctx.browser.close()
}
// clear side effects
await Promise.all((ctx.sideEffects || []).map(fn => fn()))
}

const setup = async () => {
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface TestContext {
url?: string
serverProcess?: ExecaChildProcess
mockFn?: Function
sideEffects?: (() => void)[]
}

export interface TestHooks {
Expand Down
Loading