【问题标题】:Cannot add custom matchers for Jasmine using typescript and angular无法使用打字稿和角度为 Jasmine 添加自定义匹配器
【发布时间】:2019-10-30 17:02:28
【问题描述】:

我正在尝试向 jasmine 添加多个新的自定义匹配器并使用它,目标是创建一个注册 2 个匹配器的文件,并且所有匹配器必须可用于单个注册的每个测试文件。

所以我有一个名为custom-matchers.ts的文件

/// <reference path="custom-matchers.d.ts"/>

(() => {
  beforeEach(() => {
    jasmine.addMatchers({
      toHaveText: () => {
        return {
          compare: (actual, expected) => {
            return {
              pass: actual.getText().then(pass => pass === expected)
            };
          },
        };
      },
      toBePresent: () => {
        return {
          compare: (actual, expected) => {
            return {
              pass: actual.isPresent().then(pass => pass === expected),
            };
          },
        };
      }
    });
  });
})();

一个名为 custom-matchers.d.ts 的类型定义文件(我尝试使用与引用文件不同的名称,但它是相同的):

declare namespace jasmine {
  interface Matchers<T> {
    toHaveText(text: any): boolean;
    toBePresent(text: any): boolean;
  }
}

这个文件注册在另一个名为 e2e-helper 的文件中,这个文件被导入到每个测试文件e2e-helper.ts

/// <reference path="custom-matchers.d.ts"/>
import './custom-matchers';

当我尝试在测试文件中使用它时:

expect(object as any).toBePresent();

我遇到了一个错误:

“匹配器”类型上不存在属性“toBePresent”

【问题讨论】:

    标签: angular typescript testing jasmine protractor


    【解决方案1】:

    我找到了一个解决方案,我只是将我的定义文件导入到 custom-matchers.ts 文件下方的帮助文件中,并将其重命名为具有不同的名称导入,所以我有:

    import './custom-matchers';
    import './custom-matchers-types'; // definition file
    

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 2016-03-23
      • 2012-02-15
      • 2020-03-18
      • 1970-01-01
      相关资源
      最近更新 更多