路由别名不能开玩笑,将 Next.js 与 Typescript 一起使用

问题描述 投票:0回答:0

我正在开发一个使用 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"]
}

我完全按照这些说明将笑话添加到项目中: https://nextjs.org/docs/testing#setting-up-jest-with-the-rust-compiler

生态系统运行正常,除了我使用

tsconfig.json
中定义的路径别名的任何文件外,测试将失败并显示警告:`Cannot find module '@/example/file' from 'src/pages/index.html' 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',
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);
typescript jestjs alias next
© www.soinside.com 2019 - 2024. All rights reserved.