Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Astro-specific files
.astro

# Test fixture temp directories
.tmp
57 changes: 21 additions & 36 deletions packages/core/tests/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { rm } from "node:fs/promises";
import { randomBytes } from "node:crypto";
import { cpSync, mkdirSync } from "node:fs";
import path from "node:path";
import type { AstroInlineConfig } from "astro";
import { mergeConfig } from "astro/config";
Expand All @@ -18,6 +19,9 @@ export type GetFromViteMiddlewareFunction = (
path: string,
) => Promise<Response | undefined>;

const FIXTURES_DIR = path.join(import.meta.dirname, "..", "fixtures");
const TMP_DIR = path.join(FIXTURES_DIR, ".tmp");

export function getFixture(fixtureName: string): {
fixtureRoot: string;
outDir: string;
Expand All @@ -33,38 +37,24 @@ export function getFixture(fixtureName: string): {
}>;
clean: () => Promise<void>;
} {
const fixtureRoot = path.join(
import.meta.dirname,
"..",
"fixtures",
const sourceFixtureRoot = path.join(
FIXTURES_DIR,
...dotStringToPath(fixtureName),
);

const outDir = path.join(
fixtureRoot,
`dist-${Math.random().toString(36).substring(2, 15)}`,
);
const uniqueId = randomBytes(6).toString("hex");
const fixtureRoot = path.join(TMP_DIR, uniqueId);
mkdirSync(TMP_DIR, { recursive: true });
cpSync(sourceFixtureRoot, fixtureRoot, { recursive: true });

const clean = async () => {
await rm(outDir, {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
await rm(path.join(fixtureRoot, ".astro"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
await rm(path.join(fixtureRoot, "node_modules"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
};
const outDir = path.join(fixtureRoot, "dist");

// Each getFixture() call creates a unique fixtureRoot under fixtures/.tmp/,
// so outDir, .astro, and node_modules/.vite are never shared between tests.
// The whole .tmp/ directory is wiped before each test run via globalSetup,
// so no cleanup is needed (and skipping it avoids racing with the vite
// optimizer that may still be writing to .vite after viteServer.close()).
const clean = async () => {};

const DEV_TEST_ADDRESS = "http://host-placeholder.test";

Expand Down Expand Up @@ -110,10 +100,7 @@ export function getFixture(fixtureName: string): {
toFetchResponse(res).then(resolve);
});
},
stop: async () => {
await stopDevServer();
await clean();
},
stop: stopDevServer,
};
},
build: async (params?: MightyServerOptions) => {
Expand Down Expand Up @@ -147,9 +134,7 @@ export function getFixture(fixtureName: string): {

return {
render,
stop: async () => {
await clean();
},
stop: async () => {},
};
},
clean,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/tests/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rmSync } from "node:fs";
import path from "node:path";

const TMP_DIR = path.join(import.meta.dirname, "..", "fixtures", ".tmp");

export default function setup(): void {
rmSync(TMP_DIR, { recursive: true, force: true });
}
1 change: 1 addition & 0 deletions packages/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export default defineConfig({
"@tests/": `${path.resolve(import.meta.dirname, "tests")}/`,
},
testTimeout: 30000,
globalSetup: ["./tests/globalSetup.ts"],
},
});
54 changes: 17 additions & 37 deletions packages/hono/tests/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { rm } from "node:fs/promises";
import { randomBytes } from "node:crypto";
import { cpSync, mkdirSync } from "node:fs";
import path from "node:path";
import { build } from "@gomighty/core/build";
import type { MightyServerOptions } from "@gomighty/core/types";
Expand All @@ -15,6 +16,9 @@ type AppEnv = {
};
};

const FIXTURES_DIR = path.join(import.meta.dirname, "..", "fixtures");
const TMP_DIR = path.join(FIXTURES_DIR, ".tmp");

export function getFixture(fixtureName: string): {
fixtureRoot: string;
outDir: string;
Expand All @@ -30,44 +34,20 @@ export function getFixture(fixtureName: string): {
};
clean: () => Promise<void>;
} {
const fixtureRoot = path.join(
import.meta.dirname,
"..",
"fixtures",
fixtureName,
);
const sourceFixtureRoot = path.join(FIXTURES_DIR, fixtureName);

const outDir = path.join(
fixtureRoot,
`dist-${Math.random().toString(36).substring(2, 15)}`,
);
const uniqueId = randomBytes(6).toString("hex");
const fixtureRoot = path.join(TMP_DIR, uniqueId);
mkdirSync(TMP_DIR, { recursive: true });
cpSync(sourceFixtureRoot, fixtureRoot, { recursive: true });

const clean = async (): Promise<void> => {
await rm(outDir, {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
await rm(path.join(fixtureRoot, "dist"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
await rm(path.join(fixtureRoot, ".astro"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
await rm(path.join(fixtureRoot, "node_modules"), {
recursive: true,
force: true,
maxRetries: 5,
retryDelay: 100,
});
};
const outDir = path.join(fixtureRoot, "dist");

// Each getFixture() call creates a unique fixtureRoot under fixtures/.tmp/,
// so outDir, .astro, dist, and node_modules/.vite are never shared between
// tests. The whole .tmp/ directory is wiped before each test run via
// globalSetup, so no cleanup is needed.
const clean = async (): Promise<void> => {};

return {
fixtureRoot,
Expand Down
8 changes: 8 additions & 0 deletions packages/hono/tests/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rmSync } from "node:fs";
import path from "node:path";

const TMP_DIR = path.join(import.meta.dirname, "..", "fixtures", ".tmp");

export default function setup(): void {
rmSync(TMP_DIR, { recursive: true, force: true });
}
1 change: 1 addition & 0 deletions packages/hono/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default defineConfig({
"@/": `${path.resolve(import.meta.dirname, "src")}/`,
},
testTimeout: 30000,
globalSetup: ["./tests/globalSetup.ts"],
},
});
1 change: 0 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export default defineConfig({
test: {
projects: ["packages/*"],
testTimeout: 30000,
fileParallelism: false,
},
});
Loading