jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.29k stars 6.47k forks source link

[FEATURE REQUEST] Chaining expect matchers #3998

Closed ecbrodie closed 7 years ago

ecbrodie commented 7 years ago

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.

cpojer commented 7 years ago

Currently we are not interesting in adding support for this, mainly because it adds complexity that few people need.

ecbrodie commented 7 years ago

@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.

cpojer commented 7 years ago

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.

matthias-t commented 6 years ago

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

mattphillips commented 6 years ago

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?

cpojer commented 6 years ago

Yeah, happy for this to be part of the docs!

baspeeters commented 6 years ago

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.

github-actions[bot] commented 3 years ago

This issue 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.