Skip to content

Commit

Permalink
chore: fix tests (#1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Mar 3, 2023
1 parent 9e38bd2 commit 23da516
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 39 deletions.
11 changes: 0 additions & 11 deletions test/fixture/api/storage/[...base].get.ts

This file was deleted.

9 changes: 0 additions & 9 deletions test/fixture/api/storage/[...base].put.ts

This file was deleted.

11 changes: 11 additions & 0 deletions test/fixture/api/storage/item.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default eventHandler(async (event) => {
const { base = "", key = "" } = getQuery(event) as Record<string, string>;
const storage = useStorage(`test:${base}`);

if (!key || key.endsWith(":")) {
return await storage.getKeys();
}

const value = await storage.getItem(key);
return value;
});
7 changes: 7 additions & 0 deletions test/fixture/api/storage/item.put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default eventHandler(async (event) => {
const { base = "", key = "" } = getQuery(event) as Record<string, string>;
const storage = useStorage(`test:${base}`);
const value = await readBody(event);
await storage.setItem(key, value);
return value;
});
4 changes: 2 additions & 2 deletions test/presets/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe("nitro:preset:node", async () => {
testNitro(ctx, async () => {
const { listener } = await import(resolve(ctx.outDir, "server/index.mjs"));
await startServer(ctx, listener);
return async ({ url }) => {
const res = await ctx.fetch(url);
return async ({ url, ...opts }) => {
const res = await ctx.fetch(url, opts);
return res;
};
});
Expand Down
4 changes: 2 additions & 2 deletions test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe("nitro:preset:vercel", async () => {
resolve(ctx.outDir, "functions/__nitro.func/index.mjs")
).then((r) => r.default || r);
await startServer(ctx, handle);
return async ({ url }) => {
const res = await ctx.fetch(url);
return async ({ url, ...options }) => {
const res = await ctx.fetch(url, options);
return res;
};
},
Expand Down
52 changes: 37 additions & 15 deletions test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { resolve } from "pathe";
import { listen, Listener } from "listhen";
import destr from "destr";
import { fetch } from "ofetch";
import { fetch, FetchOptions } from "ofetch";
import { expect, it, afterAll } from "vitest";
import { isWindows } from "std-env";
import { fileURLToPath } from "mlly";
import { joinURL } from "ufo";
import * as _nitro from "../src";
Expand All @@ -17,7 +16,7 @@ export interface Context {
nitro?: Nitro;
rootDir: string;
outDir: string;
fetch: (url: string) => Promise<any>;
fetch: (url: string, opts?: FetchOptions) => Promise<any>;
server?: Listener;
isDev: boolean;
}
Expand All @@ -30,8 +29,11 @@ export async function setupTest(preset: string) {
isDev: preset === "nitro-dev",
rootDir: fixtureDir,
outDir: resolve(fixtureDir, ".output", preset),
fetch: (url) =>
fetch(joinURL(ctx.server!.url, url.slice(1)), { redirect: "manual" }),
fetch: (url, opts) =>
fetch(joinURL(ctx.server!.url, url.slice(1)), {
redirect: "manual",
...(opts as any),
}),
};

const nitro = (ctx.nitro = await createNitro({
Expand Down Expand Up @@ -273,17 +275,37 @@ export function testNitro(
});
});

it("supports useStorage(base)", async () => {
const { data } = await callHandler({ url: "/api/storage/test" });
expect(data).toMatchObject([]);
const { data: res } = await callHandler({
url: '/api/storage/test',
method: 'PUT',
body: { key: 'hello', value: 'world' }
it("useStorage (with base)", async () => {
const putRes = await callHandler({
url: "/api/storage/item?key=test:hello",
method: "PUT",
body: "world",
});
expect(res.key).toBe('hello');
const { data: keys } = await callHandler({ url: "/api/storage/test" });
expect(data).toMatchObject(['hello']);
expect(putRes.data).toMatchObject("world");

expect(
(
await callHandler({
url: "/api/storage/item?key=:",
})
).data
).toMatchObject(["test:hello"]);

expect(
(
await callHandler({
url: "/api/storage/item?base=test&key=:",
})
).data
).toMatchObject(["hello"]);

expect(
(
await callHandler({
url: "/api/storage/item?base=test&key=hello",
})
).data
).toBe("world");
});

if (additionalTests) {
Expand Down

0 comments on commit 23da516

Please sign in to comment.