ARTICLE AD BOX
I am working on a messaging app. I am using express, prisma, typescript, and ts-jest for the backend. I am running into a problem with jest. this is the exact error I am getting when running my testing code:
/home/johan/projects/theOdinProject/messagingApp/messaging-app-back-end/src/generated/prisma/client.ts:52 globalThis['__dirname'] = path.dirname((0, node_url_1.fileURLToPath)(import.meta.url)); ^^^^
SyntaxError: Cannot use 'import.meta' outside a module 1 | import "dotenv/config"; 2 | import { PrismaPg } from "@prisma/adapter-pg"; > 3 | import { PrismaClient } from "../generated/prisma/client"; | ^ 4 | 5 | 6 | const databaseUrl = at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1318:40) at Object.<anonymous> (src/lib/prisma.ts:3:1) at Object.<anonymous> (src/validator/user.ts:2:1) at Object.<anonymous> (src/controller/user.ts:2:1) at Object.<anonymous> (src/routes/user.ts:2:1) at Object.<anonymous> (src/routes/index.ts:1:1) at Object.<anonymous> (src/app.ts:3:1) at Object.<anonymous> (src/test/user.test.ts:1:1)Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.306 s Ran all test suites. (node:29667) Warning: --localstorage-file was provided without a valid path (Use node --trace-warnings ... to show where the warning was created)
My test work fine when i omit prisma. I can see that the problem might have to do with the use of import.meta.url from prisma but I am not sure how to fix this issue.
This is my jest.config.ts and tsconfig.json:
jest.config.ts
import type { Config } from 'jest'; import { createDefaultPreset } from 'ts-jest'; const tsJestTransformCfg = createDefaultPreset().transform; const config: Config = { testEnvironment: "node", transform: { ...tsJestTransformCfg, }, testPathIgnorePatterns: ["/node_modules/", "/dist/"], modulePathIgnorePatterns: ["/dist/"] }; export default config;TSconfig.json
{ "extends": "@tsconfig/node20/tsconfig.json", "compilerOptions": { "outDir": "./dist", "rootDir": "./src", "forceConsistentCasingInFileNames": true, "isolatedModules": true, "module": "esnext", "moduleResolution": "nodenext", "target": "es2023", "esModuleInterop": true, "skipLibCheck": true }, "include": [ "src/**/*" ], "exclude": [ "node_modules", "dist" ] }this is my testing code ( im new to supertest ):
import app from "../app"; import request from "supertest"; const newUser = { userName: "JohanM", email: "[email protected]", password: "Pa$$word#1", passwordConfirmation: "Pas$$word#1" } test("get user", async () => { const res = await request(app) .get("/user") .expect(200); expect(res.status).toBe(200); }); describe("User Route", () => { test("Create user", async () => { const res = request(app) .post("/user") .send(newUser) .expect(201); expect((await res).status).toBe(201); } ) }); test('object assignment', () => { expect(2 + 2).toBe(4); });