DEV Community

Cover image for How to write unit tests in JavaScript with Jest

How to write unit tests in JavaScript with Jest

Domagoj Štrekelj on July 24, 2021

Unit testing is an important and often overlooked part of the development process. It is considered boring by many, and being traditionally difficu...
Collapse
 
chiaralyn profile image
ChiaraLyn

I have recently discovered the importance of TDD and will soon study how to do it. These guides are invaluable and help me so much to understand the dynamics behind good code. This article is saved in my favorites, thank you very much.

Collapse
 
dstrekelj profile image
Domagoj Štrekelj • Edited

Hi @chiaralyn ! Thank you for the comment :)

It's very motivating to hear you saved the article to your favorites. I'm happy you found it helpful and enjoyable!

There's a new article in this series coming up next week where we'll talk about mocking imported functions with Jest. This will also be our first look into writing solitary unit tests for sociable units. I'd love to hear your thoughts on it once it's published :)

Collapse
 
chiaralyn profile image
ChiaraLyn

I can't wait to read the new series of articles. It would be really useful and interesting. Thank you so much for your efforts and your kindness!

Collapse
 
bobrundle profile image
Bob Rundle

Great introduction to Jest. Got it fully working in my express 4.17 test bench.

However now I want to use isInteger client-side. I can't use modules in the browser and have to rewrite isInteger as a simple function. Now my Jest unit test doesn't work anymore. I get "isInteger is not a function".

So I am wondering how to test client-side JS with Jest. Should be a simple way to do it but it is not obvious to me.

Collapse
 
andreeadraniceanu profile image
AndreeaDraniceanu

Thanks for sharing this informative post on unit testing in JavaScript with Jest! It's always valuable to learn about different approaches and techniques. By the way, I recently came across a comprehensive guide on unit testing with Jest that covers various aspects in depth. It provides a step-by-step approach, practical examples, and helpful tips. If you're interested in diving deeper into Jest and expanding your unit testing knowledge, you might find it useful. Keep up the great work with your blog posts

Collapse
 
juanfabiorey profile image
juanfabiorey

Oh great! Thank you!

Collapse
 
dstrekelj profile image
Domagoj Štrekelj

You're welcome juanfabiorey :)

Collapse
 
fdmmarshall profile image
Flor Marshall

I'm currently implementing jest/ unit tests in the application I work on, its my first time and although there are a good amount of examples and jest documentation, your article was straight forward, descriptive, and very helpful. Thanks! I look forward to the following parts in the series.

Collapse
 
mac10046 profile image
Abdeali • Edited

I have not written any test code in js yet, but your article is nice and simple and also credits to jest, I look forward to write some test code soon

Collapse
 
dstrekelj profile image
Domagoj Štrekelj

Thank you for the comment Abdeali!

Glad to hear you're going to try your hand at writing tests. Let me know how it goes! :)

Collapse
 
srshifu profile image
Ildar Sharafeev

How to optimize test execution in pre-commit hook? find answer in my post: dev.to/srshifu/under-the-hood-how-...