直接访问Jest相等性测试

问题描述 投票:0回答:1

使用笑话,您可以轻松地对两个对象进行深度比较:

expect(requestBody).toEqual(expectedRequest);

有没有办法以编程方式使用同一个相等性测试而不会触发断言?有点像

const matches = isEqual(requestBody, expectedRequest);

这对于在单元测试中编写谓词非常有用。例如。使Angulars HttpTestingController监视特定的请求正文,只需编写就可以了

  httpController.expectOne(request => isEqual(request.body, expectedRequest));
jestjs
1个回答
0
投票

Jest匹配器功能未公开。如果需要进行深度相等性检查,可以直接使用Lodash / UnderscoreisEqualwhich is used by Jest

有可能捕获断言错误:

isEqual = (a, b) => {
  try {
    expect(a).toEqual(b);
    return true;
  } catch (err) {
    if (e.matcherResult)
      return false;

    throw err;
  }
};

通过定义有权访问HttpTestingControllercustom matcher,可以将玩笑与equals集成。

© www.soinside.com 2019 - 2024. All rights reserved.