From 6938edd609561be8a68911958d3acc23902ced04 Mon Sep 17 00:00:00 2001 From: Tejas Mehta Date: Wed, 18 Oct 2023 10:33:51 -0400 Subject: [PATCH] fixup! Add Hyperdrive Binding support --- .../test/plugins/hyperdrive/index.spec.ts | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/packages/miniflare/test/plugins/hyperdrive/index.spec.ts b/packages/miniflare/test/plugins/hyperdrive/index.spec.ts index b8dfcd401..b9e31bdf5 100644 --- a/packages/miniflare/test/plugins/hyperdrive/index.spec.ts +++ b/packages/miniflare/test/plugins/hyperdrive/index.spec.ts @@ -1,19 +1,6 @@ -import { Server, createServer } from "net"; import { Hyperdrive } from "@cloudflare/workers-types/experimental"; -import { MiniflareOptions, ReplaceWorkersTypes } from "miniflare"; -import { - MiniflareDurableObjectControlStub, - MiniflareTestContext, - Namespaced, - miniflareTest, - namespace, -} from "../../test-shared"; - -interface Context extends MiniflareTestContext { - ns: string; - hyperdrive: Namespaced>; - object: MiniflareDurableObjectControlStub; -} +import { MiniflareOptions } from "miniflare"; +import { MiniflareTestContext, miniflareTest } from "../../test-shared"; const TEST_CONN_STRING = `postgresql://user:password@localhost:5432/database`; @@ -23,42 +10,45 @@ const opts: Partial = { }, }; -const test = miniflareTest(opts, async (global) => { - return new global.Response(null, { status: 404 }); -}); - -test.beforeEach(async (t) => { - const ns = `${Date.now()}_${Math.floor( - Math.random() * Number.MAX_SAFE_INTEGER - )}`; - t.context.ns = ns; - t.context.hyperdrive = namespace( - ns, - await t.context.mf.getHyperdrive("hyperdrive") - ); -}); +const test = miniflareTest<{ hyperdrive: Hyperdrive }, MiniflareTestContext>( + opts, + async (global, _, env) => { + return global.Response.json({ + connectionString: env.hyperdrive.connectionString, + user: env.hyperdrive.user, + password: env.hyperdrive.password, + database: env.hyperdrive.database, + host: env.hyperdrive.host, + }); + } +); test("connectionString: different from configuration", async (t) => { - const hyperdrive = t.context.hyperdrive; + const hyperdriveResp = await t.context.mf.dispatchFetch("http://localhost/"); + const hyperdrive = await hyperdriveResp.json(); t.not(hyperdrive.connectionString, TEST_CONN_STRING); }); test("user: matches configuration", async (t) => { - const hyperdrive = t.context.hyperdrive; + const hyperdriveResp = await t.context.mf.dispatchFetch("http://localhost/"); + const hyperdrive = await hyperdriveResp.json(); t.is(hyperdrive.user, "user"); }); test("password: matches configuration", async (t) => { - const hyperdrive = t.context.hyperdrive; + const hyperdriveResp = await t.context.mf.dispatchFetch("http://localhost/"); + const hyperdrive = await hyperdriveResp.json(); t.is(hyperdrive.password, "password"); }); test("database: matches configuration", async (t) => { - const hyperdrive = t.context.hyperdrive; + const hyperdriveResp = await t.context.mf.dispatchFetch("http://localhost/"); + const hyperdrive = await hyperdriveResp.json(); t.is(hyperdrive.database, "database"); }); test("host: randomized", async (t) => { - const hyperdrive = t.context.hyperdrive; + const hyperdriveResp = await t.context.mf.dispatchFetch("http://localhost/"); + const hyperdrive = await hyperdriveResp.json(); t.not(hyperdrive.host, "localhost"); });