Basic Node Unit Testing with Jest

Alex Foreman
Nerd For Tech
Published in
3 min readJul 10, 2021

--

As a junior developer, you might be able to get a job without knowing much (or anything) about testing. However, it’s invaluable information as you progress in your career, as all software engineering roles will ultimately require testing in some form or another. So understanding testing and its implementation could set you apart as a new programmer.

It’s important to understand, first, that there are many types of testing with varying degrees of complexity and automation. I will start with the basics, touching on one of the simplest forms, unit tests.

Unit testing (sometimes known as component testing) is a good place from which to start because it requires you to think about your code and how it should operate, and therefore, how it could potentially fail. You are testing individual components or units of your code, most notably, your functions. When you write a function, you are expecting a certain input to return a certain output.

Jest is a JavaScript testing library that works with Node, React, Angular, and Vue, among other frameworks. It comes built-in to your Create React App and others and is helpful testing the previously mentioned input/output scenarios, as well as more complex logic you might have written.

The Jest docs contain a ton of helpful examples and methods and implementation practices. Based on these, I have set up a simple Node example to understand a unit test.

Simple function in a practice file

Considering the above function, here is how you could create a simple test. First, create a testing file. Jest will automatically test any file containing “test” or “spec.” You then test your variables with your expected outcome. Since this function is simple arithmetic, we can implement the test as follows:

Testing some math

Now that you have written a test, type “npm run test” in your terminal to get the following output:

The test passes

You can see how simple it is to validate your code. Here’s an example of a failure:

FAILURE

In the above example, I added two more tests for the product function, but I did some incorrect math for one of them. Jest conveniently points out the line of code where the mistake occurs and tells how the received input (what you passed in) differs from the expected output (what Jest knows to be correct based on your code).

You can see, though, that user error could easily give a false positive or negative. For example, in the test above that fails, I passed in a string with the correct input and output, but that doesn’t matter. The string could say anything. Because my test expects the answer toBe(16), my variables should combine to return that product, but they do not.

Jest refers to wording like toBe(16) as a “matcher,” used when you have an exact expectation. Other examples of matchers include toEqual, toBeNull, toBeTruthy, toContain, and toBeGreaterThan, among many others. By implementing a variety of matchers, you could potentially avoid errors with edge cases, as your code becomes more complex. Some matchers refer more specifically to strings or booleans, as well as iterables and even errors.

Jest allows you to create much more complex tests than what I outlined above, including those for asynchronous requests, as in the case of API fetches. But this is a good starting place for beginning to test functions with an expected output.

As you grow as a programmer, you will write tests that automatically run with the program and which handle much more difficult tasks, but unit testing is a wonderful way to make sure you understand your own code.

--

--