【问题标题】:ng test don't find relative pathng 测试找不到相对路径
【发布时间】:2021-04-20 08:43:15
【问题描述】:

我正在尝试在 Angular 中执行我的第一个测试,并且我想测试一个服务,但是当我运行 ng test 时,它在我在 tsconfi.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 testing angular-cli tsconfig angular-devkit


    【解决方案1】:

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

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

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

    tsconfig.app.json 用于正常构建。 tsconfig.spec.json 用于测试构建。 (More information.)

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

    我不能 100% 确定以下几点:

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

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多