jest icon indicating copy to clipboard operation
jest copied to clipboard

Added expect.satisfies(predicate) matcher

Open johnw42 opened this issue 2 years ago • 3 comments
trafficstars

Summary

This PR adds a new matcher, expect.satisfies(predicate), similar to toSatisfy matcher from the jest-extended package but implemented as an asymmetric matcher for more flexibility. It is useful for composing with other expectation functions to test for arbitrary conditions without going outside the expect(subject).toVerb(object) pattern.

For example, consider the example code for expect.objectContaining(object), but modified to accept only even numbers:

test('onPress gets called with the right thing', () => {
  const onPress = jest.fn();
  simulatePresses(onPress);
  const isEven = (n) => n % 2 === 0;
  expect(onPress).toHaveBeenCalledWith(
    expect.objectContaining({
      x: expect.satisfies(isEven), // Prints as "Satisfies isEven"
      y: expect.satisfies(isEven),
    }),
  );
});

Without expect.satisfies, the expect call has to be totally rewritten in a way that's harder to read and produces much less helpful error messages:

let isOk = false;
for (const [arg] of onPress.mock.calls) {
  if (isEven(arg.x) && isEven(arg.y)) {
    isOk = true;
    break;
  }
}
expect(isOk).toBe(true);

For further motivation, consider the Truly matcher from Gtest, which is used in numerous places in Chromium.

Test plan

This PR includes full unit tests for the new functionality.

johnw42 avatar May 16 '23 18:05 johnw42

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: johnw42 / name: John Williams (94def65766a038c6593668064453470ee761733c, 3153cf2ba73059702deefad1f86bac2cc743dcb7, 19f528df13dacf1a7ffde454cba9b68b897dd454, e1941556a8dd52bdfb5f21612689a5d2cae522a2)

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
Latest commit e1941556a8dd52bdfb5f21612689a5d2cae522a2
Latest deploy log https://app.netlify.com/sites/jestjs/deploys/6463c546f5846e000841ed60
Deploy Preview https://deploy-preview-14145--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 settings.

netlify[bot] avatar May 16 '23 18:05 netlify[bot]

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

github-actions[bot] avatar Oct 13 '24 02:10 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 Nov 12 '24 03:11 github-actions[bot]

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Dec 13 '24 00:12 github-actions[bot]