忽略空格的字符串的 Jest 相等匹配器

Jest Equality Matcher For Strings That Disregards Whitespace

Jest 的 toEqual 匹配器在检查相等性时会考虑空格。在测试中格式化预期值时,不可能以匹配包含换行符、制表符等的字符串的方式进行格式化。

Jest 是否提供了一种在匹配时忽略空格的方法?

注意:我编辑了问题以使其更通用。

据我所知,开箱即用的 Jest 无法实现这一点。

但是,write your own reusable matcher using expect.extend 非常简单。从两个字符串中删除所有空格,例如通过 str.replace(/\s/g, ''),并比较字符串。

正如@Timo 所说,执行此操作的唯一方法似乎是使用自定义匹配器。这是一个基于 Jest 的 toEqual 匹配器将所有 whitespace 压缩为单个 space 以提高可读性的方法。它将处理制表符、换行符等。它将为您提供漂亮的输出,例如随附的 Jest 匹配器:

//matchStringIgnoringWhiteSpace.js

import { replace, map, equals } from 'ramda';
import { matcherHint, printReceived, printExpected } from 'jest-matcher-utils';
import diff from 'jest-diff';

const replaceWhitespace = replace(/\s+/g, ` `);
const compressWhitespace = map(replaceWhitespace);

const name = `toEqualWithCompressedWhitespace`;

export default function (received, expected) {
  const [
    receivedWithCompresssedWhitespace,
    expectedWithCompresssedWhitespace,
  ] = compressWhitespace([received, expected]);
  const pass = equals(
    receivedWithCompresssedWhitespace,
    expectedWithCompresssedWhitespace
  );
  const message = pass
    ? () =>
        `${matcherHint(`.not.${name}`)}\n\n` +
        `Uncompressed expected value:\n` +
        `  ${printExpected(expected)}\n` +
        `Expected value with compressed whitespace to not equal:\n` +
        `  ${printExpected(expectedWithCompresssedWhitespace)}\n` +
        `Uncompressed received value:\n` +
        `  ${printReceived(received)}\n` +
        `Received value with compressed whitespace:\n` +
        `  ${printReceived(receivedWithCompresssedWhitespace)}`
    : () => {
        const diffString = diff(
          expectedWithCompresssedWhitespace,
          receivedWithCompresssedWhitespace,
          {
            expand: this.expand,
          }
        );
        return (
          `${matcherHint(`.${name}`)}\n\n` +
          `Uncompressed expected value:\n` +
          `  ${printExpected(expected)}\n` +
          `Expected value with compressed whitespace to equal:\n` +
          `  ${printExpected(expectedWithCompresssedWhitespace)}\n` +
          `Uncompressed received value:\n` +
          `  ${printReceived(received)}\n` +
          `Received value with compressed whitespace:\n` +
          `  ${printReceived(receivedWithCompresssedWhitespace)}${
            diffString ? `\n\nDifference:\n\n${diffString}` : ``
          }`
        );
      };
  return {
    actual: received,
    expected,
    message,
    name,
    pass,
  };
};

要注册自定义匹配器,您需要将其添加到您的 setupTests 文件中。首先使用 setupFilesAfterEnv 字段在 jest.config.js 中注册 setupTests:

 setupFilesAfterEnv: `<rootDir>/path/to/setupTests.js`,

然后在 expect 对象上注册自定义匹配器。

//setupTests.js

import toMatchStringIgnoringWhitespace from "<rootDir>/path/to/matchStringIgnoringWhiteSpace";

expect.extend({
    toMatchStringIgnoringWhitespace: toMatchStringIgnoringWhitespace
}); 

如果您使用的是 TypeScript,您还需要将类型添加到 expect 对象

虽然这不是直接的答案,但您也可以这样做:

mockedFunction.mock.calls[0] // To get array of arguments

// or 

mockedFunction.mock.calls[0][0] // to get first argument and so on

然后比较相等