diff --git a/packages/miniflare/test/plugins/cache/index.spec.ts b/packages/miniflare/test/plugins/cache/index.spec.ts index d870d6493b..a2f0b1d22d 100644 --- a/packages/miniflare/test/plugins/cache/index.spec.ts +++ b/packages/miniflare/test/plugins/cache/index.spec.ts @@ -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, @@ -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; @@ -290,7 +292,7 @@ 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(), }, @@ -298,22 +300,25 @@ test("expires after Expires", async ({ expect }) => { }); }); 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; - cached: boolean; -}) { +async function testIsCached( + expect: ExpectStatic, + opts: { + headers: Record; + cached: boolean; + } +) { const cache = ctx.caches.default; // Use different key for each invocation of this helper @@ -332,25 +337,25 @@ 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, }); @@ -358,7 +363,7 @@ test("put does not cache with Set-Cookie", async ({ expect }) => { 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", diff --git a/packages/miniflare/test/plugins/cache/parse-http.spec.ts b/packages/miniflare/test/plugins/cache/parse-http.spec.ts index d5aa8caff4..2cea3dbecc 100644 --- a/packages/miniflare/test/plugins/cache/parse-http.spec.ts +++ b/packages/miniflare/test/plugins/cache/parse-http.spec.ts @@ -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"); }); diff --git a/packages/miniflare/test/plugins/d1/suite.ts b/packages/miniflare/test/plugins/d1/suite.ts index 43f675b751..daeeb0b97e 100644 --- a/packages/miniflare/test/plugins/d1/suite.ts +++ b/packages/miniflare/test/plugins/d1/suite.ts @@ -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"; @@ -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); }); /** @@ -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_%')"; @@ -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; diff --git a/packages/miniflare/test/plugins/kv/index.spec.ts b/packages/miniflare/test/plugins/kv/index.spec.ts index 932b40c60e..81d63fa3f7 100644 --- a/packages/miniflare/test/plugins/kv/index.spec.ts +++ b/packages/miniflare/test/plugins/kv/index.spec.ts @@ -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, @@ -84,10 +83,13 @@ beforeEach(async () => { await ctx.object.enableFakeTimers(secondsToMillis(TIME_NOW)); }); -async function testValidatesKey(opts: { - method: string; - f: (kv: ReplaceWorkersTypes, key?: any) => Promise; -}) { +async function testValidatesKey( + expect: ExpectStatic, + opts: { + method: string; + f: (kv: ReplaceWorkersTypes, key?: any) => Promise; + } +) { const { kv } = ctx; kv.ns = ""; await expect(opts.f(kv, "")).rejects.toThrow( @@ -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); @@ -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"); @@ -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.` ) ); }); @@ -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); @@ -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["keys"][]; -}) { +async function testList( + expect: ExpectStatic, + opts: { + values: Record< + string, + { value: string; expiration?: number; metadata?: unknown } + >; + options?: KVNamespaceListOptions; + pages: KVNamespaceListResult["keys"][]; + } +) { const { kv, ns } = ctx; for (const [key, value] of Object.entries(opts.values)) { await kv.put(key, value.value, { @@ -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" }, @@ -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" }, @@ -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 " }, @@ -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" }, @@ -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 }, @@ -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 } }, @@ -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", @@ -602,7 +611,7 @@ 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: [[]], }); @@ -610,7 +619,7 @@ test("list: returns an empty list with no keys", async ({ expect }) => { test("list: returns an empty list with no matching keys", async ({ expect, }) => { - await testList({ + await testList(expect, { values: { key1: { value: "value1" }, key2: { value: "value2" }, @@ -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" }, @@ -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" }, diff --git a/packages/miniflare/test/plugins/kv/sites.spec.ts b/packages/miniflare/test/plugins/kv/sites.spec.ts index f9c681e75f..7a0bec214d 100644 --- a/packages/miniflare/test/plugins/kv/sites.spec.ts +++ b/packages/miniflare/test/plugins/kv/sites.spec.ts @@ -3,8 +3,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import esbuild from "esbuild"; import { Miniflare } from "miniflare"; -// eslint-disable-next-line no-restricted-imports -import { beforeAll, expect, test } from "vitest"; +import { beforeAll, type ExpectStatic, test } from "vitest"; import { useDispose, useTmp } from "../../test-shared"; const FIXTURES_PATH = path.resolve(__dirname, "../../fixtures/sites"); @@ -43,10 +42,13 @@ const routeContents = { "/b/b.txt": "b", }; -async function testGet(opts: { - options: { siteInclude?: string[]; siteExclude?: string[] }; - expectedRoutes: Set; -}) { +async function testGet( + expect: ExpectStatic, + opts: { + options: { siteInclude?: string[]; siteExclude?: string[] }; + expectedRoutes: Set; + } +) { const tmp = await useTmp(); for (const [route, contents] of Object.entries(routeContents)) { const routePath = path.join(tmp, route === "/" ? "index.html" : route); @@ -71,19 +73,19 @@ async function testGet(opts: { } test("gets all assets with no filter", async ({ expect }) => { - await testGet({ + await testGet(expect, { options: {}, expectedRoutes: new Set(["/", "/a.txt", "/b/b.txt"]), }); }); test("gets included assets with include filter", async ({ expect }) => { - await testGet({ + await testGet(expect, { options: { siteInclude: ["b"] }, expectedRoutes: new Set(["/b/b.txt"]), }); }); test("gets all but excluded assets with include filter", async ({ expect }) => { - await testGet({ + await testGet(expect, { options: { siteExclude: ["b"] }, expectedRoutes: new Set(["/", "/a.txt"]), }); @@ -91,14 +93,14 @@ test("gets all but excluded assets with include filter", async ({ expect }) => { test("gets included assets with include and exclude filters", async ({ expect, }) => { - await testGet({ + await testGet(expect, { options: { siteInclude: ["*.txt"], siteExclude: ["b"] }, expectedRoutes: new Set(["/a.txt", "/b/b.txt"]), }); }); // Tests for checking different types of globs are matched correctly -async function testMatch(include: string) { +async function testMatch(expect: ExpectStatic, include: string) { const tmp = await useTmp(); const dir = path.join(tmp, "a", "b", "c"); await fs.mkdir(dir, { recursive: true }); @@ -115,19 +117,19 @@ async function testMatch(include: string) { } test("matches file name pattern", async ({ expect }) => { - await testMatch("test.txt"); + await testMatch(expect, "test.txt"); }); test("matches exact pattern", async ({ expect }) => { - await testMatch("a/b/c/test.txt"); + await testMatch(expect, "a/b/c/test.txt"); }); test("matches extension patterns", async ({ expect }) => { - await testMatch("*.txt"); + await testMatch(expect, "*.txt"); }); test("matches globstar patterns", async ({ expect }) => { - await testMatch("**/*.txt"); + await testMatch(expect, "**/*.txt"); }); test("matches wildcard directory patterns", async ({ expect }) => { - await testMatch("a/*/c/*.txt"); + await testMatch(expect, "a/*/c/*.txt"); }); test("doesn't cache assets", async ({ expect }) => { diff --git a/packages/miniflare/test/plugins/local-explorer/d1.spec.ts b/packages/miniflare/test/plugins/local-explorer/d1.spec.ts index 4c4a2f2c3e..3ff1334866 100644 --- a/packages/miniflare/test/plugins/local-explorer/d1.spec.ts +++ b/packages/miniflare/test/plugins/local-explorer/d1.spec.ts @@ -60,7 +60,8 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, - zD1ListDatabasesResponse + zD1ListDatabasesResponse, + expect ); expect(data.success).toBe(true); @@ -87,7 +88,8 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, - zD1ListDatabasesResponse + zD1ListDatabasesResponse, + expect ); expect(data).toMatchObject({ @@ -119,7 +121,8 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, - zD1RawDatabaseQueryResponse + zD1RawDatabaseQueryResponse, + expect ); expect(data.success).toBe(true); @@ -152,7 +155,8 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, - zD1RawDatabaseQueryResponse + zD1RawDatabaseQueryResponse, + expect ); expect(data).toMatchObject({ @@ -190,7 +194,8 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, - zD1RawDatabaseQueryResponse + zD1RawDatabaseQueryResponse, + expect ); expect(data).toMatchObject({ @@ -233,6 +238,7 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, zD1ApiResponseCommonFailure, + expect, 404 ); @@ -261,6 +267,7 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, zD1ApiResponseCommonFailure, + expect, 500 ); @@ -291,6 +298,7 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, zD1ApiResponseCommonFailure, + expect, 400 ); @@ -317,6 +325,7 @@ INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com'); const data = await expectValidResponse( response, zD1ApiResponseCommonFailure, + expect, 400 ); diff --git a/packages/miniflare/test/plugins/local-explorer/helpers.ts b/packages/miniflare/test/plugins/local-explorer/helpers.ts index 4c226edd93..eb32723a00 100644 --- a/packages/miniflare/test/plugins/local-explorer/helpers.ts +++ b/packages/miniflare/test/plugins/local-explorer/helpers.ts @@ -1,6 +1,5 @@ -// eslint-disable-next-line no-restricted-imports -import { expect } from "vitest"; import { z } from "zod"; +import type { ExpectStatic } from "vitest"; /** * Validates a response body against a Zod schema and returns typed data. @@ -9,6 +8,7 @@ import { z } from "zod"; export async function expectValidResponse( response: Response, schema: T, + expect: ExpectStatic, expectedStatus = 200 ): Promise> { expect(response.status).toBe(expectedStatus); @@ -17,7 +17,11 @@ export async function expectValidResponse( if (!result.success) { throw new Error( - `Response validation failed:\n${JSON.stringify(result.error.format(), null, 2)}\n\nActual response:\n${JSON.stringify(json, null, 2)}` + `Response validation failed:\n${JSON.stringify( + result.error.format(), + null, + 2 + )}\n\nActual response:\n${JSON.stringify(json, null, 2)}` ); } diff --git a/packages/miniflare/test/plugins/local-explorer/kv.spec.ts b/packages/miniflare/test/plugins/local-explorer/kv.spec.ts index e50d224ed7..1b72a00928 100644 --- a/packages/miniflare/test/plugins/local-explorer/kv.spec.ts +++ b/packages/miniflare/test/plugins/local-explorer/kv.spec.ts @@ -43,7 +43,8 @@ describe("KV API", () => { const data = await expectValidResponse( response, - zWorkersKvNamespaceListNamespacesResponse + zWorkersKvNamespaceListNamespacesResponse, + expect ); expect(data.result).toEqual( expect.arrayContaining([ @@ -113,7 +114,8 @@ describe("KV API", () => { const data = await expectValidResponse( response, - zWorkersKvNamespaceListANamespaceSKeysResponse + zWorkersKvNamespaceListANamespaceSKeysResponse, + expect ); expect(data.result).toEqual( expect.arrayContaining([ @@ -250,7 +252,9 @@ describe("KV API", () => { await kv.put(specialKey, "special-value"); const response = await mf.dispatchFetch( - `${BASE_URL}/storage/kv/namespaces/test-kv-id/values/${encodeURIComponent(specialKey)}` + `${BASE_URL}/storage/kv/namespaces/test-kv-id/values/${encodeURIComponent( + specialKey + )}` ); expect(response.status).toBe(200); @@ -270,7 +274,8 @@ describe("KV API", () => { const data = await expectValidResponse( response, - zWorkersKvNamespaceWriteKeyValuePairWithMetadataResponse + zWorkersKvNamespaceWriteKeyValuePairWithMetadataResponse, + expect ); expect(data.success).toBe(true); @@ -327,7 +332,8 @@ describe("KV API", () => { const data = await expectValidResponse( response, - zWorkersKvNamespaceDeleteKeyValuePairResponse + zWorkersKvNamespaceDeleteKeyValuePairResponse, + expect ); expect(data.success).toBe(true); @@ -369,7 +375,9 @@ describe("KV API", () => { await kv.put(specialKey, "value"); const response = await mf.dispatchFetch( - `${BASE_URL}/storage/kv/namespaces/test-kv-id/values/${encodeURIComponent(specialKey)}`, + `${BASE_URL}/storage/kv/namespaces/test-kv-id/values/${encodeURIComponent( + specialKey + )}`, { method: "DELETE", } @@ -408,7 +416,8 @@ describe("KV API", () => { const data = await expectValidResponse( response, - zWorkersKvNamespaceGetMultipleKeyValuePairsResponse + zWorkersKvNamespaceGetMultipleKeyValuePairsResponse, + expect ); expect(data.success).toBe(true); expect(data.result).toMatchObject({ diff --git a/packages/miniflare/test/plugins/local-explorer/r2.spec.ts b/packages/miniflare/test/plugins/local-explorer/r2.spec.ts index 55e3216c6d..b4aa68459e 100644 --- a/packages/miniflare/test/plugins/local-explorer/r2.spec.ts +++ b/packages/miniflare/test/plugins/local-explorer/r2.spec.ts @@ -38,7 +38,11 @@ describe("R2 API", () => { test("lists all available R2 buckets", async ({ expect }) => { const response = await mf.dispatchFetch(`${BASE_URL}/r2/buckets`); - const data = await expectValidResponse(response, zR2ListBucketsResponse); + const data = await expectValidResponse( + response, + zR2ListBucketsResponse, + expect + ); expect(data.result?.buckets).toEqual( expect.arrayContaining([ expect.objectContaining({ name: "test-bucket" }), @@ -63,7 +67,8 @@ describe("R2 API", () => { const data = await expectValidResponse( response, - zR2BucketListObjectsResponse + zR2BucketListObjectsResponse, + expect ); expect(data.result).toEqual( expect.arrayContaining([ @@ -165,7 +170,8 @@ describe("R2 API", () => { const data = await expectValidResponse( response, - zR2BucketGetObjectResponse + zR2BucketGetObjectResponse, + expect ); expect(data.result).toMatchObject({ key: "metadata-test.txt", @@ -204,7 +210,9 @@ describe("R2 API", () => { await r2.put(specialKey, "special content"); const response = await mf.dispatchFetch( - `${BASE_URL}/r2/buckets/test-bucket/objects/${encodeURIComponent(specialKey)}` + `${BASE_URL}/r2/buckets/test-bucket/objects/${encodeURIComponent( + specialKey + )}` ); expect(response.status).toBe(200); @@ -225,7 +233,8 @@ describe("R2 API", () => { const data = await expectValidResponse( response, - zR2BucketPutObjectResponse + zR2BucketPutObjectResponse, + expect ); expect(data.result).toMatchObject({ key: "upload-test.txt", @@ -319,7 +328,8 @@ describe("R2 API", () => { const data = await expectValidResponse( response, - zR2BucketDeleteObjectsResponse + zR2BucketDeleteObjectsResponse, + expect ); expect(data.result).toEqual( expect.arrayContaining([ diff --git a/packages/miniflare/test/plugins/r2/index.spec.ts b/packages/miniflare/test/plugins/r2/index.spec.ts index 596436b663..4a856197ea 100644 --- a/packages/miniflare/test/plugins/r2/index.spec.ts +++ b/packages/miniflare/test/plugins/r2/index.spec.ts @@ -12,8 +12,7 @@ import { R2_PLUGIN_NAME, ReplaceWorkersTypes, } 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 { FIXTURES_PATH, MiniflareDurableObjectControlStub, @@ -98,10 +97,13 @@ beforeEach(async () => { await ctx.object.enableFakeTimers(1_000_000); }); -async function testValidatesKey(opts: { - method: string; - f: (r2: ReplaceWorkersTypes, key?: any) => Promise; -}) { +async function testValidatesKey( + expect: ExpectStatic, + opts: { + method: string; + f: (r2: ReplaceWorkersTypes, key?: any) => Promise; + } +) { const { r2, ns } = ctx; await expect(opts.f(r2, "x".repeat(1025 - ns.length))).rejects.toThrow( new Error(`${opts.method}: The specified object name is not valid. (10020)`) @@ -156,7 +158,10 @@ test("head: returns metadata for existing keys", async ({ expect }) => { expect(headers.get("X-Key")).toBe("value"); }); test("head: validates key", async ({ expect }) => { - await testValidatesKey({ method: "head", f: (r2, key) => r2.head(key) }); + await testValidatesKey(expect, { + method: "head", + f: (r2, key) => r2.head(key), + }); }); test("get: returns null for non-existent keys", async ({ expect }) => { @@ -207,7 +212,10 @@ test("get: returns metadata and body for existing keys", async ({ expect }) => { expect(headers.get("X-Key")).toBe("value"); }); test("get: validates key", async ({ expect }) => { - await testValidatesKey({ method: "get", f: (r2, key) => r2.get(key) }); + await testValidatesKey(expect, { + method: "get", + f: (r2, key) => r2.get(key), + }); }); test("get: range using object", async ({ expect }) => { const { r2 } = ctx; @@ -402,7 +410,7 @@ test("put: overrides existing keys", async ({ expect }) => { expect(await object.getBlob(objectRow.blob_id)).toBe(null); }); test("put: validates key", async ({ expect }) => { - await testValidatesKey({ + await testValidatesKey(expect, { method: "put", f: (r2, key) => r2.put(key, "v"), }); @@ -611,23 +619,26 @@ test("delete: deletes existing keys", async ({ expect }) => { expect(await r2.head("key3")).toBe(null); }); test("delete: validates key", async ({ expect }) => { - await testValidatesKey({ + await testValidatesKey(expect, { method: "delete", f: (r2, key) => r2.delete(key), }); }); test("delete: validates keys", async ({ expect }) => { - await testValidatesKey({ + await testValidatesKey(expect, { method: "delete", f: (r2, key) => r2.delete(["valid key", key]), }); }); -async function testList(opts: { - keys: string[]; - options?: R2ListOptions; - pages: string[][]; -}) { +async function testList( + expect: ExpectStatic, + opts: { + keys: string[]; + options?: R2ListOptions; + pages: string[][]; + } +) { const { r2, ns } = ctx; // Seed bucket @@ -665,20 +676,20 @@ async function testList(opts: { } } test("list: lists keys in sorted order", async ({ expect }) => { - await testList({ + await testList(expect, { keys: ["key3", "key1", "key2", ", ", "!"], pages: [["!", ", ", "key1", "key2", "key3"]], }); }); test("list: lists keys matching prefix", async ({ expect }) => { - await testList({ + await testList(expect, { keys: ["section1key1", "section1key2", "section2key1"], options: { prefix: "section1" }, pages: [["section1key1", "section1key2"]], }); }); test("list: returns an empty list with no keys", async ({ expect }) => { - await testList({ + await testList(expect, { keys: [], pages: [[]], }); @@ -686,7 +697,7 @@ test("list: returns an empty list with no keys", async ({ expect }) => { test("list: returns an empty list with no matching keys", async ({ expect, }) => { - await testList({ + await testList(expect, { keys: ["key1", "key2", "key3"], options: { prefix: "none" }, pages: [[]], @@ -695,21 +706,21 @@ test("list: returns an empty list with no matching keys", async ({ test("list: returns an empty list with an invalid cursor", async ({ expect, }) => { - await testList({ + await testList(expect, { keys: ["key1", "key2", "key3"], options: { cursor: "bad" }, pages: [[]], }); }); test("list: paginates keys", async ({ expect }) => { - await testList({ + await testList(expect, { keys: ["key1", "key2", "key3"], options: { limit: 2 }, pages: [["key1", "key2"], ["key3"]], }); }); test("list: paginates keys matching prefix", async ({ expect }) => { - await testList({ + await testList(expect, { keys: ["section1key1", "section1key2", "section1key3", "section2key1"], options: { prefix: "section1", limit: 2 }, pages: [["section1key1", "section1key2"], ["section1key3"]], @@ -718,7 +729,7 @@ test("list: paginates keys matching prefix", async ({ expect }) => { test("list: lists keys starting from startAfter exclusive", async ({ expect, }) => { - await testList({ + await testList(expect, { keys: ["key1", "key2", "key3", "key4"], options: { startAfter: "key2" }, pages: [["key3", "key4"]], @@ -727,7 +738,7 @@ test("list: lists keys starting from startAfter exclusive", async ({ test("list: lists keys with startAfter and limit (where startAfter matches key)", async ({ expect, }) => { - await testList({ + await testList(expect, { keys: ["key1", "key2", "key3", "key4"], options: { startAfter: "key1", limit: 2 }, pages: [["key2", "key3"], ["key4"]], @@ -736,7 +747,7 @@ test("list: lists keys with startAfter and limit (where startAfter matches key)" test("list: lists keys with startAfter and limit (where startAfter doesn't match key)", async ({ expect, }) => { - await testList({ + await testList(expect, { keys: ["key1", "key2", "key3", "key4"], options: { startAfter: "key", limit: 2 }, pages: [ diff --git a/packages/miniflare/test/plugins/r2/validator.spec.ts b/packages/miniflare/test/plugins/r2/validator.spec.ts index d56fd42b68..b67147eeec 100644 --- a/packages/miniflare/test/plugins/r2/validator.spec.ts +++ b/packages/miniflare/test/plugins/r2/validator.spec.ts @@ -1,6 +1,6 @@ import { test } from "vitest"; import { runWorkerTest } from "../../test-shared"; -test("testR2Conditional: matches various conditions", async () => { - await runWorkerTest("r2", "r2", "validator.ts"); +test("testR2Conditional: matches various conditions", async ({ expect }) => { + await runWorkerTest(expect, "r2", "r2", "validator.ts"); }); diff --git a/packages/miniflare/test/plugins/shared/router.spec.ts b/packages/miniflare/test/plugins/shared/router.spec.ts index a8a5b5f32d..5fadfd21f4 100644 --- a/packages/miniflare/test/plugins/shared/router.spec.ts +++ b/packages/miniflare/test/plugins/shared/router.spec.ts @@ -1,6 +1,6 @@ import { test } from "vitest"; import { runWorkerTest } from "../../test-shared"; -test("Router: routes requests", async () => { - await runWorkerTest("shared", "shared", "router.ts"); +test("Router: routes requests", async ({ expect }) => { + await runWorkerTest(expect, "shared", "shared", "router.ts"); }); diff --git a/packages/miniflare/test/test-shared/worker-test.ts b/packages/miniflare/test/test-shared/worker-test.ts index 682fe4f742..502a189bc2 100644 --- a/packages/miniflare/test/test-shared/worker-test.ts +++ b/packages/miniflare/test/test-shared/worker-test.ts @@ -2,10 +2,9 @@ import assert from "node:assert"; import path from "node:path"; import esbuild from "esbuild"; import { Miniflare } from "miniflare"; -// eslint-disable-next-line no-restricted-imports -import { expect } from "vitest"; import { useDispose } from "./miniflare"; import { useTmp } from "./storage"; +import type { ExpectStatic } from "vitest"; export const FIXTURES_PATH = path.resolve( require.resolve("miniflare"), @@ -30,6 +29,7 @@ export const EXPORTED_FIXTURES = path.resolve( ); export async function runWorkerTest( + expect: ExpectStatic, testName: string, ...fixturePath: string[] ): Promise {