ng serve 命令导致错误 "Cannot find name 'describe' (or 'beforeEach', 'it', 'expect', etc.)"
ng serve command causes error "Cannot find name 'describe' (or 'beforeEach', 'it', 'expect', etc.)"
我正在尝试 运行 对我们构建的库(这是 Angular 版本 8)进行 Karma/Jasmine 单元测试。单元测试 运行 很好(使用 "ng test"),但是当我尝试编译(ng build)时,出现以下错误:
Cannot find name 'describe'. 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.
我在 'beforeEach'、'it'、'expect' 和 'describe' 中遇到同样的错误。
我试过:
- 正在重新安装所有 node_module 个文件。
- 正在 spec.ts 文件中导入 'jasmine'。
- 按照错误信息的指示安装@types/jest和@types/mocha。
- 将这两个以及一些其他已安装的模块添加到 tsconfig.json 中的 "types" 列表中(请参阅 post 末尾的文件)。
- 正在验证 node_modules@types\jasmine 中的 index.d.ts 文件是否包含我收到错误的方法的定义。
只是基本的、自动生成的测试文件导致编译错误:
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
]
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
});
这是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",
"typeRoots": [
"node_modules/@types"
],
"types": [
"jest",
"node",
"jasmine",
"jasminewd2",
"mocha"
],
"lib": [
"es2018",
"dom"
],
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
仅供参考,我运行正在使用 TypeScript 3.5.3、Karma 4.1.0 和 Jasmine 3.4.0。
重现此错误的一种方法是注释掉 src/tsconfig.app.json
文件中的一行。
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": []
},
"exclude": [
"test.ts",
// "**/*.spec.ts" <-- commenting this out loads your spec, which isn't what you want for npm start
]
}
另一种可能是您错误命名了规范文件,因此此过滤器不会排除它,或者您在实际的 .ts
文件中编写了测试。
我正在尝试 运行 对我们构建的库(这是 Angular 版本 8)进行 Karma/Jasmine 单元测试。单元测试 运行 很好(使用 "ng test"),但是当我尝试编译(ng build)时,出现以下错误:
Cannot find name 'describe'. 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.
我在 'beforeEach'、'it'、'expect' 和 'describe' 中遇到同样的错误。
我试过:
- 正在重新安装所有 node_module 个文件。
- 正在 spec.ts 文件中导入 'jasmine'。
- 按照错误信息的指示安装@types/jest和@types/mocha。
- 将这两个以及一些其他已安装的模块添加到 tsconfig.json 中的 "types" 列表中(请参阅 post 末尾的文件)。
- 正在验证 node_modules@types\jasmine 中的 index.d.ts 文件是否包含我收到错误的方法的定义。
只是基本的、自动生成的测试文件导致编译错误:
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
]
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
});
这是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",
"typeRoots": [
"node_modules/@types"
],
"types": [
"jest",
"node",
"jasmine",
"jasminewd2",
"mocha"
],
"lib": [
"es2018",
"dom"
],
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
仅供参考,我运行正在使用 TypeScript 3.5.3、Karma 4.1.0 和 Jasmine 3.4.0。
重现此错误的一种方法是注释掉 src/tsconfig.app.json
文件中的一行。
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": []
},
"exclude": [
"test.ts",
// "**/*.spec.ts" <-- commenting this out loads your spec, which isn't what you want for npm start
]
}
另一种可能是您错误命名了规范文件,因此此过滤器不会排除它,或者您在实际的 .ts
文件中编写了测试。