使用Typescript创建自定义茉莉花匹配器

d51*_*512 11 jasmine typescript jasmine-matchers

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

declare module jasmine {
    interface Matchers {
        toBeNumeric(): void;
    }
}
Run Code Online (Sandbox Code Playgroud)

另一篇文章给出了一个类似但略有不同的解决方案,它给出了同样的错

declare namespace jasmine {
    interface Matchers {
        toHaveText(expected: string): boolean;
    }
}
Run Code Online (Sandbox Code Playgroud)

我试过这个

let m: jasmine.Matchers = expect(someSpy.someMethod).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)

并得到此错误:

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

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

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

Cam*_*ron 6

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 /袋鼠角节点纱线工作区/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`
                    }
                }

            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

介面档案-matcher-types.d.ts-不能与您的配对档案同名

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

自定义匹配器测试

describe('Hello', () => {

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

    it('should allow custom matchers', () => {
        expect('foo').toReallyEqual('foo');
        expect('bar').not.toReallyEqual('test');
    })
});
Run Code Online (Sandbox Code Playgroud)


daf*_*daf 5

基本上,您的第二个示例(“声明名称空间”)是可行的方法,当然,匹配器的逻辑在其他地方。

欢迎您查看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 文件。