mercredi 30 octobre 2019

Cannot add custom matchers for Jasmine using typescript and angular

I'm trying to add multiple new custom matchers to jasmine and use it, the goal is to create a file that register 2 matchers and all that matchers must be available

so i've got a file named custom-matchers.ts

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

(() => {
  beforeEach(async () => {
    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),
            };
          },
        };
      }
    });
  });
})();

A type definition file named custom-matchers.d.ts (i've tried with a different name than the references file but it's the same):

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

this file is registered in another file named e2e-helper, this file is imported in every test file e2e-helper.ts:

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

When i try to use it in a test file for exemple:

expect(object as any).toBePresent();

I got an error :

Property 'toBePresent' does not exist on type 'Matchers'

Aucun commentaire:

Enregistrer un commentaire