Ale*_*edt 5 enums karma-jasmine angular angular6 angular7
我正在将Angular应用程序从v6迁移到v7。除了对枚举进行比较的任何测试以外,其他一切都很好。当我运行测试时,会遇到很多关于枚举的错误,例如
ERROR in src/.../some-thing.component.spec.ts: error TS2345: Argument of type 'PlanDuration.SixMonths' is not assignable to parameter of type 'Expected<PlanDuration.TwelveMonths>'.
Run Code Online (Sandbox Code Playgroud)
运行测试的示例如下所示:
export enum PlanDuration {
SixMonths,
TwelveMonths
}
...
it('should toggle plan duration to six months if the event source id is the toggle duration and the event is not checked', () => {
component.selectedPlanDuration = PlanDuration.TwelveMonths;
component.handleToggle(event);
expect(component.selectedPlanDuration).toBe(PlanDuration.SixMonths); // Tests cannot run because of errors here
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我将枚举值转换为数字,则测试将完美运行!像这样在任何地方更新我的规格都不太理想:
expect(component.selectedPlanDuration).toBe(<number> PlanDuration.SixMonths);
Run Code Online (Sandbox Code Playgroud)
我不确定我是否错过了一些东西package.json
。我已经将一个新的angular 7项目与自己的项目进行了比较,并且它们之间的angular core,打字稿,茉莉花和业力版本相同。
如何获得测试以正确比较枚举?下面是我的package.json
"dependencies": {
"@angular/animations": "~7.2.0",
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/forms": "~7.2.0",
"@angular/http": "~7.2.0",
"@angular/platform-browser": "~7.2.0",
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"rxjs": "~6.4.0",
"tslib": "^1.9.0",
"zone.js": "~0.8.26",
"@angular/cdk": "^7.0.3",
"@angular/flex-layout": "7.0.0-beta.24",
"@angular/material": "7.3.6",
"hammerjs": "2.0.8",
"intl": "1.2.5",
"jshashes": "1.0.7",
"lodash-es": "4.17.11",
"request-promise-native": "1.0.5",
"stream": "0.0.2",
"timers": "0.1.1",
"url-search-params-polyfill": "5.0.0",
"xml2js": "0.4.19"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.0",
"@angular/cli": "~7.3.7",
"@angular/compiler-cli": "~7.2.0",
"@angular/language-service": "~7.2.0",
"@types/node": "~8.9.4",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2",
"@types/lodash-es": "4.17.1",
"gulp": "3.9.1",
"gulp-stylelint": "7.0.0",
"jasmine-data-provider": "2.2.0",
"karma-cli": "1.0.1",
"karma-junit-reporter": "1.2.0",
"karma-parallel": "0.3.0",
"karma-spec-reporter": "0.0.32",
"lodash": "4.17.11",
"moment": "2.22.2",
"npm": "6.0.0",
"protractor-beautiful-reporter": "1.2.5",
"protractor-jasmine2-screenshot-reporter": "0.5.0",
"stylelint": "9.6.0",
"stylelint-order": "1.0.0",
"tslint-jasmine-noSkipOrFocus": "1.0.9"
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"importHelpers": true,
"preserveConstEnums": true,
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.spec.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"module": "commonjs",
"target": "es5",
"baseUrl": "",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
长话短说
快速修复:转到node_modules/@types/jasmine/index.d.ts
,搜索type Expected
// Change this line:
// type Expected<T> = T | ObjectContaining<T> | Any | Spy;
// to:
type Expected<T> = any;
Run Code Online (Sandbox Code Playgroud)
就是这样 :)
欲了解更多详细信息,请阅读:
我相信这是茉莉花类型定义中的一个错误。我设置了一个新的 ng7 工作区并尝试重现您的问题。这是我的发现:
.d.ts
工作区中有两个jasmine相关文件:
// package.json
...
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
Run Code Online (Sandbox Code Playgroud)
我不是 100% 确定这两者如何协同工作,但它们为相同的 jasmine utils 声明了冲突的类型。例如:
// Change this line:
// type Expected<T> = T | ObjectContaining<T> | Any | Spy;
// to:
type Expected<T> = any;
Run Code Online (Sandbox Code Playgroud)
// package.json
...
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
Run Code Online (Sandbox Code Playgroud)
现在问题出在jasmine/index.d.ts
.
这条线toBe(expected: Expected<T>)
根本就是错误的。这是一个测试用例,当然您可以根据any
值进行测试。然而Expected<T>
被声明为某种复杂类型是没有意义的。
修复它的最简单方法是手动更正它。解决方案一开始就已经给出了。干杯。
归档时间: |
|
查看次数: |
520 次 |
最近记录: |