Nathan G Nathan G - 25 days ago 11
TypeScript Question

Angular 2 Karma Unit Tests Cannot Find Name

I have an Angular 2 project set up that I wanted to add unit testing to using Karma, Jasmine, and Webpack. However, after setting things up I have an issue where the tests compile and run successfully, but the console output logs a ton of errors from TypeScript saying it can't find names for certain jasmine and es6 classes as shown below:

enter image description here

I haven't been able to figure out why this is occuring, as all the cases I've come across where people have had similar issues were using < TypeScript 2.0, so the issue was generally something with typings. In my case I'm running TypeScript 2.0.8 and have the typings using the new style via npm @types. Obviously the compiler is somehow correctly finding the types (since the unit tests do run fine after the errors) while still saying they do not exist. Has anyone had this issue before and know a solution?

Answer

Obviously the compiler is somehow correctly finding the types (since the unit tests do run fine after the errors)

That is incorrect. Actually the compiler is not finding them. It works fine because you have jasmine (JS) and es6-shim (JS) added to the karma runtime. It can't find the typings (hence the compile errors) but at runtime the JS files are present in the environment.

Make sure you have

  1. Installed @types/jasmine and @types/core-js
  2. Added the following to your tsconfig.json - compilerOptions

    "typeRoots": [
        "./node_modules/@types/"
    ]
    
    // works for me without it, but maybe also
    "types": [
        "jasmine",
        "core-js"
    ]