clerk / t3-turbo-and-clerk

A t3 Turbo starter with Clerk as the auth provider.
https://clerk.dev
1.03k stars 75 forks source link

Clerk throws an error when running jest test #65

Closed matewilk closed 1 year ago

matewilk commented 1 year ago

Describe the bug When running turbo test with jest I'm getting the following error:

$ npx turbo test
• Packages in scope: @acme/api, @acme/db, @acme/expo, @acme/nextjs, @acme/tailwind-config
• Running test in 5 packages
• Remote caching disabled
@acme/api:test: cache miss, executing 0889905f60e2a623
@acme/api:test: 
@acme/api:test: > @acme/api@0.1.0 test /Users/matewilk/Workspace/palinka/packages/api
@acme/api:test: > jest
@acme/api:test: 
@acme/api:test: FAIL src/router/post.test.ts
@acme/api:test:   ● Test suite failed to run
@acme/api:test: 
@acme/api:test:     ReferenceError: Request is not defined
@acme/api:test: 
@acme/api:test:       2 | import { type inferAsyncReturnType } from "@trpc/server";
@acme/api:test:       3 | import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
@acme/api:test:     > 4 | import { getAuth } from "@clerk/nextjs/server";
@acme/api:test:         | ^
@acme/api:test:       5 | import type {
@acme/api:test:       6 |   SignedInAuthObject,
@acme/api:test:       7 |   SignedOutAuthObject,
@acme/api:test: 
@acme/api:test:       at Object.Request (../../node_modules/src/server/web/spec-extension/request.ts:10:34)
@acme/api:test:       at Object.<anonymous> (../../node_modules/next/server.js:2:16)
@acme/api:test:       at Object.<anonymous> (../../node_modules/@clerk/nextjs/dist/server/utils.js:5:18)
@acme/api:test:       at Object.<anonymous> (../../node_modules/@clerk/nextjs/dist/server/getAuth.js:6:17)
@acme/api:test:       at Object.<anonymous> (../../node_modules/@clerk/nextjs/dist/server/index.js:5:22)
@acme/api:test:       at Object.<anonymous> (../../node_modules/@clerk/nextjs/server.js:1:38)
@acme/api:test:       at Object.<anonymous> (src/context.ts:4:1)
@acme/api:test:       at Object.<anonymous> (src/router/post.test.ts:3:1)
@acme/api:test: 
@acme/api:test: Test Suites: 1 failed, 1 total
@acme/api:test: Tests:       0 total
@acme/api:test: Snapshots:   0 total
@acme/api:test: Time:        2.077 s
@acme/api:test: Ran all test suites.
@acme/api:test: (node:44169) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
@acme/api:test: (Use `node --trace-warnings ...` to show where the warning was created)
@acme/api:test:  ELIFECYCLE  Test failed. See above for more details.
@acme/api:test: ERROR: command finished with error: command (/Users/matewilk/Workspace/palinka/packages/api) pnpm run test exited (1)
command (/Users/matewilk/Workspace/palinka/packages/api) pnpm run test exited (1)

 Tasks:    0 successful, 1 total
Cached:    0 cached, 1 total
  Time:    2.997s 

 ERROR  run failed: command  exited (1)

To Reproduce Steps to reproduce the behavior:

  1. cd to /packages/api and setup jest
    1. install necessary modules pnpm add jest @types/jest ts-jest ts-node
  2. create jest.config.js file in the same directory
    /** @type {import('ts-jest').JestConfigWithTsJest} */
    module.exports = {
    preset: "ts-jest",
    testEnvironment: "node",
    };
  3. add "test": {} inside pipeline in turbo.json file
  4. add "test": "jest" inside scripts of /packages/api/package.json file
  5. create post.test.ts file in /packages/api/src/router
    
    import { postRouter } from "./post";
    import { type inferProcedureInput } from "@trpc/server";
    import { createContextInner } from "../context";
    import { appRouter } from "../router";
    import { SignedInAuthObject } from "@clerk/nextjs/dist/api";

describe("postRouter", () => { it("byId", async () => { const auth = { userId: "123", } as SignedInAuthObject; const ctx = await createContextInner({ auth }); const caller = appRouter.createCaller(ctx);

type Input = inferProcedureInput<typeof postRouter.byId>;
const input: Input = "1";

const result = await caller.post.byId(input);

expect(result).toMatchObject({});

}); });


6. run your test with `npx turbo test` (from the root directory)

**Expected behaviour**
No errors related to @clerk packages imports.
The error seems to be coming from `/packages/api/router/context.ts` fails.
Apparently some modules are not being loaded as expected.

**Logs**
as shown above

**Screenshots**
not applicable

**Desktop (please complete the following information):**
 - OS: [e.g. iOS]
 - Browser [e.g. chrome, safari]
 - Version [e.g. 22]

**Smartphone (please complete the following information):**
 - Device: [e.g. iPhone6]
 - OS: [e.g. iOS8.1]
 - Browser [e.g. stock browser, safari]
 - Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
matewilk commented 1 year ago

Ok, I think I've found the answer 👍

The error went away 🎉