Skip to content

Commit

Permalink
fix: teardown build directories after tests (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 2, 2023
1 parent 7490334 commit 8a82ce6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
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 = 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: {
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.teardown = ctx.teardown || []
ctx.teardown.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.teardown || []).map(fn => fn()))
}

const setup = async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export interface TestContext {
url?: string
serverProcess?: ExecaChildProcess
mockFn?: Function
/**
* Functions to run on the vitest `afterAll` hook.
* Useful for removing anything created during the test.
*/
teardown?: (() => void)[]
}

export interface TestHooks {
Expand Down

0 comments on commit 8a82ce6

Please sign in to comment.