使用Typescript创建自定义茉莉花匹配器 [英] Create custom jasmine matcher using Typescript

查看:196
本文介绍了使用Typescript创建自定义茉莉花匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在angular2项目上使用jasmine,并且在编写自定义匹配器时遇到一些麻烦。我希望能够比较两个相对复杂的对象。我找到了这篇文章解决问题,但它只是导致一个打字稿错误,说明它无法识别jasmine的 Matchers 对象上的新方法。相关代码如下:

I'm using jasmine on an angular2 project and having some trouble writing a custom matcher for a test. I want to be able to compare two relatively complex objects. I found this article which claims to solve the issue but it simply results in a typescript error stating that it doesn't recognize the new method on jasmine's Matchers object. The relevant code is this:

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;
    }
}

我试过这个

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

并收到此错误:


类型'jasmine.Matchers'不能指定为'jasmine.Matchers'类型。
存在两种具有此名称的不同类型,但它们不相关。

Type 'jasmine.Matchers' is not assignable to type 'jasmine.Matchers'. Two different types with this name exist, but they are unrelated.

这似乎表明 declare namespace jasmine 语句正在创建一个新的 jasmine 命名空间,而不是扩展现有命名空间。

That seems to indicate that the declare namespace jasmine statement is creating a new jasmine namespace rather than extending the existing one.

那么如何创建我自己的打字稿,打字稿会很满意?

So how can I create my own matcher that typescript will be happy with?

推荐答案

Daf的回答主要适用于我我刚刚注意到他的示例代码和他命名文件的方式存在问题。我也遇到了另一个无关的问题。因此,一个新的答案。

Daf's answer mostly worked for me I just noticed an issue with his sample code and the way he named his files. I also happened upon another unrelated issue. Hence a new answer.


  • 由于某些原因,当界面文件与匹配文件同名时,我的应用程序不喜欢它。例如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

Sample custom matcher here: - https://github.com/vespertilian/wallaby-angular-node-yarn-workspaces/tree/master/api/src/test-helpers Sample specs here: - 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');
    })
});

这篇关于使用Typescript创建自定义茉莉花匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆