1

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 for each test file with a single registration.

so i've got a file named 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),
            };
          },
        };
      }
    });
  });
})();

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'

1 Answer 1

1

I found a solution, i just import my definition file in the helper file below the custom-matchers.ts file and rename it to have a different name import, so i have :

import './custom-matchers';
import './custom-matchers-types'; // definition file
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.