각도 2 단위 테스트 : 'describe'이름을 찾을 수 없습니다
나는 다음과 같은거야
그들이 말했듯이 단위 테스트를 만들기 위해 hero.spec.ts 파일을 만들었습니다.
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);
});
});
단위 테스트는 매력처럼 작동합니다. 문제는 : 튜토리얼에 언급 된 오류가 있습니다.
우리의 편집기와 컴파일러는 무엇을 모르는 불평 수및
it
그들이 재스민을 설명하는 입력 파일이 부족하기 때문이다. 우리는 그 성가신 불만이 무해하기 때문에 지금은 무시할 수 있습니다.
expect
그리고 그들은 실제로 그것을 무시했습니다. 이러한 오류는 무해하지만 출력 오류가 발생하면 출력 콘솔에서 좋지 않습니다.내가 얻는 것의 예 :
'설명'이름을 찾을 수 없습니다.'it'이름을 찾을 수 없습니다.'expect'이름을 찾을 수 없습니다.
이 문제를 해결하려면 어떻게해야합니까?
당신이 설치되기를 바랍니다-
npm install --save-dev @types/jasmine
그런 다음
hero.spec.ts
파일 맨 위에 다음 가져 오기를 넣으십시오.
import 'jasmine';
문제를 해결해야합니다.
Typescript@2.0 이상에서는 다음을 사용하여 유형을 설치할 수 있습니다.
npm install -D @types/jasmine
그런 다음
types
옵션을 사용하여 유형을 자동으로 가져옵니다
tsconfig.json
.
"types": ["jasmine"],
이 솔루션은
import {} from 'jasmine';
각 스펙 파일에 필요하지 않습니다 .
npm install @types/jasmine
일부 의견에서 언급했듯이
"types": ["jasmine"]
더 이상 필요하지 않으며 모든
@types
패키지가 자동으로 컴파일에 포함됩니다 (v2.1부터 생각합니다).내 생각에 가장 쉬운 해결책은
tsconfig.json
에서 테스트 파일을 제외하는 것입니다.
"exclude": [
"node_modules",
"**/*.spec.ts"
]
이것은 나를 위해 작동합니다.공식
에 대한 추가 정보 .
jasmine에 타이핑을 설치해야합니다. 비교적 최신 버전의 typescript 2에 있다고 가정하면 다음을 수행 할 수 있습니다.
npm install --save-dev @types/jasmine
Typescript@2.0 이상을 사용하면 npm install로 유형을 설치할 수 있습니다
npm install --save-dev @types/jasmine
그런 다음 tsconfig.json 의
typeRoots
옵션을 사용하여 유형을 자동으로 가져 오십시오 .
"typeRoots": [
"node_modules/@types"
],
이 솔루션에는 'jasmine'에서 가져 오기 {}가 필요하지 않습니다. 각 사양 파일에서.
이 문제 에 대한 해결책
그의 답변에 쓴 것과 관련이 있습니다. 그러나 모든 것을 설명하지는 않으므로 마음에 들지 않으면 직접 작성하겠습니다.
해결책:
이 줄을 추가 :
///<reference path="./../../../typings/globals/jasmine/index.d.ts"/>
hero.spec.ts
파일 의 시작 부분에 문제가 해결되었습니다. 경로는
typings
모든 입력 내용이 저장된 폴더로 연결됩니다 .입력을 설치하려면
typings.json
프로젝트의 루트에 다음 내용으로 파일 을 작성해야 합니다.
{
"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"
}
}
그리고 NPM 패키지가
typings install
있는 곳을 실행하십시오
typings
.
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.
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.
Only had to do the following to pick up @types in a Lerna Mono-repo where several node_modules exist.
npm install -D @types/jasmine
Then in each tsconfig.file of each module or app
"typeRoots": [
"node_modules/@types",
"../../node_modules/@types" <-- I added this line
],
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).
In order for TypeScript Compiler to use all visible Type Definitions during compilation, types
option should be removed completely from compilerOptions
field in tsconfig.json
file.
This problem arises when there exists some types
entries in compilerOptions
field, where at the same time jest
entry is missing.
So in order to fix the problem, compilerOptions
field in your tscongfig.json
should either include jest
in types
area or get rid of types
comnpletely:
{
"compilerOptions": {
"esModuleInterop": true,
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"types": ["reflect-metadata", "jest"], //<-- add jest or remove completely
"moduleResolution": "node",
"sourceMap": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
I'll just add Answer for what works for me in "typescript": "3.2.4" I realized that jasmine in node_modules/@types there is a folder for ts3.1 under the jasmine type so here are the steps:-
- Install type jasmine
npm install -D @types/jasmine
-
Add to tsconfig.json jasmine/ts3.1
"typeRoots": [ ... "./node_modules/jasmine/ts3.1" ],
-
Add Jasmine to the types
"types": [ "jasmine", "node" ],
Note: No need for this import 'jasmine';
anymore.
I'm on Angular 6, Typescript 2.7, and I'm using Jest framework to unit test. I had @types/jest
installed and added on typeRoots
inside tsconfig.json
But still have the display error below (i.e: on terminal there is no errors)
cannot find name describe
And adding the import :
import {} from 'jest'; // in my case or jasmine if you're using jasmine
doesn't technically do anything, so I thought, that there is an import somewhere causing this problem, then I found, that if delete the file
tsconfig.spec.json
in the src/
folder, solved the problem for me. As @types is imported before inside the rootTypes.
I recommend you to do same and delete this file, no needed config is inside. (ps: if you're in the same case as I am)
In my case, I was getting this error when I serve the app, not when testing. I didn't realise I had a different configuration setting in my tsconfig.app.json file.
I previously had this:
{
...
"include": [
"src/**/*.ts"
]
}
It was including all my .spec.ts
files when serving the app. I changed the include property to
exclude` and added a regex to exclude all test files like this:
{
...
"exclude": [
"**/*.spec.ts",
"**/__mocks__"
]
}
Now it works as expected.
참고URL : https://stackoverflow.com/questions/39020022/angular-2-unit-tests-cannot-find-name-describe
'programing' 카테고리의 다른 글
.NET 어셈블리 란 무엇입니까? (0) | 2020.05.12 |
---|---|
프로세스가 끝날 때까지 기다리십시오 (0) | 2020.05.12 |
DOM 요소를 첫 번째 자식으로 설정하는 방법은 무엇입니까? (0) | 2020.05.12 |
PowerShell에서 명령 실행 타이밍 (0) | 2020.05.12 |
C ++ 문자열에서 마지막 문자 제거 (0) | 2020.05.12 |