我正在开发一个使用Typescript的Next.js应用程序(v13.2.3)。我在tsconfig.json
中配置了一个路径别名。有什么想法可以让jest环境知道路径别名吗?
// 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"]
}
我完全按照以下说明添加jest到项目中:https://nextjs.org/docs/testing#setting-up-jest-with-the-rust-compiler
生态系统运行正常,除了我使用tsconfig.json
中定义的路径别名的任何文件外,测试将失败,并显示警告:'无法从“src/pages/index. tsx”中找到模块“@/example/file”。
//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',
};
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);
1条答案
按热度按时间1tu0hz3e1#
通过将
jest.config.js
更新为以下内容进行修复: