How to add a Jasmine Typescript custom match definition?

I was looked around , and this question looks like a repeating thing . However, none of the solutions I found seem to work for me.

Using the following:

{
  "typescript": "2.3.2",
  "jasmine-core": "2.6.1",
  "@types/jasmine": "2.5.47"
}

I cannot get Typescript to merge the namespace declaration containing my custom mate definition.

Adding this:

declare namespace jasmine {
  interface Matchers<T> {
    toBeAnyOf(expected: jasmine.Expected<T>, expectationFailOutput?: any): boolean;
  }
}

Hides all other types previously declared on jasmine. The compiler displays errors such as:

[ts] Namespace 'jasmine' has no exported member 'CustomMatcherFactories'
[ts] Namespace 'jasmine' has no exported member 'CustomMatcher'.

Is there any suitable way to add custom matches and make it beautiful with Typescript?

tslint:recommended, tslint. namespace module, linter ( "no-namespace"), . , , " ".

+6
1

, . matchers.ts, . test.ts matchers.ts. , typings.d.ts, .

matchers.ts( )

beforeEach(() => {
    jasmine.addMatchers({
        toContainText: () => {
            return {
                compare: (actual: HTMLElement, expectedText: string, customMessage?: string) => {
                    const actualText = actual.textContent;
                    return {
                        pass: actualText.indexOf(expectedText) > -1,
                        get message() {
                            let failureMessage = 'Expected ' + actualText + ' to contain ' + expectedText;

                            if (customMessage) {
                                failureMessage = ' ' + customMessage;
                            }

                            return failureMessage;
                        }
                    };
                }
            };
        },
    });
});

test.ts

import 'test/test-helpers/global/matchers'; (my relative filepath)

typings.d.ts

declare module jasmine {
  interface Matchers {
    toContainText(text: string, message?: string): boolean;
  }
}
+5

Source: https://habr.com/ru/post/1016995/


All Articles