

Automated testing with Jest 🤖
source link: https://dev.to/mqnguyen/automated-testing-with-jest-4745
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
Managing code complexity can be difficult. I mean, you already added comments inside your codebase, wrote detailed documentation, and even set up static analysis tooling to keep your consistently formatted. But even so, somehow, your code still breaks. Human mistake? Bugs? It is hard to know. You can try adding safety nets to handle the errors, but can you even catch all of them?
Luckily, we have tests. Testing allows developers to prevent their existing code from breaking, identifies bugs, and tracks easy to miss edge cases. The added extra layer of protection ensures that your project, small or large, will evolve in the right direction when new changes are introduced.
What is Jest?
Since I wrote my project in JavaScript, I can choose between Mocha and Jest. In the end, I decided to go with the later option, as I have a bit of experience with it before.
A quick overview: Jest is a JavaScript testing framework that focuses on simplicity. It allows you to write tests and test suites in files ending with .spec.js
or .test.js
.
To begin, start by installing Jest using npm:
npm install --save-dev jest
Then, create a file with the ending as mentioned above to begin testing.
Writing tests
Because my project is a static site generator, I'll be testing the parsing logic that converts text to HTML.
First, I started by exporting and importing the necessary functions:
-
generate-html.js
module.exports = { generateHtmlBody, ... }
-
generate-html.test.js
const { generateHtmlBody, ... } = require('./generate-html')
With everything up and ready to go, I began thinking of how I should test the logic. Here are some example scenarios:
Checking if the paragraphs are wrapped in
<p>
tags. A paragraph is identified by having one or several consecutive lines of text, followed by a single blank line as a paragraph limit.Checking if the title is wrapped in
<h1>
tags. A title is defined by being the first line followed by 2 blank lines.
Thus, my test should look something like this:
describe('generateHtmlBody tests', () => {
test('returned html paragraph should be wrapped in <p> tag', () => {
const data = 'Hello World!';
const extName = '.txt';
const title = null;
const result = generateHtmlBody(data, extName, title);
expect(result).toMatch(new RegExp(/<p>(.+?)<\/p>/g));
});
test('returned html title should be wrapped in <h1> tag', () => {
const data = 'Hello World!';
const extName = '.txt';
const title = 'a title';
const result = generateHtmlBody(data, extName, title);
expect(result).toMatch(new RegExp(/<h1>(.+?)<\/h1>\n\n/g));
});
});
Running the test
Great! We now have a file that tests for specific functionality, but how do we run it?
With Jest, setting up test scripts was not too difficult. Inside "package.json," add the following scripts:
{
scripts: {
"test": "jest --",
"test:watch": "jest --watch --",
"coverage": "jest --collectCoverage --"
}
}
-
npm test [file]
will run the test on the specified file. -
npm run test:watch [file]
will also run the test but in watch mode, which automatically re-runs when changes are made. -
npm run coverage [file]
will generate a code coverage report.
Conclusion
The experience of writing tests has forced me to challenge my way of thinking. Specifically, I had to abandon the thought of writing good tests every time and accepting that there would always be room for improvement. After all, there is no "done" in programming, only "version 1, 2, 3, and many more."
Free yourself from the need to be perfect. Embrace “better than it was” and continue living in that
Recommend
-
18
Testing Flux Stores without JestThursday, February 19th 2015Flux stores present an interesting unit testing challenge. From
-
8
React Component Testing Guide: Jest and RTLMarch 14th 2021 5
-
11
React is an open-source framework for building reusable UI components and apps. It is used by thousands of developers around the world to create complex and multifaceted a...
-
10
Testing React app with JestIn this post we will look into how to write tests for react application using Jest Jest is open source testing framework built on top of JavaScript. It was majorly designed t...
-
6
Getting started with the Jest Javascript testing framework Testing Typescript with Jest
-
12
Testing Solid.js code beyond jestSo, you started writing an app or library in Solid.js and TypeScript - what an excellent choice - but now you want to unit test everything as fast as possible to avoid re...
-
7
This week I looked at code testing for my Static Site Generator dodo-SSG. I picked Jest for this since it is a very popular unit testing tool used by many open...
-
11
Introduction I find it quite hard to find the right steps when you already have a set of technologies in your project, and as the title goes, my target audience are those who already know how to develop a backend application...
-
4
Why should we write tests for our code? When there are more than one developers making changes actively to the code base, issues and bugs tend to arise. It is also difficult to troubleshoot on who commited the buggy code, or...
-
6
PrerequisitesAn existing React app.You can find the full source code @ GitHub: https://github.com/alexadam/project-templates/tree...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK