如何添加 Jasmine 自定义匹配器 Typescript 定义?

How do I add a Jasmine custom matcher Typescript definition?(如何添加 Jasmine 自定义匹配器 Typescript 定义?)
本文介绍了如何添加 Jasmine 自定义匹配器 Typescript 定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 寻找 around 这个问题似乎是一个 重复发生的事情.但是,我发现的解决方案似乎都不适合我.

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

隐藏之前在 jasmine 上声明的所有其他类型.编译器输出错误如:

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:recommended 规则集,则 tslint 会出现其他问题.这些规则不允许使用 namespacemodule 关键字,所以我不得不禁用 linter(或更改 "no-namespace" 规则)为了试试这个.如果不推荐",不确定如何绕过扩展定义.

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 定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)