question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add expect.toPass to non-literal value assertions

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Right now I have to write a test like this:

test('some test', () => {
  const arg = {a: 'b', c: Math.random()}
  const subject = jest.fn()
  subject(arg)
  expect(subject).toHaveBeenCalledWith({
    a: 'b',
    c: expect.any(Number),
  })
  expect(subject.mock.calls[0][0].c).toBeLessThanOrEqual(1)
})

I’d prefer to write this test like this:

test('some test', () => {
  const arg = {a: 'b', c: Math.random()}
  const subject = jest.fn()
  subject(arg)
  expect(subject).toHaveBeenCalledWith({
    a: 'b',
    c: expect.toPass(val => expect(val).toBeLessThanOrEqual(1))
  })
})

I’m fine if we call it something else or if the API is different, but this would be really helpful 😃

Note, with my proposed API you could write multiple assertions:

test('some test', () => {
  const arg = {a: 'b', c: Math.random()}
  const subject = jest.fn()
  subject(arg)
  expect(subject).toHaveBeenCalledWith({
    a: 'b',
    c: expect.toPass(val => {
      expect(val).toBeGreaterThanOrEqual(0)
      expect(val).toBeLessThanOrEqual(1)
    })
  })
})

Thoughts?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
mattphillipscommented, Mar 19, 2018

This API currently exists in jest-extended see toSatisfy.

it("some test", () => {
  const arg = {a: 'b', c: Math.random()}
  const subject = jest.fn()
  subject(arg)
  expect(subject).toHaveBeenCalledWith({
    a: 'b',
    c: expect.any(Number)
  });

  expect(subject.mock.calls[0][0].c).toSatisfy(n => {
    expect(n).toBeGreaterThanOrEqual(0);
    expect(n).toBeLessThanOrEqual(1);
  });
});

Once custom asymmetric matchers is released in Jest then jest-extended can also offer all APIs asymmetrically and the above will become:

it("some test", () => {
  const arg = {a: 'b', c: Math.random()}
  const subject = jest.fn()
  subject(arg)
  expect(subject).toHaveBeenCalledWith({
    a: 'b',
    c: expect.toSatisfy(n => {
      expect(n).toBeGreaterThanOrEqual(0);
      expect(n).toBeLessThanOrEqual(1);
    });
  });
});
1reaction
rickhanloniicommented, Mar 20, 2018

TIL @mattphillips, I missed that one 👍

We’re not going to add this to Jest core right now. We encourage people to use jest-extended, custom asymmetric matchers, and custom matchers, so avoiding them is not a compelling argument to add it to core 🤙

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chainable asymmetric matchers · Issue #5788 · facebook/jest
I don't think it provides a lot of value for the added complexity and it can make assertions harder to read rather than...
Read more >
What is the difference between literals and non-literals, other ...
Clearly literals can be mutable in Rust. First, you need to understand what a literal is. Literals are never mutable because they are ......
Read more >
Expression functions in the mapping data flow - Microsoft Learn
Calculates a cosine inverse value. add, Adds a pair of strings or numbers. Adds a date to a number of days. Adds a...
Read more >
Build settings reference | Apple Developer Documentation
A detailed list of individual Xcode build settings that control or change the way a target is built.
Read more >
SystemVerilog Assertions and Functional Coverage
Chapter 14 describes the 'expect' statement and how it applies to formal and simu- ... 6.3 Sampling Edge (Clock Edge) Value: How Are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found