在从6到7的角度迁移之后,无法比较茉莉花中的枚举

问题描述 投票:4回答:3

我正在将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>'.

正在运行的测试示例如下所示:

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
    });

但是,如果我将我的枚举转换为数字,我的测试工作完美!对于像这样更新我的规格来说,这将是不太理想的:

expect(component.selectedPlanDuration).toBe(<number> PlanDuration.SixMonths);

我不确定我是否错过了我的package.json。我将一个新的角度7项目与我自己的项目进行了比较,它们之间的角度核心,打字稿,茉莉和业力的版本是相同的。

如何让我的测试正确地比较枚举?下面是我的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"
  }

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"
    ]
  }
}

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"
  ]
}
angular enums angular6 karma-jasmine angular7
3个回答
0
投票

我在一段时间后遇到了问题,Angular告诉我它无法访问MyEnumValue of undefined。经过一番摆弄后,我发现将所有枚举导出为const,并将"preserveConstEnums": true添加到我的tsconfig.json中,使其工作正常。

但是枚举总是数字,除非另有说明,谢天谢地,不需要强制转换,但是Typescript的枚举汇编有时可能很时髦,就像界面很时髦一样。

编辑:

在您的组件中,请确保:

// If you give this a default value, TypeScript will assume
// that the only "valid" type is PlanDuration.TwelveMonths
// Type evaluates to: PlanDuration | number between 0 and 1;
selectedPlanDuration: PlanDuration = PlanDuration.TwelveMonths;

// Type evaluates to: PlanDuration.TwelveMonths | 1;
selectedPlanDuration = PlanDuration.TwelveMonths

0
投票

TL; DR

快速修复:去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;

而已 :)


有关详细信息,请继续阅读:

我相信这是茉莉花类型定义中的一个错误。我设置了一个新的ng7工作区,并尝试重现您的问题。这是我的发现:

工作区中有两个与jasmine相关的.d.ts文件:

// package.json
  ...
  "@types/jasmine": "~2.8.8",
  "@types/jasminewd2": "~2.0.3",

我不是100%确定这两个如何一起工作,但是他们正在声明相同的jasmine utils的冲突类型。例如:

// jasminewd2/index.d.ts
declare namespace jasmine {
  interface Matchers<T> {
    toBe(expected: any, expectationFailOutput?: any): Promise<void>;
    ...
// jasmine/index.d.ts
declare namespace jasmine {
  interface Matchers<T> {
    toBe(expected: Expected<T>, expectationFailOutput?: any): boolean;

现在问题出在jasmine/index.d.ts

这条线toBe(expected: Expected<T>)简直是错误的。这是一个测试用例,当然你可以测试any值。然而Expected<T>被宣布为一些复杂的无关点。

解决它的最简单方法是手动纠正它。解决方案已在开始时给出。干杯。


-1
投票

我在Angular 7项目中实现新测试时遇到了同样的问题(没有从6到7的迁移)。如果您希望解决方法而不是进行可能影响Karma / Jasmine未来更新的更改,您可以交换比较,以便枚举是第一个:

expect(PlanDuration.SixMonths).toBe(component.selectedPlanDuration);
© www.soinside.com 2019 - 2024. All rights reserved.