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

rbpvctlc  于 2023-04-03  发布在  Jest
关注(0)|答案(3)|浏览(161)

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

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

  • 注:我编辑了问题,使其更通用。
yvt65v4c

yvt65v4c1#

正如@Timo所说,唯一的方法似乎是使用自定义匹配器。这里有一个基于Jest的toEqual匹配器将所有空格压缩到一个单一的空格以提高可读性。它将处理制表符,换行符等。它将为您提供像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对象following the instructions here中。

vfh0ocws

vfh0ocws2#

据我所知,没有办法实现这一点与Jest开箱即用。
但是,你可以使用expect.extend编写自己的可重用匹配器。从两个字符串中删除所有空格,例如通过str.replace(/\s/g, ''),并比较字符串。

pobjuy32

pobjuy323#

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

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

// or 

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

然后与平等进行比较。

相关问题