我正在尝试为我的角度应用程序运行单元测试,但是在运行'ng test'时收到以下错误:“错误TS2304:找不到名称。。”运行'ng serve'效果很好。
我已经尝试过本指南,但无济于事https://github.com/angular/angular-cli/wiki/stories-1.0-update#one-tsconfig-per-app。
tsconfig.json文件
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"downlevelIteration": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
}
}
tsconfig.app.json文件
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": [],
"paths": {
"@app/*": ["app/*"],
"@env/*": ["environments/*"]
}
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
tsconfig.spec.json文件
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
我希望看到Karma已加载并运行,显示我的测试通过了,因为它是默认测试。而是弹出错误,提示“在此处找不到名称”
这是我的package.json文件
{
"name": "*****",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"compile:server": "webpack --config webpack.server.config.js --progress --colors",
"serve:ssr": "node dist/server",
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
"build:client-and-server-bundles": "ng build --prod && ng run ******"
},
"private": true,
"dependencies": {
"@angular/animations": "^8.0.1",
"@angular/cdk": "~8.0.1",
"@angular/common": "~8.0.1",
"@angular/compiler": "~8.0.1",
"@angular/core": "~8.0.1",
"@angular/forms": "~8.0.1",
"@angular/material": "^8.0.1",
"@angular/platform-browser": "~8.0.1",
"@angular/platform-browser-dynamic": "~8.0.1",
"@angular/platform-server": "~8.0.1",
"@angular/router": "~8.0.1",
"@nguniversal/express-engine": "^7.1.1",
"@nguniversal/module-map-ngfactory-loader": "7.1.1",
"bloomreach-experience-ng-sdk": "^0.1.1-rc1",
"bootstrap": "^4.3.1",
"core-js": "^3.1.4",
"express": "^4.17.1",
"hammerjs": "^2.0.8",
"jsonpointer": "^4.0.1",
"path-to-regexp": "^3.0.0",
"rxjs": "~6.5.2",
"tslib": "^1.10.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.800.0",
"@angular/cli": "~8.0.3",
"@angular/compiler-cli": "~8.0.1",
"@angular/language-service": "~8.0.1",
"@types/jasmine": "~3.3.13",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.0.12",
"codelyzer": "^5.0.1",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"protractor": "~5.4.0",
"ts-loader": "^6.0.2",
"ts-node": "~8.3.0",
"tslint": "~5.17.0",
"typescript": "~3.4.0",
"webpack-cli": "^3.3.4"
}
}
请确保已将要使用的任何模型或类正确导出为其他文件。
例如这样做
export interface FooModel {
someProp: string;
}
而不是:
interface FooModel {
someProp: string;
}
[为应用程序提供服务时,它不会检查在尚未导出的另一个文件中使用接口的情况。但是,在进行ng测试时,它将进行检查。