【问题标题】:spyOn(document, 'getElementById') throws error after upgrading to angular 8/"@types/jasmine": "^3.4.0"spyOn(document, 'getElementById') 升级到 angular 8/"@types/jasmine": "^3.4.0" 后抛出错误
【发布时间】:2020-06-22 11:54:22
【问题描述】:

以下代码升级到“@types/jasmine”后抛出错误:“^3.4.0”

spyOn(document, 'getElementById').and.callFake(function() {
      return {
        value: 'test'
      };
    });

错误是:

Argument of type '() => { value: string; }' is not assignable to parameter of type '(elementId: string) => HTMLElement'.
  Type '{ value: string; }' is missing the following properties from type 'HTMLElement': accessKey, accessKeyLabel, autocapitalize, dir, and 233 more.

【问题讨论】:

    标签: angular typescript jasmine karma-jasmine angular-upgrade


    【解决方案1】:

    尝试强制转换以解决问题。

    spyOn(document, 'getElementById').and.callFake(function(elementId: string) {
          return {
            value: 'test'
          } as HTMLElement;
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2019-11-07
      • 2020-05-25
      相关资源
      最近更新 更多