Skip to content

Commit

Permalink
test: lazy init driver for conditional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 12, 2024
1 parent 5ab4afd commit 33d90a9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 62 deletions.
15 changes: 8 additions & 7 deletions test/drivers/cloudflare-kv-http.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe } from "vitest";
import driver from "../../src/drivers/cloudflare-kv-http";
import cfKvHttpDriver from "../../src/drivers/cloudflare-kv-http";
import { testDriver } from "./utils";

const accountId = process.env.VITE_CLOUDFLARE_ACC_ID;
Expand All @@ -10,12 +10,13 @@ describe.skipIf(!accountId || !namespaceId || !apiToken)(
"drivers: cloudflare-kv-http",
() => {
testDriver({
driver: driver({
accountId: accountId!,
namespaceId: namespaceId!,
apiToken: apiToken!,
base: Math.round(Math.random() * 1_000_000).toString(16),
}),
driver: () =>
cfKvHttpDriver({
accountId: accountId!,
namespaceId: namespaceId!,
apiToken: apiToken!,
base: Math.round(Math.random() * 1_000_000).toString(16),
}),
});
}
);
16 changes: 9 additions & 7 deletions test/drivers/db0.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, describe, expect } from "vitest";
import { afterAll, describe, expect, it } from "vitest";
import { createDatabase } from "db0";
import db0Driver from "../../src/drivers/db0";
import { testDriver } from "./utils";
Expand Down Expand Up @@ -42,13 +42,15 @@ for (const driver of drivers) {
});

testDriver({
driver: db0Driver({ database: db }),
additionalTests: async (ctx) => {
await ctx.storage.setItem("meta:test", "test_data");
driver: () => db0Driver({ database: db }),
additionalTests: (ctx) => {
it("meta", async () => {
await ctx.storage.setItem("meta:test", "test_data");

expect(await ctx.storage.getMeta("meta:test")).toMatchObject({
birthtime: expect.any(Date),
mtime: expect.any(Date),
expect(await ctx.storage.getMeta("meta:test")).toMatchObject({
birthtime: expect.any(Date),
mtime: expect.any(Date),
});
});
},
});
Expand Down
14 changes: 7 additions & 7 deletions test/drivers/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ describe("drivers: http", async () => {
base: listener!.url,
headers: { "x-global-header": "1" },
}),
async additionalTests({ storage }) {
async additionalTests(ctx) {
it("custom headers", async () => {
await storage.setItem("authorized", "test", {
await ctx.storage.setItem("authorized", "test", {
headers: { "x-auth-header": "1" },
});
});
it("null item", async () => {
await storage.setItem("nullItem", null);
await storage.setItem("nullStringItem", "null");
expect(await storage.getItem("nullItem")).toBeNull();
expect(await storage.getItem("nanItem")).toBeNull();
expect(await storage.getItem("nullStringItem")).toBeNull();
await ctx.storage.setItem("nullItem", null);
await ctx.storage.setItem("nullStringItem", "null");
expect(await ctx.storage.getItem("nullItem")).toBeNull();
expect(await ctx.storage.getItem("nanItem")).toBeNull();
expect(await ctx.storage.getItem("nullStringItem")).toBeNull();
});
},
});
Expand Down
10 changes: 5 additions & 5 deletions test/drivers/lru-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ describe("drivers: lru-cache with size", () => {
driver: driver({
maxEntrySize: 50,
}),
additionalTests({ storage }) {
additionalTests(ctx) {
it("should not store large items", async () => {
await storage.setItem(
await ctx.storage.setItem(
"big",
"0123456789012345678901234567890123456789012345678901234567890123456789"
);
expect(await storage.getItem("big")).toBe(null);
expect(await ctx.storage.getItem("big")).toBe(null);

await storage.setItemRaw("bigBuff", Buffer.alloc(100));
expect(await storage.getItemRaw("bigBuff")).toBe(null);
await ctx.storage.setItemRaw("bigBuff", Buffer.alloc(100));
expect(await ctx.storage.getItemRaw("bigBuff")).toBe(null);
});
},
});
Expand Down
4 changes: 2 additions & 2 deletions test/drivers/upstash.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe } from "vitest";
import { testDriver } from "./utils";
import driver from "../../src/drivers/upstash";
import upstashDriver from "../../src/drivers/upstash";

const url = process.env.VITE_UPSTASH_REDIS_REST_URL;
const token = process.env.VITE_UPSTASH_REDIS_REST_TOKEN;
Expand All @@ -9,7 +9,7 @@ describe.skipIf(!url || !token)("drivers: upstash", async () => {
process.env.UPSTASH_REDIS_REST_URL = url;
process.env.UPSTASH_REDIS_REST_TOKEN = token;
testDriver({
driver: driver({
driver: upstashDriver({
base: Math.round(Math.random() * 1_000_000).toString(16),
}),
});
Expand Down
27 changes: 17 additions & 10 deletions test/drivers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, expect } from "vitest";
import { it, expect, beforeAll, afterAll } from "vitest";
import {
type Storage,
type Driver,
Expand All @@ -12,15 +12,26 @@ export interface TestContext {
}

export interface TestOptions {
driver: Driver;
driver: Driver | (() => Driver);
additionalTests?: (ctx: TestContext) => void;
}

export function testDriver(opts: TestOptions) {
const ctx: TestContext = {
storage: createStorage({ driver: opts.driver }),
driver: opts.driver,
};
const ctx = {} as TestContext;

beforeAll(() => {
ctx.driver =
typeof opts.driver === "function" ? opts.driver() : opts.driver;

ctx.storage = createStorage({
driver: ctx.driver,
});
});

afterAll(async () => {
await ctx.driver?.dispose?.();
await ctx.storage?.dispose?.();
});

it("init", async () => {
await restoreSnapshot(ctx.storage, { initial: "works" });
Expand Down Expand Up @@ -167,8 +178,4 @@ export function testDriver(opts: TestOptions) {
await ctx.storage.clear();
expect(await ctx.storage.getKeys()).toMatchObject([]);
});

it("dispose", async () => {
await ctx.storage.dispose();
});
}
13 changes: 7 additions & 6 deletions test/drivers/vercel-blob.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { describe } from "vitest";
import { testDriver } from "./utils";
import driver from "../../src/drivers/vercel-blob";
import vercelBlobDriver from "../../src/drivers/vercel-blob";

const token = process.env.VITE_VERCEL_BLOB_READ_WRITE_TOKEN;

describe.skipIf(!token)("drivers: vercel-blob", async () => {
process.env.VERCEL_TEST_READ_WRITE_TOKEN = token;
testDriver({
driver: driver({
access: "public",
base: Math.round(Math.random() * 1_000_000).toString(16),
envPrefix: "VERCEL_TEST",
}),
driver: () =>
vercelBlobDriver({
access: "public",
base: Math.round(Math.random() * 1_000_000).toString(16),
envPrefix: "VERCEL_TEST",
}),
});
});
24 changes: 6 additions & 18 deletions test/drivers/vercel-kv.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { describe, it } from "vitest";
// import driver from "../../src/drivers/vercel-kv";
import { describe } from "vitest";
import vercelKVDriver from "../../src/drivers/vercel-kv";
import { testDriver } from "./utils";

// TODO: Only works locally with env. Mock upstash client to run in CI

const hasEnv = process.env.KV_REST_API_URL && process.env.KV_REST_API_TOKEN;

if (hasEnv) {
describe("drivers: vercel-kv", async () => {
const driver = await import("../../src/drivers/vercel-kv").then(
(r) => r.default
);
testDriver({
driver: driver({}),
});
});
} else {
// TODO: vitest describe.skipIf has no effect!!
describe("drivers: vercel-kv", () => {
it.skip("", () => {});
describe.skipIf(!hasEnv)("drivers: vercel-kv", async () => {
testDriver({
driver: () => vercelKVDriver({}),
});
}
});

0 comments on commit 33d90a9

Please sign in to comment.