Search code examples
angulartestingangular-clitsconfigangular-devkit

ng test don't find relative path


I am trying to perform my first tests in Angular and I want to test a service, but when I run ng test it gives me an error in all the relative imports that I have defined in the tsconfi.json, such as for example

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

give me this error

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

Solution

  • In an Angular application, there are typically multiple tsconfig files:

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

    Typically, the tsconfig.app.json and tsconfig.spec.json files extend tsconfig.json.

    tsconfig.app.json is used for the normal build. tsconfig.spec.json is used for the test build. (More information.)

    If the normal ng build doesn't give any errors about your paths, I would guess you have only added the @interceptors/* path to the tsconfig.app.json, but not the tsconfig.spec.json.

    I'm not 100% sure about the following:

    If you specified the paths in the tsconfig.json, but also have paths in tsconfig.spec.json, it could be that the paths in the latter one overwrites the paths in the tsconfig.json.