Member-only story

Unit Testing with Jest

Jeff P
11 min readOct 30, 2023

--

I kind of never really saw the bigger picture with unit testing until I watched an excellent YouTube video by Kyle over at Web Dev Simplified (by the way, follow this guy, his videos are excellent!)

NOW I get it…. ok, so for small projects of say 1000 lines of code or less, it’s probably not necessary, as it’s fairly straightforward to skim through the code and identify the issues, especially if you’ve been adding console logs along the way that help confirm things are working as expected. However if you’re involved is medium to high complexity projects over thousands and thousands of lines of code, and/or your working as part of a team, then unit testing becomes really important.

Here are a couple of variables, and a few functions….it’s a very silly example, but at least it hopefully explains the importance of unit testing:

(Important note: no errors at all are reported with this code in VS Code!)

const word3 = 'brush';
const word4 = 'pick';

function checkLength(word3) {
if (word4.length > 4) {
return true;
} else {
return false;
}
}

function checkIfWordContainsI() {
if (word3.includes('i')) {
return true;
} else {
return false;
}
}

function concatTwoWords(word1, word2) {
return word1 + word3;
}

if I called checkLength() without an argument, what response would I get? true or false?

checkLength()
// false

what about if I pass in word 3 as the argument when I call it?

--

--

Jeff P
Jeff P

Written by Jeff P

I tend to write about anything I find interesting. There’s not much more to it than that really :-)

No responses yet