Ryo
Ryo

Reputation: 595

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

env

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.

Upvotes: 0

Views: 702

Answers (2)

Ryo
Ryo

Reputation: 595

Answer myself

remove types from tsconfig.json and add it in tsconfig.app.json

tsconfig.json

   // "types": ["node"], // remove

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["node"] // <= add
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

Upvotes: 1

yanesof__
yanesof__

Reputation: 144

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"]
   }
}
 

Upvotes: 0

Related Questions