如何添加Jasmine自定义匹配器Typescript定义? [英] How do I add a Jasmine custom matcher Typescript definition?

查看:129
本文介绍了如何添加Jasmine自定义匹配器Typescript定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找 周围这个问题似乎是重复的事情。但是,我找到的解决方案似乎都不适合我。

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.

使用以下内容:

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

我无法使用Typescript合并命名空间包含我的自定义匹配器定义的声明。

I can't get Typescript to merge the namespace declaration containing my custom matcher definition.

添加:

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

隐藏先前在<$ c $上声明的所有其他类型C>茉莉。编译器输出错误,例如:

Hides every other type previously declared on jasmine. Compiler outputs errors such as:

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

有没有正确的方法来添加自定义匹配器并使其与Typescript很好地配合?

Is there any proper way to add a custom matcher and make it play nicely with Typescript?

如果你使用的是 tslint 还有一个问题: tslint:推荐规则集。这些规则禁止使用命名空间模块关键字,因此我必须禁用linter(或更改no-namespace rule)以便尝试这一点。不确定如果不推荐,如何扩展定义。

There's an additional issue with tslint if you are using tslint:recommended ruleset. Those rules disallow the usage of namespace or module keywords, so I had to disable the linter (or change the "no-namespace" rule) in order to try this out. Unsure how one would get around extending definitions if this is "not recommended".

推荐答案

添加时我必须修改三个文件自定义匹配器。我创建了一个名为matchers.ts的文件,其中包含实际的匹配器。然后我为matchers.ts文件添加了test.ts的导入。最后,我在typings.d.ts文件中添加了一个包含我的匹配器的接口。

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(任意名称)

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

typings.d.ts

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

这篇关于如何添加Jasmine自定义匹配器Typescript定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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