Skip to content

Commit aa3675b

Browse files
test: turn on Prisma playground tests for Vite plugin (#8085)
1 parent 599def3 commit aa3675b

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

packages/vite-plugin-cloudflare/playground/__test-utils__/responses.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ export async function getJsonResponse(
99
path = "/"
1010
): Promise<null | Record<string, unknown>> {
1111
const response = await getResponse(path);
12-
return response.json();
12+
const text = await response.text();
13+
try {
14+
return JSON.parse(text);
15+
} catch (e) {
16+
throw new Error("Invalid JSON response:\n" + text);
17+
}
1318
}
1419

1520
async function getResponse(path = "/") {

packages/vite-plugin-cloudflare/playground/prisma/__tests__/prisma.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { expect, test } from "vitest";
22
import { getJsonResponse } from "../../__test-utils__";
33

4-
// Need to remove the `.wrangler` directory and run the following commands before the tests.
5-
// I'm not sure how to do this with our testing setup so have skipped the test for now.
6-
// const commands = [
4+
// Need to remove the `.wrangler` directory and run the following commands before the tests:
5+
//
76
// `pnpm wrangler d1 migrations apply prisma-demo-db --local`,
87
// `pnpm wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email\", \"name\") VALUES ('[email protected]', 'Jane Doe (Local)');" --local`,
9-
// ];
8+
// `pnpm prisma generate`
9+
//
10+
// We do this in the `preServe()` hook, in `serve.ts`, that is called from the `packages/vite-plugin-cloudflare/playground/vitest-setup.ts` file.
1011

11-
test.skip("runs D1 query using Prisma", async () => {
12+
test("runs D1 query using Prisma", async () => {
1213
const result = await getJsonResponse();
1314
expect(result).toEqual([
1415
{ id: 1, email: "[email protected]", name: "Jane Doe (Local)" },
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { execSync } from "node:child_process";
2+
import { rmSync } from "node:fs";
3+
import { resolve } from "node:path";
4+
import { startDefaultServe } from "../../vitest-setup";
5+
6+
export function preServe() {
7+
const cwd = process.cwd();
8+
try {
9+
process.chdir(resolve(__dirname, ".."));
10+
rmSync(".wrangler", { force: true, recursive: true });
11+
execSync(`pnpm wrangler d1 migrations apply prisma-demo-db --local`);
12+
execSync(
13+
`pnpm wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email\", \"name\") VALUES ('[email protected]', 'Jane Doe (Local)');" --local`
14+
);
15+
execSync(`pnpm prisma generate`);
16+
} finally {
17+
process.chdir(cwd);
18+
}
19+
}
20+
21+
export async function serve() {
22+
return startDefaultServe();
23+
}

packages/vite-plugin-cloudflare/playground/vitest-setup.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ beforeAll(async (s) => {
151151
await preServe();
152152
}
153153
if (serve) {
154-
server = await serve();
155-
viteServer = mod.viteServer;
154+
server = (await serve()) ?? server;
155+
viteServer = mod.viteServer ?? viteServer;
156156
}
157157
} else {
158158
await startDefaultServe();
@@ -178,7 +178,7 @@ beforeAll(async (s) => {
178178
await browser.close();
179179
}
180180
};
181-
});
181+
}, 15_000);
182182

183183
beforeEach(async () => {
184184
await page.goto(viteTestUrl);
@@ -246,7 +246,9 @@ async function loadConfig(configEnv: ConfigEnv) {
246246
return mergeConfig(options, config || {});
247247
}
248248

249-
export async function startDefaultServe(): Promise<void> {
249+
export async function startDefaultServe(): Promise<
250+
ViteDevServer | http.Server
251+
> {
250252
setupConsoleWarnCollector(serverLogs.warns);
251253

252254
if (!isBuild) {
@@ -297,6 +299,7 @@ export async function startDefaultServe(): Promise<void> {
297299
}
298300
await page.goto(viteTestUrl);
299301
}
302+
return server;
300303
}
301304

302305
/**

0 commit comments

Comments
 (0)