-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for nice error messages with assert
#2237
Comments
I don't think this is controversial, there are two pieces to this:
Finally, I am gonna put the hammer down a bit on "power-assert". I think it is way too crazy as a default. I don't wanna do anything to prevent you from using it though and you should already be able to, no? If no, then that should be fixed separately. We spent a ton of time making failures look really good and helpful this half and while we could bikeshed forever, the status quo is pretty and gives good signal. None of the things I mention here are actively being worked on so whatever solution you think makes sense, taking it to a PR will make it more likely to get into Jest. Code wins arguments. |
built-in here's an example of an assert error. however, when using assertion like this: all you get is I think the best option for now is to make |
@cpojer @DmitriiAbramov can we close this issue then? |
i guess we're not going to get here anytime soon. we should add a list of |
Yes, same here, I really can't be bothered to learn or use (I guess it autocompletes though) all those I made https://github.com/caub/deep-eq beause it's often useful to test deep objects, and I use it like in the first comment, for every assertions. One API to learn I really wish Jest would support something like this natively! |
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. |
Do you want to request a feature or report a bug?
Feature.
What is the current behavior?
Right now you only get nice assertions from Jest if you use
expect
.What is the expected behavior?
It would be awesome to have nice error messages for
assert
built-in by default, like Ava does, becauseassert
has a lot less downsides thanexpect
.I realize this is going to be potentially controversial, since many people use
expect
as their main assertion library, but please hear me out, because I think there are some pretty objectively good reasons to offer an alternative...1.
expect
has long, hard to remember method names.Things like
toHaveBeenCalledWith
ortoBeGreaterThanOrEqual
are easy to misspell and forget since they are so long to begin with. And typing them out gets pretty tedious.2.
expect
has inconsistent naming schemes.To solve the "hard to type" problem,
expect
adds some aliases. But the naming schemes are inconsistent with each other. For example, there is an alias fortoHaveBeenCalled
namedtoBeCalled
. But the alias fortoHasBeenLastCalledWith
is not namedtoBeLastCalledWith
, it's justlastCalledWith
, which is unexpected.3.
expect
uses incorrect grammar.On top of it already being hard to remember long method names, many method names are not grammatically correct, making it even harder to remember them. For example, it is
toBeLessThanOrEqual
instead oftoBeLessThanOrEqualTo
. And it istoHaveBeenCalledTimes
. But all of those built-in method names are awkward to say, and thus hard to remember.4.
expect
is very unfriendly to non-English speakers.If you consider the case of someone who isn't a native English speaker, trying to learn conjuctions and verb tenses correctly in English can be hard. But much of
expect
's API is tied to knowing specific conjuctions and verbs and tenses. For example it'stoHaveLength
, which is grammatically correct, but it's not easy to remember that it's nottoBeLength
, especially when "be" is heavily used elsewhere. Or for example it istoContain
to check a child element, and nottoHave
.This is made even worse by the previous issue, since some of the method names aren't grammatically correct to begin with, so it's even harder for non-English speakers.
5.
expect
doesn't build on Javascript's built-in functionality.expect
uses very specific method names to allow for nice error messages, which makes sense when you want to try to provide them at runtime. But with the rise of Babel and other source transformations this is no longer a requirement. And once you ditch that requirement, things likepower-assert
become available, which let you use the native Javascript syntax to express your tests.Instead of something like:
You get to write:
Which are readable by anyone who knows Javascript, without having to learn an API, without having to know English well, and without having to type long strings.
And they allow for more flexibility, because, "it's just Javascript". I think this is similar to how JSX gives you so much more flexibility over previous templating systems.
All of these issues add up to having to refer to the documentation much more often than should be necessary, and also having to write a lot more verbose assertions that should be necessary.
I open this not to start a fight, but because I think Jest's goal is pretty awesome, and I think including something like
power-assert
would make it even better.The text was updated successfully, but these errors were encountered: