I am trying to setup Jest for testing in my NextJS application on a monorepo. I followed the guide on the official NextJS doc that uses the Rest Compiler, then I configured babel, but for some reason when I try to run a pnpm test
I get an issues stating that "Jest encountered an unexpected token".
Also in the error message details it shows that Jest is looking at the node_modules
folder of the parent directory and not the web
node_modules of the application where my jest.config.ts
file is located.
Screenshot of project directories
I have tried to set my jest.config.ts
file to explicitly state the location of my modulePaths
& moduleDirectories
but that doesn't seem to help. Can anyone give me some advice.
Here is my jest.config.ts
file.
const nextJest = require("next/jest");
// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir: "./" });
// Any custom config you want to pass to Jest
const customJestConfig = {
setupFilesAfterEnv: ["<rootDir>/src/test/jest.setup.js"],
testEnvironment: "jest-environment-jsdom",
moduleDirectories: ["<rootDir>/node_modules"],
modulePaths: ["<rootDir>/"],
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
"^.+\\.(js|jsx)$": "babel-jest",
},
};
// createJestConfig is exported in this way to ensure that next/jest can load the Next.js configuration, which is async
module.exports = createJestConfig(customJestConfig);
Spec
NextJS: 13.4.1
Turbo Repo