如何让 Jest 自定义匹配器在打字稿中工作? [英] How to get a Jest custom matcher working in typescript?

查看:20
本文介绍了如何让 Jest 自定义匹配器在打字稿中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常进行单元测试,需要比较两个矩对象.我希望我们使用 moment 的内置函数 moment.isSame(moment) 来比较它们.但是,这意味着我的断言将如下所示:

I regularly have unit tests where I need to compare two moment objects. I'd us moment's built-in function moment.isSame(moment) to compare them. However, this means my assertion will look like this:

expect(moment1.isSame(moment2)).toBeTrue();

我不太喜欢这个,特别是因为失败消息的信息量会较少.因此,我想编写一个自定义的笑话匹配器toBeSameMoment".以下代码似乎至少可以编译:

I didn't quite like this, especially because the failure message will be less informative. Hence, I wanted to write a custom jest matcher "toBeSameMoment". The following code seems to compile at least:

import moment from "moment";

declare global {
  namespace jest {
    interface MomentMatchers extends Matchers<moment.Moment> {
      toBeSameMoment: (expected: moment.Moment) => CustomMatcherResult;
    }
  }
}

expect.extend({
  toBeSameMoment(received: moment.Moment, expected: moment.Moment): jest.CustomMatcherResult {
    const pass: boolean = received.isSame(expected);
    const message: () => string = () => pass ? "" : `Received moment (${received.toISOString()}) is not the same as expected (${expected.toISOString()})`;

    return {
      message,
      pass,
    };
  },
});

但是,我无法真正让它在我的单元测试中工作......当我尝试以下测试代码时:

However, I can't really get it to work in my unit test... When I try the following test code:

import moment from "moment";
import "../jest-matchers/moment";

describe("Moment matcher", () => {

  test("should fail", () => {
    const moment1 = moment.utc();
    const moment2 = moment();

    expect(moment1).toBeSameMoment(moment2);
  });

});

...然后我收到以下错误:

...then I get the following error:

error TS2339: Property 'toBeSameMoment' does not exist on type 'JestMatchersShape<Matchers<void, Moment>, Matchers<Promise<void>, Moment>>'.

不过,我不太明白这个错误.例如,this 所指的 void 类型是什么?我试过在谷歌上搜索它,但并没有真正找到一个好的指南.我确实注意到了这个问题:How to let know关于玩笑自定义匹配器的打字稿编译器?,这似乎基本上是重复的,但显然还不够清楚.

I don't quite get this error, though. For example, what is the void type this is referring to? I've tried googling about it, but didn't really find a good guide or so. I did take notice of this question: How to let know typescript compiler about jest custom matchers?, which seems to basically be a duplicate, but apparently not clear enough, yet.

我的 tsconfig 中包含了玩笑类型

I have jest types included in my tsconfig

推荐答案

您链接到的其他问题和答案是正确的,您还可以在 这个github评论关于react-testing-library.

The other question and answer you linked to were correct, and you can also find a very succinct example for how to extend jest in this github comment on react-testing-library.

要为您的代码实现他们的解决方案,只需更改:

To implement their solution for your code, just change:

declare global {
  namespace jest {
    interface MomentMatchers extends Matchers<moment.Moment> {
      toBeSameMoment: (expected: moment.Moment) => CustomMatcherResult;
    }
  }
}

致:

declare global {
  namespace jest {
    interface Matchers<R> {
      toBeSameMoment(expected: moment.Moment): CustomMatcherResult;
    }
  }
}

这篇关于如何让 Jest 自定义匹配器在打字稿中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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