DEV Community

Cover image for Start Testing Your JavaScript Code with Jest

Start Testing Your JavaScript Code with Jest

Dylan Oh on December 19, 2021

Why should we write tests for our code? When there are more than one developers making changes actively to the code base, issues and bug...
Collapse
 
jaecktec profile image
Constantin

Not a big fan of jest, since it has something about 800 dependencies.
Sadly the ide support for UVU is not great yet, however I'm looking forward.

Currently I prefer mocha for backend node code. For mocks I try to avoid them however if I have to I use sinon

Collapse
 
ohdylan profile image
Dylan Oh

Agreed with that! Because Jest pretty much covers most of the functionalities by itself and it is not as flexible like others :)

Mocha and Sinon makes a good combination too!

Collapse
 
jaecktec profile image
Constantin

you should also mention proxyquire since sinon does not cover everything.
Mocking 3rd party deps is easy with jest, however requires some tinkering with sinon.
Sadly...

Thread Thread
 
ohdylan profile image
Dylan Oh

Well, I guess nobody is perfect :) Basically, I stick with Jest for the initial setup, and will combine with other libraries when necessary

Thread Thread
 
jaecktec profile image
Constantin

In my experience that works well. I just got paranoid with the recent supply chain attacks 😂

Collapse
 
spock123 profile image
Lars Rye Jeppesen

I like Jest but it quickly becomes a mess when introducing Typescript, and testing backend code.

Mocking named exports is a big mess.

Other than that I like Jest (except for their ties to Facebook evil )

Great article, good for introduction, thanks

Collapse
 
ohdylan profile image
Dylan Oh

Thanks for leaving a comment :)
Yea... It needs a lot of workarounds when it comes to Typescript :)

Collapse
 
spock123 profile image
Lars Rye Jeppesen

It does - but I still use Jest as for me its the best all around unit testing framework/library.

For E2E and integration tests, I swear on Cypress, but Jest rules for unit tests.

Cheers, and thanks

Collapse
 
rishadomar profile image
Rishad Omar

Thanks! This article has made me curious to see how jsx is tested.

Collapse
 
ohdylan profile image
Dylan Oh

Sure Rishad! I will try to share more of this in the next article ;)

Collapse
 
assertnotnull profile image
Patrice Gauthier

Besides the problems with Typescript it also suffers from memory leaks. I wouldn't event recommend it. It's just used because of herd effect. Tests are much faster and stable with Mocha.

Collapse
 
ohdylan profile image
Dylan Oh

Thanks for leaving a comment Patrice :)
I see... I haven't read up on its memory leaks issue, but I am interested to read up more and will definitely share my findings on that! Thank you for your suggestions :)

Collapse
 
prabhukadode profile image
Prabhu

Good

Collapse
 
ohdylan profile image
Dylan Oh

Thanks!

Collapse
 
jazzminnno profile image
JazzminnNo

Thank you for this article. This helped in my journey exploring unit testing :)

Collapse
 
ohdylan profile image
Dylan Oh

No problem! Glad this could help :)

Collapse
 
saroj8455 profile image
Saroj Padhan

Thank you

Collapse
 
paulcanning profile image
Paul Canning

Code, not codes.

Collapse
 
ohdylan profile image
Dylan Oh

Thanks a lot Paul! I have made the corrections :)

Collapse
 
thenickest profile image
TheNickest

Nice article on testing! Interested to see more of this in the future.

Collapse
 
ohdylan profile image
Dylan Oh

Thanks! Will definitely share more :)

Collapse
 
sipamungkas profile image
Ragil Pamungkas

thank you for the article. why did u use jest instead of mocha and chai?
I can't wait to see how to use jest on jsx or tsx hehe

Collapse
 
ohdylan profile image
Dylan Oh

Hey Ragil, thanks for leaving a comment! I generally prefer Jest because it provides a lot of built-in functionalities such as test runner, code coverage etc, without having to combine with other testing tools (it's like all in one) :)

However, Mocha makes a good combination with other libraries such as Sinon.js, Chai.

In order to test React, I planned to use methods from RTL and combination with Jest :)

Collapse
 
ugurguler profile image
Uğur Güler

thanks very helpful

Collapse
 
ohdylan profile image
Dylan Oh

Glad you find it helpful :)

Collapse
 
metammodern profile image
Buniamin Shabanov

Don't.

Collapse
 
prabhukadode profile image
Prabhu

Good

Collapse
 
kspshnik profile image
Evgeny Karpel

Why don't you mock fetch itself? Shall we test 'isomorphic-fetch' itself??

Some comments have been hidden by the post's author - find out more