简体   繁体   English

Angular jest 更新到 27 后无法理解导入路径

[英]Angular jest doesn't understand import path after update to 27

This is a weird error.这是一个奇怪的错误。 So I updated jest to 27 and all stopped working所以我将 jest 更新为 27 并且全部停止工作

It seems to have issues with import paths.导入路径似乎有问题。 So the following所以下面

import { something } form 'src/app/components/.....';

doesn't work, but this does:不起作用,但这确实有效:

import { something } from '../../components/....';

I guess this is managed in tsconfig.我想这是在 tsconfig 中管理的。 This is my spec tsconfig:这是我的spec tsconfig:

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
        "jest", // 1
        "node",
        "Chrome"
    ],
    "esModuleInterop": true, // 2
    "emitDecoratorMetadata": true, // 3
    "allowSyntheticDefaultImports": true,
  },
  "files": ["src/test.ts", "src/polyfills.ts"],
  "include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}

and the main tsconfig:和主要的tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": ["es2018", "dom"],
    "types": [],
    "paths": {
      "@shared/*": ["src/shared/*"]
    },
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "noEmitHelpers": false
  },
  "files": ["src/main.ts", "src/polyfills.ts"],
  "angularCompilerOptions": {
    "strictTemplates": true,
    "fullTemplateTypeCheck": true
  }
}

Any suggestions what might cause this import issue任何可能导致此导入问题的建议

Adding this option in jest.config.js should resolve this issuejest.config.js中添加这个选项应该可以解决这个问题

moduleNameMapper: {
    '^src/(.*)$': '<rootDir>/src/$1',
    '^app/(.*)$': '<rootDir>/src/app/$1',
    '^assets/(.*)$': '<rootDir>/src/assets/$1',
    '^environments/(.*)$': '<rootDir>/src/environments/$1',
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM