When working on Jest test cases, you might come across two different approaches, as you can see below.
test('if this function succeeds', () => {
expect(1).toBeTruthy();
});
it('should test if this function succeeds', () => {
expect(1).toBeTruthy();
});
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', () => {
expect(1).toBeTruthy();
});
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)
I use to use
it
but started usingtest
when I started naming tests after the scenario rather than the expected outcome. For example: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.
Both have their pros and cons:
it
test
But, whatever you choose, please be consistent or be prepared to confuse the living jester out of your co-maintainers.
Great last point!
When used intertwined it can cause a lot of confusion!