DEV Community 👩‍💻👨‍💻

Cover image for Jest test vs it
Chris Bongers
Chris Bongers

Posted on • Originally published at

Jest test vs it

When working on Jest test cases, you might come across two different approaches, as you can see below.

test('if this function succeeds', () => {

it('should test if this function succeeds', () => {
Enter fullscreen mode Exit fullscreen mode

As you can see, the above tests are pretty much the same function. However, the naming is different.

The difference between test and it in Jest

Basically, it is an alias for test, so they are functionally the same.

So, what makes it different?

This alias is created to make your tests more readable from a test output point of view.

It can really help make your tests more readable from a readability point of view.

Imagine the second test to use the test function.

test('should test if this function succeeds', () => {
Enter fullscreen mode Exit fullscreen mode

This immediately sounds a bit off, right?

And these outputs definitely show up weird in your test result, so try to make them look as much like English sentences as possible.

Which one wins?

This totally depends on the use case.

I personally use it more for render checks. It often makes more sense.

  • "it should have a button."
  • "it should navigate to x."

However, it's totally up to you which one makes more sense for specific use-cases.

Which one do you prefer?

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (3)

auroratide profile image
Timothy Foster • Edited on

I use to use it but started using test when I started naming tests after the scenario rather than the expected outcome. For example:

test('user inputs a negative number', () => {})
test('user inputs a positive number', () => {})
Enter fullscreen mode Exit fullscreen mode

Assuming the outcome is a readable line of code and somewhere around the bottom of the test, I found this both helps me navigate the tests and gets me thinking in terms of alternate paths.

lexlohr profile image
Alex Lohr

Both have their pros and cons:

  • it
    • only two letters
    • coincides with the output
    • makes you think about the unit
  • test
    • easier to port to other testing tools
    • more verbose that this is a test
    • makes you think about testing

But, whatever you choose, please be consistent or be prepared to confuse the living jester out of your co-maintainers.

dailydevtips1 profile image
Chris Bongers Author

Great last point!
When used intertwined it can cause a lot of confusion!

🤔 Did you know?

✍️ Writing your own article is easy (we even support markdown)