diff --git a/test/presets/nitro-dev.test.ts b/test/presets/nitro-dev.test.ts new file mode 100644 index 0000000000..5547efeebc --- /dev/null +++ b/test/presets/nitro-dev.test.ts @@ -0,0 +1,21 @@ +import { describe, it, expect } from "vitest"; +import { setupTest, testNitro } from "../tests"; + +describe("nitro:preset:nitro-dev", async () => { + const ctx = await setupTest("nitro-dev"); + testNitro( + ctx, + () => { + return async ({ url }) => { + const res = await ctx.fetch(url); + return res; + }; + }, + (_ctx, callHandler) => { + it("returns correct status for devProxy", async () => { + const { status } = await callHandler({ url: "/proxy/example" }); + expect(status).toBe(200); + }); + } + ); +}); diff --git a/test/tests.ts b/test/tests.ts index c719f7ea14..6cbc11cc63 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -11,20 +11,22 @@ import type { Nitro } from "../src"; const { createNitro, build, prepare, copyPublicAssets, prerender } = (_nitro as any as { default: typeof _nitro }).default || _nitro; -interface Context { +export interface Context { preset: string; nitro?: Nitro; rootDir: string; outDir: string; fetch: (url: string) => Promise; server?: Listener; + isDev: boolean; } -export async function setupTest(preset) { +export async function setupTest(preset: string) { const fixtureDir = fileURLToPath(new URL("fixture", import.meta.url).href); const ctx: Context = { preset, + isDev: preset === "nitro-dev", rootDir: fixtureDir, outDir: resolve(fixtureDir, ".output", preset), fetch: (url) => @@ -33,8 +35,10 @@ export async function setupTest(preset) { const nitro = (ctx.nitro = await createNitro({ preset: ctx.preset, + dev: ctx.isDev, rootDir: ctx.rootDir, - serveStatic: preset !== "cloudflare" && preset !== "vercel-edge", + serveStatic: + preset !== "cloudflare" && preset !== "vercel-edge" && !ctx.isDev, output: { dir: ctx.outDir }, routeRules: { "/rules/headers": { headers: { "cache-control": "s-maxage=60" } }, @@ -53,10 +57,24 @@ export async function setupTest(preset) { "/rules/nested/override": { redirect: { to: "/other" } }, }, })); - await prepare(nitro); - await copyPublicAssets(nitro); - await prerender(nitro); - await build(nitro); + + if (ctx.isDev) { + // Setup development server + const devServer = _nitro.createDevServer(ctx.nitro); + ctx.server = await devServer.listen({}); + await prepare(ctx.nitro); + const ready = new Promise((resolve) => { + ctx.nitro.hooks.hook("dev:reload", () => resolve()); + }); + await build(ctx.nitro); + await ready; + } else { + // Production build + await prepare(nitro); + await copyPublicAssets(nitro); + await prerender(nitro); + await build(nitro); + } afterAll(async () => { if (ctx.server) { @@ -70,7 +88,7 @@ export async function setupTest(preset) { return ctx; } -export async function startServer(ctx, handle) { +export async function startServer(ctx: Context, handle) { ctx.server = await listen(handle); console.log(">", ctx.server!.url); } @@ -84,7 +102,8 @@ type TestHandler = (options: any) => Promise; export function testNitro( ctx: Context, - getHandler: () => TestHandler | Promise + getHandler: () => TestHandler | Promise, + additionalTests?: (ctx: Context, callHandler: TestHandler) => void ) { let _handler: TestHandler; @@ -215,5 +234,9 @@ export function testNitro( subpathLib: "2.0.1", }); }); + + if (additionalTests) { + additionalTests(ctx, callHandler); + } } }