jest icon indicating copy to clipboard operation
jest copied to clipboard

Pass jest-diff options to fix a11 y issues#12576

Open grazirs opened this issue 3 years ago • 12 comments

Summary

This PR is a first step to solve #12576. The proposed solution was to let the user pass a DiffOptions to the configs, allowing them to set their own color choices. However, accessing the config where the default colors are used would require deep refactoring.

As a first approach, with this PR we let the user pass a MatcherHintOptions and DiffOptions in the expect state with expect.setState, and we make sure the default colors are used only if the user didn't overwrite them.

There are some methods where the default colors are still being used because passing a MatcherHintOptions or DiffOptions to them would require deep refactoring.

The affected files are:

  • packages/jest-each/src/validation.ts
  • packages/jest-snapshot/*
  • packages/expect/src/spyMatchers.ts

Test plan

These changes are not affecting the current behavior of the code or breaking any test, so no new tests have been added.

grazirs avatar Oct 03 '22 00:10 grazirs

Hi @grazirs!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

facebook-github-bot avatar Oct 03 '22 00:10 facebook-github-bot

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

facebook-github-bot avatar Oct 03 '22 02:10 facebook-github-bot

This is a great start, thanks for working on it! I have no comments beyond the ones left by @mrazauskas 👍

And yeah, tests that assert options passed through are respected seems reasonable. This PR brings down overall code coverage - while admittedly it's not super high currently, this PR should quite easily be able to keep the status quo 🙂

SimenB avatar Oct 03 '22 06:10 SimenB

Hi @SimenB and @mrazauskas, thanks for your suggestions. I started working on the tests for matcher-utils, and I'd like to have your feedback on the approach I want to have. Basically for each test already existing in matcher-utils, after all the assertions, I additionally assert that every function I changed in this PR is called with the color options passed by the user.

Here's an example of how it would look like for a test of the toBe matcher in packages/expect/src/__tests__/matchers.test.js:

const matcherUtils = require('jest-matcher-utils');
const chalk = require('chalk');
const {stringify} = require('jest-matcher-utils');
const {expect: jestExpect} = require('../');

const matcherHintOptions = {
  expectedColor: chalk.yellow,
  noDim: true,
  receivedColor: chalk.magenta,
  secondArgumentColor: chalk.yellow,
};
const diffOptions = {
  aColor: chalk.bgHex('#0000FF'),
  bColor: chalk.magenta,
  noDim: true,
};

expect.setState({diffOptions, matcherHintOptions});

const registerSpies = () => {
  return {
    matcherHint: jest.spyOn(matcherUtils, 'matcherHint'),
    printDiffOrStringify: jest.spyOn(matcherUtils, 'printDiffOrStringify'),
    printExpected: jest.spyOn(matcherUtils, 'printExpected'),
    printReceived: jest.spyOn(matcherUtils, 'printReceived'),
  };
};

const expectUserColorsAreUsed = (
  matcherHint,
  printExpected,
  printReceived,
  printDiffOrStringify,
) => {
  matcherHint.mock.calls.forEach(call => {
    const passedOptions = call[3];
    Object.keys(matcherHintOptions).forEach(key => {
      expect(matcherHintOptions[key]).toEqual(passedOptions[key]);
    });
  });

  printExpected.mock.calls.forEach(call =>
    expect(call[1]).toEqual(diffOptions.aColor),
  );

  printReceived.mock.calls.forEach(call =>
    expect(call[1]).toEqual(diffOptions.bColor),
  );

  printDiffOrStringify.mock.calls.forEach(call =>
    expect(call[5]).toEqual(diffOptions),
  );
};

[
  [1, 2],
   ...
  [-0, +0],
].forEach(([a, b]) => {
  it(`fails for: ${stringify(a)} and ${stringify(b)}`, () => {
    const {matcherHint, printExpected, printReceived, printDiffOrStringify} =
      registerSpies();

    expect(() => jestExpect(a).toBe(b)).toThrowErrorMatchingSnapshot();

    expectUserColorsAreUsed(
      matcherHint,
      printExpected,
      printReceived,
      printDiffOrStringify,
    );
  });
});

I would do the same in all the matchers and test where styled strings are printed.

To ensure that there are no hardcoded colors used somewhere, I'm not passing the default colors in the matcher's state. This will make the snapshots update, as the output of the tests will have a different style.

As I wrote in the PR description, there are some matchers that cannot be updated in this PR, therefore their tests will not be updated.

grazirs avatar Oct 09 '22 21:10 grazirs

I would do the same in all the matchers and test where styled strings are printed.

To ensure that there are no hardcoded colors used somewhere, I'm not passing the default colors in the matcher's state. This will make the snapshots update, as the output of the tests will have a different style.

That seems reasonable to me!

SimenB avatar Oct 11 '22 01:10 SimenB

Hi @SimenB @mrazauskas! Are there any updates on this?

grazirs avatar Oct 31 '22 13:10 grazirs

This PR is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Apr 14 '23 22:04 github-actions[bot]

This PR was closed because it has been stalled for 30 days with no activity. Please open a new PR if the issue is still relevant, linking to this one.

github-actions[bot] avatar May 14 '23 23:05 github-actions[bot]

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: grazirs / name: Graziele Ribeiro Santos (84f32f6a6f49e657003b04abec7e879333f84c62, 3383cf406edae442e1e7c8d9cb774ae4509356df, ac1c239852a43533345cfc578e8ab192a7a42e39, 2c184e6daae9c048651a6d63befbc23829b399ac, 0d4e9678a3ab2bd6f1eb7a100aebf38bcb8cd6ec, 5dc8dd2f07de9b7d15dad04cb1dd972fdac7ec75, d9d9ca6da3a0915a108c5c0fdd9eef463a46e1ed, c36b9524632c90461dfffbdfcb33abdfe62c7813, eaccdf52d56aeadedcccb2071e93971f4d7256b5, cfbf23b957c106c4b0830c28c3b1f224847ebdb8, 18cc8f6a60e52a9f638b77830049384108f78ba8, 396929f6e5d16f918cc34a36c135a1ce8cb0ddf2, 7af2685236e303413a4e52be5c4e32dee666662e, c16d49f748f6e3c096ae0da389401b7062b1a686, 83e9a4825e132bef9c860aa791e760b3bf79a7b1, 9c9c11f3b7d93d930fa13efdd2423767a6c1004a, 5fb4f436b37151077ddf84cc2453a83881690586, 13a140c5652c46b28683e2366b4a722101a1564e, 5d97465ff1dce3e2c9e031e309c7f212d8a91c2f, 25be7fc6b5b12d5f331fcd50d3e6467f0abac410, 094f16b41fe139a2c8dba9b43f5f369142450786)
  • :white_check_mark: login: SimenB / name: Simen Bekkhus (bb3abfb95a297de718648644af93e5bfbfab4bcf)

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
Latest commit 094f16b41fe139a2c8dba9b43f5f369142450786
Latest deploy log https://app.netlify.com/sites/jestjs/deploys/64fc4b40218de70008761463
Deploy Preview https://deploy-preview-13362--jestjs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Sep 08 '23 21:09 netlify[bot]

Hi @SimenB and @mrazauskas . I synced my branch with main and resolved the conflicts. All the tests are passing on my machine (node 16.20.0 on macOS) but the snapshot are failing on the CI. Any idea on what it might be?

grazirs avatar Sep 09 '23 12:09 grazirs