Vitest, Clerk and TRPC integration

After adding Clerk to my application, my tests are encountering the following error:
ReferenceError: Request is not defined
❯ Object.<anonymous> node_modules/src/server/web/spec-extension/request.ts:10:34
❯ Object.<anonymous> node_modules/next/server.js:2:16
ReferenceError: Request is not defined
❯ Object.<anonymous> node_modules/src/server/web/spec-extension/request.ts:10:34
❯ Object.<anonymous> node_modules/next/server.js:2:16
Looking deeper into it, it seems to be related to how the NextJS server code exports commonjs and esm compatible versions of the request/response modules. The error is fired when the getAuth function from clerk is being used, as for why this occurs I have zero clue. I assume it's importing the NextJS modules in a way which makes Vitest very unhappy. For now my workaround is to separate the createTRPCContext into a completely separate file, such that the dependency flow for my Vitest tests don't end up needing to pull in the getAuth function from clerk and happily rely on just createInnerTRPCContext. Meanwhile for my API route it still imports the full createTRPCContext and getAuth functions as needed for runtime. Not sure if there's a better way thinkies
0 Replies
No replies yetBe the first to reply to this messageJoin