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

test: add tests for dev server and proxy #644

Merged
merged 11 commits into from
Dec 8, 2022
21 changes: 21 additions & 0 deletions test/presets/nitro-dev.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
);
});
41 changes: 32 additions & 9 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
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) =>
Expand All @@ -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" } },
Expand All @@ -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<void>((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) {
Expand All @@ -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);
}
Expand All @@ -84,7 +102,8 @@ type TestHandler = (options: any) => Promise<TestHandlerResult | Response>;

export function testNitro(
ctx: Context,
getHandler: () => TestHandler | Promise<TestHandler>
getHandler: () => TestHandler | Promise<TestHandler>,
additionalTests?: (ctx: Context, callHandler: TestHandler) => void
) {
let _handler: TestHandler;

Expand Down Expand Up @@ -215,5 +234,9 @@ export function testNitro(
subpathLib: "2.0.1",
});
});

if (additionalTests) {
additionalTests(ctx, callHandler);
}
}
}