DEV Community

Cover image for Jest: execution testing & automated setup.
Andre Willomitzer
Andre Willomitzer

Posted on

Jest: execution testing & automated setup.

Why Test?

When we write software or applications, generally we try to do it with the future in mind. By that I mean when you're writing code you already somewhat anticipate issues the user will create (for example, entering a letter where a number input should be), and you take steps to catch that error.

But honestly, there is likely something that was missed. There are too many possibilities to catch as you're writing the code. That is where automated testing comes in. There are a variety of frameworks, but because my application is in NodeJS/JavaScript I picked Jest. I also have written 1 or 2 tests for it in the past so I was familiar with it() and describe().

What Is Jest?

Jest is a testing framework used for writing unit tests for your application. It allows you to write tests and test suite files ending in .test.js or .spec.js. In these files, you are able to test individual pieces of your application. For example, in my textToHTML SSG I have a function that processes Markdown syntax, called... processMarkdown()... I know, super creative.

To install Jest, run npm install --save-dev jest in your project folder. Then, as mentioned earlier create a .spec.js file for your testing.

How and What to Test?

Because Markdown has so many symbols and combinations that can result in different HTML, I figured it would be a good idea to test different combinations in my tests.

To test functionality, make sure to require() or include your file containing all the functions you want to test, and be sure to export them in that main file:

  • const { processMarkdown, generatePara } = require("./textToHTML");
  • And then inside textToHTML: module.exports = { processMarkdown, generatePara };

Example Test with processMarkdown()

Here are a few examples of tests I wrote. describe() is used to group tests into a suite, and can be used to organize WHAT the group of tests is testing. it() can be used to run a single test.

describe("it should process various Markdown combinations", () => {
  it("should handle a blank file", () => {
    //testing if the input I give is blank (empty file but it exists)
    const emptyContent = "";
    const returnResult = processMarkdown(emptyContent);
    expect(returnResult).toMatch(""); //it should be blank if md.render() worked

  it("should return a string", () => {
    const testString = "";
    const returnResult = processMarkdown(testString);
    expect(typeof returnResult).toBe("string");

  it("it should return h1", () => {
    const testString = "# Should be inside h1.";
    const returnResult = processMarkdown(testString);
    const expectedResult = "<h1>Should be inside h1.</h1>";
Enter fullscreen mode Exit fullscreen mode

This is only a small portion of the tests, but you can write as many as you want for whatever functionality you want in your program.

Okay great, but how do I run the test?

To automate the tests, all you have to do is go into package.json and add "test": "jest". Save the file and run npm test from the command line. Voila! It should search for all .spec and .test files.

To exclude a single test, you may go into the test file and change it() to xit(). To run only a single test or test suite, add .only() to the end of the describe() or it() test you want to run.

What I learned

I learned a lot doing testing for the first time on my application, and I definitely plan to use more of it in the future. It promotes modular programming because you can't really test unless you have functions that handle smaller pieces of logic. Furthermore, it makes you really think about what the edge cases are in your program and how it SHOULD work vs. how it does.

I hope that was helpful,
Thanks for reading,
Andre :)

Top comments (0)