忽略空格的字符串的 Jest Equality Matcher

Und*_*ion 15 javascript string matcher html-escape-characters jestjs

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

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

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

Und*_*ion 10

As @Timo says, the only way of doing this appears to be with a custom matcher. Here is one that compresses all whitespace down to a single space for readability based on Jest's toEqual matcher. It will deal with tabs, newlines etc. It will give you pretty output like the included Jest matchers:

//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,
  };
};
Run Code Online (Sandbox Code Playgroud)

To register the custom matcher you need to add it to your setupTests files. First register setupTests in your jest.config.js using the setupFilesAfterEnv field:

 setupFilesAfterEnv: `<rootDir>/path/to/setupTests.js`,
Run Code Online (Sandbox Code Playgroud)

And then register the custom matcher on the expect object.

//setupTests.js

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

expect.extend({
    toMatchStringIgnoringWhitespace: toMatchStringIgnoringWhitespace
}); 

Run Code Online (Sandbox Code Playgroud)

If you are using TypeScript you will also want to add the typings to the expect object following the instructions here.

  • 这非常有用 - 仅建议更改:`const replaceWhitespace = (str) =&gt; str.replace(/\s+/g, ' ').trim()` 以考虑字符串开头/结尾的空格。 (2认同)

Tim*_*imo 5

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

但是,使用expect.extend. 从两个字符串中删除所有空格,例如 via str.replace(/\s/g, ''),并比较字符串。