Angular 2 Unit Tests: Cannot find name 'describe'

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I'm following this tutorial from angular.io

As they said, I've created hero.spec.ts file to create unit tests:

import { Hero } from './hero'; describe('Hero', () => {   it('has name', () => {     let hero: Hero = {id: 1, name: 'Super Cat'};     expect(hero.name).toEqual('Super Cat');   });   it('has id', () => {     let hero: Hero = {id: 1, name: 'Super Cat'};     expect(hero.id).toEqual(1);   }); }); 

Unit Tests work like a charm. The problem is: I see some errors, which are mentioned in tutorial:

Our editor and the compiler may complain that they don’t know what it and expect are because they lack the typing files that describe Jasmine. We can ignore those annoying complaints for now as they are harmless.

And they indeed ignored it. Even though those errors are harmless, it doesn't look good in my output console when I receive bunch of them.

Example of what I get:

Cannot find name 'describe'.

Cannot find name 'it'.

Cannot find name 'expect'.

What can I do to fix it?

回答1:

I hope you've installed -

npm install --save-dev @types/jasmine

Then put following import at the top of the hero.spec.ts file -

import {} from 'jasmine';

It should solve the problem.



回答2:

With Typescript@2.0 or later you can install types with npm install @types/jasmine, then import the types automatically using the types option in tsconfig.json.

"types": ["jasmine"], 

This solution does not require import {} from 'jasmine'; in each spec file.



回答3:

npm install @types/jasmine 

As mentioned in some comments the "types": ["jasmine"] is not needed anymore, all @types packages are automatically included in compilation (since v2.1 I think).

In my opinion the easiest solution is to exclude the test files in your tsconfig.json like:

"exclude": [     "node_modules",     "**/*.spec.ts" ] 

This works for me.

Further information in the official tsconfig docs.



回答4:

You need to install typings for jasmine. Assuming you are on a relatively recent version of typescript 2 you should be able to do:

npm install --save-dev @types/jasmine



回答5:

Solution to this problem is connected with what @Pace has written in his answer. However, it doesn't explain everything so, if you don't mind, I'll write it by myself.

SOLUTION:

Adding this line:

///

at the beginning of hero.spec.ts file fixes problem. Path leads to typings folder (where all typings are stored).

To install typings you need to create typings.json file in root of your project with following content:

{   "globalDependencies": {     "core-js": "registry:dt/core-js#0.0.0+20160602141332",     "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",     "node": "registry:dt/node#6.0.0+20160807145350"   } } 

And run typings install (where typings is NPM package).



回答6:

With Typescript@2.0 or later you can install types with npm install

npm install --save-dev @types/jasmine 

then import the types automatically using the typeRoots option in tsconfig.json.

"typeRoots": [       "node_modules/@types"     ], 

This solution does not require import {} from 'jasmine'; in each spec file.



回答7:

In my case, the solution was to remove the typeRoots in my tsconfig.json.

As you can read in the TypeScript doc

If typeRoots is specified, only packages under typeRoots will be included.



回答8:

I'm up to the latest as of today and found the best way to resolve this is to do nothing...no typeRoots no types no exclude no include all the defaults seem to be working just fine. Actually it didn't work right for me until I removed them all. I had: "exclude": [ "node_modules" ] but that's in the defaults so I removed that.

I had: "types": [ "node" ] to get past some compiler warning. But now I removed that too.

The warning that shouldn't be is: error TS2304: Cannot find name 'AsyncIterable'. from node_modules\@types\graphql\subscription\subscribe.d.ts

which is very obnoxious so I did this in tsconfig so that it loads it: "compilerOptions": { "target": "esnext", } since it's in the esnext set. I'm not using it directly so no worries here about compatibility just yet. Hope that doesn't burn me later.



回答9:

Look at the import maybe you have a cycle dependency, this was in my case the error, using import {} from 'jasmine'; will fix the errors in the console and make the code compilable but not removes the root of devil (in my case the cycle dependency).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!