Currently, I am developing a Next.js App (v13.2.3) using Typescript and have set up a path alias in the tsconfig.json
. Does anyone know how I can configure the jest environment to recognize this path alias?
// tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
I have carefully followed the steps outlined in adding jest to the project: https://nextjs.org/docs/testing#setting-up-jest-with-the-rust-compiler
Everything seems to be working properly in the ecosystem, except when utilizing the path alias specified in tsconfig.json
, the test fails with the error message: `Cannot find module '@/example/file' from 'src/pages/index.tsx'.
//jest.config.js
const nextJest = require('next/jest');
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
});
// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const customJestConfig = {
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testEnvironment: 'jest-environment-jsdom',
};
// Exporting createJestConfig in this manner ensures that next/jest can access the asynchronous Next.js configuration
module.exports = createJestConfig(customJestConfig);