Create custom jasmine spam using Typescript

I use jasmine in an angular2 project and have some problems writing a custom match for the test. I want to be able to compare two relatively complex objects. I found this article , which claims to solve the problem, but simply leads to a typescript error stating that it does not recognize the new method on the jasmine object Matchers. Relevant Code:

declare module jasmine {
    interface Matchers {
        toBeNumeric(): void;
    }
}

Another article gives a similar but slightly different solution that gives the same error.

declare namespace jasmine {
    interface Matchers {
        toHaveText(expected: string): boolean;
    }
}

I tried this

let m: jasmine.Matchers = expect(someSpy.someMethod).toHaveBeenCalled();

and got this error:

Enter "jasmine.Matchers" is not assigned to the type "jasmine.Matchers". There are two different types with this name, but they are not related.

, declare namespace jasmine jasmine .

, , typescript ?

+2
2

, ( " " ) - , , .

https://github.com/fluffynuts/polymer-ts-scratch/tree/5eb799f7c8d144dd8239ab2d2bcc72821327cb24/src/specs/test-utils/jasmine-matchers, Jasmine , - - Javascript .ts, .

@types/jasmine - .

, @types/jasmine ; , , Jasmine Matchers, (.. Matchers<T>), .d.ts .

+2

Daf . , . . .

  • - , , -. foo.ts foo.d.ts. foo.ts foo-interface.d.ts - .
  • foo.ts foo-interface.d.ts, .

: - https://github.com/vespertilian/wallaby-angular-node-yarn-workspaces/tree/master/api/src/test-helpers : - https://github.com/vespertilian/wallaby-angular-node-yarn-workspaces/blob/master/api/src/hello/hello.spec.ts

- custom-matchers.ts

import MatchersUtil = jasmine.MatchersUtil;
import CustomMatcherFactories = jasmine.CustomMatcherFactories;
import CustomEqualityTester = jasmine.CustomEqualityTester;
import CustomMatcher = jasmine.CustomMatcher;
import CustomMatcherResult = jasmine.CustomMatcherResult;

export const SomeCustomMatchers: CustomMatcherFactories = {
    toReallyEqual: function (util: MatchersUtil, customEqualityTester: CustomEqualityTester[]): CustomMatcher {
        return {
            compare: function (actual: any, expected: any): CustomMatcherResult {
                if(actual === expected) {
                    return {
                        pass: true,
                        message: `Actual equals expected`
                    }
                } else {
                    return {
                        pass: false,
                        message: `Actual does not equal expected`
                    }
                }

            }
        }
    }
};

- matcher-types.d.ts - , -

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

describe('Hello', () => {

    beforeEach(() => {
        jasmine.addMatchers(SomeCustomMatchers)
    });

    it('should allow custom matchers', () => {
        expect('foo').toReallyEqual('foo');
        expect('bar').not.toReallyEqual('test');
    })
});
0

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


All Articles