How to use the jest-diff function in jest-diff

To help you get started, weโ€™ve selected a few jest-diff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github testing-library / jest-dom / src / to-have-style.js View on Github external
function expectedDiff(expected, computedStyles) {
  const received = Array.from(computedStyles)
    .filter(prop => expected[prop])
    .reduce(
      (obj, prop) =>
        Object.assign(obj, {[prop]: computedStyles.getPropertyValue(prop)}),
      {},
    )
  const diffOutput = jestDiff(
    printoutStyles(expected),
    printoutStyles(received),
  )
  // Remove the "+ Received" annotation because this is a one-way diff
  return diffOutput.replace(`${chalk.red('+ Received')}\n`, '')
}
github testing-library / jest-native / src / to-have-style.js View on Github external
function expectedDiff(expected, elementStyles) {
  const received = Object.keys(elementStyles)
    .filter(prop => expected[prop])
    .reduce((obj, prop) => Object.assign(obj, { [prop]: elementStyles[prop] }), {});

  const diffOutput = jestDiff(printoutStyles(expected), printoutStyles(received));
  // Remove the "+ Received" annotation because this is a one-way diff
  return diffOutput.replace(`${chalk.red('+ Received')}\n`, '');
}
github ybonnefond / stubborn / test / matchers / toReplyWith.ts View on Github external
const fail = (message: string, { expected, received }: any = {}) => {
    const extra = expected
      ? diff(expected, received, { expand: this.expand })
      : '';
    return {
      pass: false,
      message: () =>
        `${message}\n${extra}\nRequest received:\n${JSON.stringify(
          response.body,
          null,
          2,
        )}`, // eslint-disable-line no-magic-numbers
    };
  };
github storybookjs / storybook / src / test_runner / snapshot_runner.js View on Github external
const state = this.state;
    const key = story.name;
    const context = { kind: this.kind, story: story.name };
    const tree = story.render(context);
    const renderer = ReactTestRenderer.create(tree);
    const actual = renderer.toJSON();

    const result = state.match(story.name, actual, key);

    const outcome = this.getOutcome();

    if (outcome !== 'unmatched') {
      return { state: outcome };
    }

    const diffMessage = diff(
      result.expected.trim(),
      result.actual.trim(),
      {
        aAnnotation: 'Snapshot',
        bAnnotation: 'Current story',
      },
    );

    if (this.interactive) {
      const shouldUpdate = await this.confirmUpate(diffMessage);
      if (shouldUpdate) {
        state.update = true;
        state.match(story.name, actual, key);
        state.update = false;
        return { state: 'updated' };
      }
github hmsk / jest-matcher-vue-test-utils / src / matchers / toHaveDispatched.ts View on Github external
const diffs = matched.map((action, i): string | null => {
            return `"${actionType}" action #${i} payloads:\n\n${diff(payload, action.payload, { bAnnotation: "Dispatched" })}`;
          }).join("\n\n");
          message = `"${actionType}" action has been dispatched, but payload isn't matched to the expectation\n\n${diffs}`;
github astralapp / astral / resources / assets / js / __tests__ / utils / setup.js View on Github external
: () => {
          const difference = diff(expected, data, {
            expand: this.expand
          })
          return (
            this.utils.matcherHint('toHaveBeenDispatchedWith', undefined, undefined, options) +
            '\n\n' +
            (difference &&
              `Expected: ${this.utils.printExpected(expected)}\n` + `Received: ${this.utils.printReceived(data)}`)
          )
        }
github zth / graphql-query-test-mock / src / handleErrors.js View on Github external
export function printVariablesDoesNotMatchError(
  queryMockConfig: MockGraphQLConfig,
  shouldMatchOnVariables: boolean,
  operationName: string,
  variables: Variables
): void {
  if (shouldMatchOnVariables) {
    let errorStr = `Variables do not match for operation "${operationName ||
      'unknown'}"`;

    if (queryMockConfig.matchVariables) {
      throw new Error(`${errorStr} due to custom "matchOnVariables" function`);
    } else {
      throw new Error(
        `${errorStr}.\n\nVariables in request VS mocked variables: \n${printDiff(
          variables,
          queryMockConfig.variables
        )}`
      );
    }
  }
}
github ConvertKit / slate-plugins / packages / slate-testing-library / src / index.js View on Github external
const message = () =>
      `Difference:\n${diff(expectedDocument, receivedDocument)}`;
    return { actual: received, message, pass };
github romgain / jest-websocket-mock / src / matchers.ts View on Github external
: () => {
          const diffString = diff(expected, received, { expand: this.expand });
          return (
            this.utils.matcherHint(".toReceiveMessage", "WS", "expected") +
            "\n\n" +
            `Expected the next received message to equal:\n` +
            `  ${this.utils.printExpected(expected)}\n` +
            `Received:\n` +
            `  ${this.utils.printReceived(received)}\n\n` +
            `Difference:\n\n${diffString}`
          );
        };
github tmobaird / i-want-to-contribute / src / setupTests.js View on Github external
: () => {
          const diffString = diff(expected, received, {
            expand: this.expand,
          });
          return (
              this.utils.matcherHint('.toInclude') +
              '\n\n' +
              `Expected value to include (using str.includes):\n` +
              `  ${this.utils.printExpected(expected)}\n` +
              `Received:\n` +
              `  ${this.utils.printReceived(received)}` +
              (diffString ? `\nDifference:\n${diffString}` : '')
          );
        };
    return {actual: received, message, pass};

jest-diff

Display differences clearly so people can review changes confidently.

MIT
Latest version published 7 months ago

Package Health Score

93 / 100
Full package analysis

Similar packages