Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@prisma/adapter-d1 is failing with an import error when called inside vitest tests #23911

Open
mgcrea opened this issue Apr 21, 2024 · 3 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: d1 Issues related to Cloudflare D1 topic: driverAdapters topic: @prisma/adapter-d1 topic: vitest

Comments

@mgcrea
Copy link

mgcrea commented Apr 21, 2024

Bug description

I'm trying to setup a small Cloudflare Worker app, that leverages @prisma/adapter-d1 and served by hono, I'm encountering issues when trying to run unit tests. Unfortunately the prisma WASM import fails with the following error:

Error: No such module "Users/olivier/Projects/cloudflare/cloudflare-hono-worker/node_modules/.prisma/client/#wasm-engine-loader".
    at Object.getQueryEngineWasmModule (Users/olivier/Projects/cloudflare/cloudflare-hono-worker/node_modules/.prisma/client/wasm.js?mf_vitest_no_cjs_esm_shim:182:5)
    at Users/olivier/Projects/cloudflare/cloudflare-hono-worker/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:731
    at Object.loadLibrary (Users/olivier/Projects/cloudflare/cloudflare-hono-worker/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:975)
    at gt.loadEngine (Users/olivier/Projects/cloudflare/cloudflare-hono-worker/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:3194)
    at gt.instantiateLibrary (Users/olivier/Projects/cloudflare/cloudflare-hono-worker/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:2778) {
  clientVersion: '5.12.1'
}

How to reproduce

Create a basic d1 prisma app following the official tutorial

Add a vitest unit test:

import { createExecutionContext, env, waitOnExecutionContext } from "cloudflare:test";
import { testClient } from "hono/testing";
import { app } from "src/app";
import { describe, expect, test } from "vitest";

describe("App", () => {
  const testApp = testClient(app, env);

  test("GET /users", async () => {
    const ctx = createExecutionContext();
    const res = await testApp.users.$get({ ctx });
    await waitOnExecutionContext(ctx);
    expect(res.status).toBe(200);
    expect(await res.text()).toBe("Hello Hono!");
  });
});

Can be fixed with this change in wasm.js:

config.engineWasm = {
  getRuntime: () => require('./query_engine_bg.js'),
  getQueryEngineWasmModule: async () => {
    return (await import('./query_engine_bg.wasm')).default // <-- fix
  }
}

but probably breaking other things!

Expected behavior

Prisma should work inside vitest.

Prisma information

// Add your schema.prisma
// Add your code using Prisma Client

Environment & setup

  • OS: macOS
  • Database: SQLite (D1 adapter)
  • Node.js version: v20.12.0

Prisma Version

▶ nr prisma -v                                                                                                                                    master|◒ 12 
10.5.0
@mgcrea mgcrea added the kind/bug A reported bug. label Apr 21, 2024
@mgcrea mgcrea changed the title @prisma/adapter-d1 is failing when called inside vitest tests @prisma/adapter-d1 is failing with an import error when called inside vitest tests Apr 21, 2024
@janpio janpio added team/client Issue for team Client. topic: driverAdapters topic: @prisma/adapter-d1 topic: d1 Issues related to Cloudflare D1 bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: vitest labels Apr 23, 2024
@janpio
Copy link
Member

janpio commented Apr 26, 2024

Possibly related, also vitest, problem: #23855

@jkomyno
Copy link
Contributor

jkomyno commented Apr 29, 2024

Hi @mgcrea, thanks for opening this. While we reason about this issue internally, could you please share whether using the vite-plugin-wasm plugin in your vitest configuration would fix this problem without the need to modify wasm.js?

@jkomyno jkomyno self-assigned this Apr 29, 2024
@Jolg42 Jolg42 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 3, 2024
@Jolg42
Copy link
Member

Jolg42 commented May 3, 2024

I could reproduce using the repro mentioned in:

I also hit this bug (also mentioned cloudflare/workers-sdk#5127 (comment)).

Here's a full reproduction repo https://github.com/vladinator1000/prisma-vitest-miniflare/tree/27410822df6161c2d97006352fe74dbd0692e90e

Originally posted by @vladinator1000 in cloudflare/workers-sdk#5685 (comment)

npm i
npm run gen
npm run db-migrate 
npm run itest 


> prisma-vitest-miniflare@1.0.0 itest
> prisma generate && NODE_OPTIONS='--experimental-vm-modules' vitest --config ./vitest.config.integration.ts

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma

✔ Generated Prisma Client (v5.12.1) to ./node_modules/@prisma/client in 114ms

....

 DEV  v1.3.0 /workspaces/prisma-vitest-miniflare

Using vars defined in .dev.vars
[vpw:inf] Starting isolated runtimes for vitest.config.integration.ts...
workerd/server/server.c++:2802: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=%2Fworkspaces%2Fprisma-vitest-miniflare%2Fnode_modules%2F.prisma%2Fclient%2Fwasm-worker-loader.js&referrer=%2Fworkspaces%2Fprisma-vitest-miniflare%2Fnode_modules%2F.prisma%2Fclient%2Fwasm.js%3Fmf_vitest_no_cjs_esm_shim
 ❯ src/user.itest.ts (1)
   ❯ user model (1)
     × create user
       ⠼ [ beforeEach ]

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/user.itest.ts > user model > create user
Error: No such module "workspaces/prisma-vitest-miniflare/node_modules/.prisma/client/#wasm-engine-loader".
 ❯ Object.getQueryEngineWasmModule workspaces/prisma-vitest-miniflare/node_modules/.prisma/client/wasm.js?mf_vitest_no_cjs_esm_shim:160:5
 ❯ workspaces/prisma-vitest-miniflare/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:731
 ❯ Object.loadLibrary workspaces/prisma-vitest-miniflare/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:975
 ❯ gt.loadEngine workspaces/prisma-vitest-miniflare/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:3194
 ❯ gt.instantiateLibrary workspaces/prisma-vitest-miniflare/node_modules/@prisma/client/runtime/wasm.js?mf_vitest_no_cjs_esm_shim:11:2778

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { clientVersion: '5.12.1' }
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  1 failed (1)
   Start at  14:08:05
   Duration  1.46s (transform 43ms, setup 0ms, collect 79ms, tests 25ms, environment 0ms, prepare 284ms)

@jkomyno jkomyno removed their assignment May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: d1 Issues related to Cloudflare D1 topic: driverAdapters topic: @prisma/adapter-d1 topic: vitest
Projects
None yet
Development

No branches or pull requests

4 participants