Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions packages/miniflare/test/plugins/cache/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
RequestInit,
Response,
} from "miniflare";
// eslint-disable-next-line no-restricted-imports
import { beforeEach, expect, onTestFinished, test } from "vitest";
import { beforeEach, type ExpectStatic, onTestFinished, test } from "vitest";
import {
MiniflareDurableObjectControlStub,
miniflareTest,
Expand Down Expand Up @@ -271,7 +270,10 @@ test("put overrides existing responses", async ({ expect }) => {
});

// Note this helper must be used with serial tests to avoid races.
async function testExpire(opts: { headers: HeadersInit; expectedTtl: number }) {
async function testExpire(
expect: ExpectStatic,
opts: { headers: HeadersInit; expectedTtl: number }
) {
const cache = ctx.caches.default;
const defaultObject = ctx.defaultObject;

Expand All @@ -290,30 +292,33 @@ async function testExpire(opts: { headers: HeadersInit; expectedTtl: number }) {
expect(res).toBeUndefined();
}
test("expires after Expires", async ({ expect }) => {
await testExpire({
await testExpire(expect, {
headers: {
Expires: new Date(1_000_000 + 2_000).toUTCString(),
},
expectedTtl: 2000,
});
});
test("expires after Cache-Control's max-age", async ({ expect }) => {
await testExpire({
await testExpire(expect, {
headers: { "Cache-Control": "max-age=1" },
expectedTtl: 1000,
});
});
test("expires after Cache-Control's s-maxage", async ({ expect }) => {
await testExpire({
await testExpire(expect, {
headers: { "Cache-Control": "s-maxage=1, max-age=10" },
expectedTtl: 1000,
});
});

async function testIsCached(opts: {
headers: Record<string, string>;
cached: boolean;
}) {
async function testIsCached(
expect: ExpectStatic,
opts: {
headers: Record<string, string>;
cached: boolean;
}
) {
const cache = ctx.caches.default;

// Use different key for each invocation of this helper
Expand All @@ -332,33 +337,33 @@ async function testIsCached(opts: {
expect(res?.status).toBe(opts.cached ? 200 : undefined);
}
test("put does not cache with private Cache-Control", async ({ expect }) => {
await testIsCached({
await testIsCached(expect, {
headers: { "Cache-Control": "private" },
cached: false,
});
});
test("put does not cache with no-store Cache-Control", async ({ expect }) => {
await testIsCached({
await testIsCached(expect, {
headers: { "Cache-Control": "no-store" },
cached: false,
});
});
test("put does not cache with no-cache Cache-Control", async ({ expect }) => {
await testIsCached({
await testIsCached(expect, {
headers: { "Cache-Control": "no-cache" },
cached: false,
});
});
test("put does not cache with Set-Cookie", async ({ expect }) => {
await testIsCached({
await testIsCached(expect, {
headers: { "Set-Cookie": "key=value" },
cached: false,
});
});
test("put caches with Set-Cookie if Cache-Control private=set-cookie", async ({
expect,
}) => {
await testIsCached({
await testIsCached(expect, {
headers: {
"Cache-Control": "private=set-cookie",
"Set-Cookie": "key=value",
Expand Down
4 changes: 2 additions & 2 deletions packages/miniflare/test/plugins/cache/parse-http.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "vitest";
import { runWorkerTest } from "../../test-shared";

test("parseHttpResponse: parses HTTP response messages", async () => {
await runWorkerTest("cache", "cache", "parse-http.ts");
test("parseHttpResponse: parses HTTP response messages", async ({ expect }) => {
await runWorkerTest(expect, "cache", "cache", "parse-http.ts");
});
16 changes: 11 additions & 5 deletions packages/miniflare/test/plugins/d1/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import assert from "node:assert";
import fs from "node:fs/promises";
import { type D1Database } from "@cloudflare/workers-types/experimental";
import { Miniflare, MiniflareOptions } from "miniflare";
// eslint-disable-next-line no-restricted-imports
import { beforeEach, expect, onTestFinished, test } from "vitest";
import { beforeEach, type ExpectStatic, onTestFinished, test } from "vitest";
import { useDispose, useTmp, utf8Encode } from "../../test-shared";
import { binding, ctx, getDatabase, opts } from "./test";

Expand Down Expand Up @@ -626,7 +625,7 @@ test("dumpSql exports and imports complete database structure and content correc
await mirrorDb.exec(dump);

// Verify that the schema and data in both databases are equal
await isDatabaseEqual(originalDb, mirrorDb);
await isDatabaseEqual(expect, originalDb, mirrorDb);
});

/**
Expand Down Expand Up @@ -700,7 +699,11 @@ async function fillDummyData(db: D1Database) {
* It retrieves the schema of both databases, compares the tables, and then
* checks if the data in each table is identical.
*/
async function isDatabaseEqual(db: D1Database, db2: D1Database) {
async function isDatabaseEqual(
expect: ExpectStatic,
db: D1Database,
db2: D1Database
) {
// SQL to select schema excluding internal tables
const selectSchemaSQL =
"SELECT * FROM sqlite_master WHERE type = 'table' AND (name NOT LIKE 'sqlite_%' AND name NOT LIKE '_cf_%')";
Expand All @@ -716,7 +719,10 @@ async function isDatabaseEqual(db: D1Database, db2: D1Database) {
const tableName = table.name as string;

// Escape and ORDER BY to ensure consistent ordering
const selectTableSQL = `SELECT * FROM "${tableName.replace(/"/g, '""')}" ORDER BY id ASC`;
const selectTableSQL = `SELECT * FROM "${tableName.replace(
/"/g,
'""'
)}" ORDER BY id ASC`;

const originalData = (await db.prepare(selectTableSQL).all()).results;
const mirrorData = (await db2.prepare(selectTableSQL).all()).results;
Expand Down
69 changes: 39 additions & 30 deletions packages/miniflare/test/plugins/kv/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
MiniflareOptions,
ReplaceWorkersTypes,
} from "miniflare";
// eslint-disable-next-line no-restricted-imports
import { beforeEach, expect, test } from "vitest";
import { beforeEach, type ExpectStatic, test } from "vitest";
import {
createJunkStream,
FIXTURES_PATH,
Expand Down Expand Up @@ -84,10 +83,13 @@ beforeEach(async () => {
await ctx.object.enableFakeTimers(secondsToMillis(TIME_NOW));
});

async function testValidatesKey(opts: {
method: string;
f: (kv: ReplaceWorkersTypes<KVNamespace>, key?: any) => Promise<void>;
}) {
async function testValidatesKey(
expect: ExpectStatic,
opts: {
method: string;
f: (kv: ReplaceWorkersTypes<KVNamespace>, key?: any) => Promise<void>;
}
) {
const { kv } = ctx;
kv.ns = "";
await expect(opts.f(kv, "")).rejects.toThrow(
Expand All @@ -107,7 +109,7 @@ async function testValidatesKey(opts: {
}

test("get: validates key", async ({ expect }) => {
await testValidatesKey({
await testValidatesKey(expect, {
method: "get",
f: async (kv, key) => {
await kv.get(key);
Expand Down Expand Up @@ -289,7 +291,7 @@ test("get: validates but ignores cache ttl", async ({ expect }) => {
});

test("put: validates key", async ({ expect }) => {
await testValidatesKey({
await testValidatesKey(expect, {
method: "put",
f: async (kv, key) => {
await kv.put(key, "value");
Expand Down Expand Up @@ -388,7 +390,9 @@ test("put: validates expiration", async ({ expect }) => {
kv.put("key", "value", { expiration: TIME_NOW + 30 })
).rejects.toThrow(
new Error(
`KV PUT failed: 400 Invalid expiration of ${TIME_NOW + 30}. Expiration times must be at least 60 seconds in the future.`
`KV PUT failed: 400 Invalid expiration of ${
TIME_NOW + 30
}. Expiration times must be at least 60 seconds in the future.`
)
);
});
Expand Down Expand Up @@ -416,13 +420,15 @@ test("put: validates metadata size", async ({ expect }) => {
})
).rejects.toThrow(
new Error(
`KV PUT failed: 413 Metadata length of ${maxMetadataSize + 1} exceeds limit of ${maxMetadataSize}.`
`KV PUT failed: 413 Metadata length of ${
maxMetadataSize + 1
} exceeds limit of ${maxMetadataSize}.`
)
);
});

test("delete: validates key", async ({ expect }) => {
await testValidatesKey({
await testValidatesKey(expect, {
method: "delete",
f: async (kv, key) => {
await kv.delete(key);
Expand All @@ -441,14 +447,17 @@ test("delete: does nothing for non-existent keys", async ({ expect }) => {
await kv.delete("key");
});

async function testList(opts: {
values: Record<
string,
{ value: string; expiration?: number; metadata?: unknown }
>;
options?: KVNamespaceListOptions;
pages: KVNamespaceListResult<unknown>["keys"][];
}) {
async function testList(
expect: ExpectStatic,
opts: {
values: Record<
string,
{ value: string; expiration?: number; metadata?: unknown }
>;
options?: KVNamespaceListOptions;
pages: KVNamespaceListResult<unknown>["keys"][];
}
) {
const { kv, ns } = ctx;
for (const [key, value] of Object.entries(opts.values)) {
await kv.put(key, value.value, {
Expand Down Expand Up @@ -484,7 +493,7 @@ async function testList(opts: {
}

test("list: lists keys in sorted order", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
key3: { value: "value3" },
key1: { value: "value1" },
Expand All @@ -494,7 +503,7 @@ test("list: lists keys in sorted order", async ({ expect }) => {
});
});
test("list: lists keys matching prefix", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
section1key1: { value: "value11" },
section1key2: { value: "value12" },
Expand All @@ -505,7 +514,7 @@ test("list: lists keys matching prefix", async ({ expect }) => {
});
});
test("list: prefix is case-sensitive", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
key1: { value: "lower1" },
key2: { value: "lower2 " },
Expand All @@ -517,7 +526,7 @@ test("list: prefix is case-sensitive", async ({ expect }) => {
});
});
test("list: prefix permits special characters", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
["key\\_%1"]: { value: "value1" },
["key\\a"]: { value: "bad1" },
Expand All @@ -530,7 +539,7 @@ test("list: prefix permits special characters", async ({ expect }) => {
});
});
test("list: lists keys with expiration", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
key1: { value: "value1", expiration: TIME_FUTURE },
key2: { value: "value2", expiration: TIME_FUTURE + 100 },
Expand All @@ -546,7 +555,7 @@ test("list: lists keys with expiration", async ({ expect }) => {
});
});
test("list: lists keys with metadata", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
key1: { value: "value1", metadata: { testing: 1 } },
key2: { value: "value2", metadata: { testing: 2 } },
Expand All @@ -562,7 +571,7 @@ test("list: lists keys with metadata", async ({ expect }) => {
});
});
test("list: lists keys with expiration and metadata", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
key1: {
value: "value1",
Expand Down Expand Up @@ -602,15 +611,15 @@ test("list: lists keys with expiration and metadata", async ({ expect }) => {
});
});
test("list: returns an empty list with no keys", async ({ expect }) => {
await testList({
await testList(expect, {
values: {},
pages: [[]],
});
});
test("list: returns an empty list with no matching keys", async ({
expect,
}) => {
await testList({
await testList(expect, {
values: {
key1: { value: "value1" },
key2: { value: "value2" },
Expand All @@ -621,7 +630,7 @@ test("list: returns an empty list with no matching keys", async ({
});
});
test("list: paginates keys", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
key1: { value: "value1" },
key2: { value: "value2" },
Expand All @@ -632,7 +641,7 @@ test("list: paginates keys", async ({ expect }) => {
});
});
test("list: paginates keys matching prefix", async ({ expect }) => {
await testList({
await testList(expect, {
values: {
section1key1: { value: "value11" },
section1key2: { value: "value12" },
Expand Down
Loading
Loading