Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo committed Jan 12, 2023
1 parent b974c0c commit 9db6c0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/runtime/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { eventHandler } from "h3";

import { defineNitroPlugin } from "./plugin";

export const globalTiming = globalThis.__timing__ || {
const globalTiming = globalThis.__timing__ || {
start: () => 0,
end: () => 0,
metrics: [],
};

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing
export const timingMiddleware = eventHandler((event) => {
const timingMiddleware = eventHandler((event) => {
const start = globalTiming.start();

const _end = event.node.res.end;
Expand All @@ -33,7 +33,7 @@ export const timingMiddleware = eventHandler((event) => {
}.bind(event.node.res);
});

export const timingPlugin = defineNitroPlugin((nitro) => {
export default defineNitroPlugin((nitro) => {
// Always add timing middleware to the beginning of handler stack
nitro.h3App.stack.unshift({
route: "/",
Expand Down
11 changes: 11 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function setupTest(preset: string) {
"/rules/nested/**": { redirect: "/base", headers: { "x-test": "test" } },
"/rules/nested/override": { redirect: { to: "/other" } },
},
timing: true,
}));

if (ctx.isDev) {
Expand Down Expand Up @@ -240,4 +241,14 @@ export function testNitro(
additionalTests(ctx, callHandler);
}
}

if (ctx.nitro!.options.timing) {
it("set server timing header", async () => {
const { data, status, headers } = await callHandler({
url: "/api/hello",
});
expect(status).toBe(200);
expect(headers["server-timing"]).toMatch(/-;dur=\d+;desc="Generate"/);
});
}
}

0 comments on commit 9db6c0d

Please sign in to comment.