ng 测试找不到相对路径



我试图在Angular中执行我的第一个测试,我想测试一个服务,但是当我运行ng test时,它在我在tsconf中定义的所有相对导入中给了我一个错误。Json,例如

"@interceptors/*": [
        "./src/app/interceptors/*"
      ],

给我这个错误

ERROR in src/app/app.module.ts:17:44 - error TS2307: Cannot find module '@interceptors/allhttprequests.interceptor' or its corresponding type declarations.

在Angular应用中,通常有多个tsconfig文件:

  • tsconfig.json
  • tsconfig.app.json
  • tsconfig.spec.json

通常,tsconfig.app.jsontsconfig.spec.json文件扩展tsconfig.json

tsconfig.app.json用于正常构建。tsconfig.spec.json用于测试构建。(更多的信息。)

如果正常的ng build没有给出关于您的路径的任何错误,我猜您只将@interceptors/*路径添加到tsconfig.app.json,而不是tsconfig.spec.json

我不是100%确定以下内容:

如果您在tsconfig.json中指定了paths,但在tsconfig.spec.json中也指定了paths,则后者中的paths可能会覆盖tsconfig.json中的paths

最新更新