Add expect.toPass to non-literal value assertions
See original GitHub issueDo 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:
- Created 6 years ago
- Reactions:4
- Comments:14 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This API currently exists in jest-extended see
toSatisfy
.Once custom asymmetric matchers is released in Jest then jest-extended can also offer all APIs asymmetrically and the above will become:
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 🤙