Search code examples
angularjestjstsconfig

Angular ng test cause error if I have types "node" in compiler option


env

  • Angular 9
  • node v10.16.0
  • npm:6.9.0

with angular application, ng test fails if I added types in compileroption in tsconfig.json.

errors are like

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha` and then add `jest` or `mocha` to the types field in your tsconfig.
S2304: Cannot find name 'expect'

my tsconfig is as below.

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "types": ["node"],  // <= this part
  }
}

I know if I remove that types, then it works. But actually because of package which I use, I need to have it.

I hope someone has any knowledge about such behavior.


Solution

  • In official google documentation there is no types option inside angularCompilerOptions property : exhaustive list of available options :

    1. allowEmptyCodegenFiles
    2. annotationsAs
    3. annotateForClosureCompiler
    4. disableExpressionLowering
    5. disableTypeScriptVersionCheck
    6. enableIvy
    7. enableResourceInlining
    8. enableLegacyTemplate
    9. flatModuleId
    10. flatModuleOutFile
    11. fullTemplateTypeCheck
    12. generateCodeForLibraries
    13. preserveWhitespaces
    14. skipMetadataEmit
    15. skipTemplateCodegen
    16. strictMetadataEmit
    17. strictInjectionParameters
    18. strictTemplates
    19. trace

    if u need node types you can just use npm i @types/node to install them and the types are added under this hierarchy :

    {
       "compilerOptions": {
           "types" : ["node", "lodash", "express"]
       }
    }