I'm sorry if this feature has already been requested before, but I thought I'd ask anyways.
It would be great if Jest expectations supported chaining of matchers in some way. It allows for less code duplications. For example, suppose we want to run two matchers against an array value
variable:
const value = [1, 2, 3];
expect(value).toHaveLength(3);
expect(value).toContain(3);
If chaining is supported, then we would not have to duplicate expect(value)
. I know that this is supported in the Chai expectation library, via language chains, like so:
const value = [1, 2, 3];
expect(value).to.have.length(3)
.and.contain(3);
Has this feature been considered before for Jest? If so, I'd love to learn more about its exclusion, whether it be a lack of time or a specific design decision. Regardless, I strongly vouch for the inclusion of this feature. I provided a simple example, but I could see considerable time savings while writing tests for more complicated matchers being run on a single value, such as testing a React component using Enzyme matchers.
Thank you for your time answering this.
Currently we are not interesting in adding support for this, mainly because it adds complexity that few people need.
@cpojer I respect your opinion as a primary maintainer of Jest. However, I would like to explore deeper into your view of this feature only being beneficial to a few users. This is a fairly common feature in many other test expectation libraries. In the JavaScript world, Chai and Michael Jackson's Expect support this. This is also commonplace in popular testing libraries for other languages, such as Rspec for Ruby, Hamcrest for Java and NUnit for C#
I am willing to work on a PR for this functionality as a Proof Of Concept. I just want to make sure that it would be considered before I begin the work. Thanks again.
We don't have to support every feature. I understand many libraries support it, but I don't really see the upside of supporting it directly in Jest besides added complexity. You can also build your own wrappers to support behavior like this.
How about something like:
expect([1, 2, 3]).toHaveLength(3).and.toContain(3);
Matchers would just have to return the "expectation" object wrapped in the and
property of an object
For anyone else coming to this issue I've just published jest-chain
which will allow you to chain core Jest matchers and any custom matchers you may use i.e. jest-extended
馃槃
@cpojer do you think its worth linking to jest-chain
in the docs for others who may be looking for this behaviour?
Yeah, happy for this to be part of the docs!
For anyone else coming to this issue I've just published
jest-chain
which will allow you to chain core Jest matchers and any custom matchers you may use i.e.jest-extended
@cpojer do you think its worth linking to
jest-chain
in the docs for others who may be looking for this behaviour?
Exactly what I needed to refactor my tests into something more concise. Great stuff.
Most helpful comment
For anyone else coming to this issue I've just published
jest-chain
which will allow you to chain core Jest matchers and any custom matchers you may use i.e.jest-extended
馃槃@cpojer do you think its worth linking to
jest-chain
in the docs for others who may be looking for this behaviour?