How do I add a Jasmine custom matcher Typescript definition?

后端 未结 1 421
情深已故
情深已故 2020-12-20 13:57

I\'ve been looking around and this question seems like a recurring thing. However, none of the solutions I\'ve found seem to work for me.

Using the following:

<
相关标签:
1条回答
  • 2020-12-20 14:34

    I had to modify three files when I added custom matchers. I created a file called matchers.ts that contained the actual matchers. I then added an import to test.ts for my matchers.ts file. Finally, I added an interface to the typings.d.ts file which contains my matcher.

    matchers.ts (arbitrary name)

    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;
      }
    }
    
    0 讨论(0)
提交回复
热议问题