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 all 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 = 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
Loading