Angular CLI 6中ng test执行失败,提示缺少tsConfig属性
Angular 6 ng test 报错:Schema validation failed (缺失'tsConfig'属性)
你升级到Angular 6后,ng build和ng serve都正常运行,但执行ng test时遇到了如下报错:
Schema validation failed with the following errors: Data path "" should have required property 'tsConfig'.
你的根目录tsconfig.json配置如下:
{ "compilerOptions": { "baseUrl": "", "declaration": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, "lib": [ "es2016", "dom" ], "mapRoot": "./", "module": "es2015", "moduleResolution": "node", "outDir": "../dist/out-tsc", "sourceMap": true, "target": "es5", "typeRoots": [ "../node_modules/@types" ] } }
问题原因
Angular 6 对 CLI 配置文件(angular.json,替代了旧版的.angular-cli.json)的校验规则变得更严格了。在测试构建配置中,必须显式指定tsConfig的路径——之前的Angular版本可能允许默认值,但Angular 6要求这个配置项不能为空。
你的根目录tsconfig.json是项目主编译配置,但测试环境通常需要单独的tsconfig.spec.json(默认在src/目录下),而你的angular.json里的测试配置块大概率遗漏了这个tsConfig字段。
解决方法
- 打开项目根目录的
angular.json文件 - 找到
projects-> 你的项目名称 ->architect->test配置块 - 在
options里添加或确保存在tsConfig字段,指向你的测试用TypeScript配置文件(通常是src/tsconfig.spec.json)
示例配置如下:
"test": { "builder": "@angular-devkit/build-angular:karma", "options": { "tsConfig": "src/tsconfig.spec.json", "polyfills": "src/polyfills.ts", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.css" ], "scripts": [] } }
如果你的项目没有tsconfig.spec.json,可以临时指向根目录的tsconfig.json(不推荐长期使用),或者创建一个专门的测试配置文件:
// src/tsconfig.spec.json { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/spec", "types": [ "jasmine", "node" ] }, "files": [ "test.ts", "polyfills.ts" ], "include": [ "**/*.spec.ts", "**/*.d.ts" ] }
内容的提问来源于stack exchange,提问作者user3594184
相关产品推荐
相关产品推荐

