

Code testing with Jest
source link: https://dev.to/menghif/code-testing-with-jest-3b4h
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.

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 source projects.
How to setup Jest
I started by installing Jest using npm
npm install --save-dev jest
then I added "test": "jest"
to the "scripts"
section of my package.json
to be able to run tests with the simple command:
npm test
Before getting into writing tests, I had to tackle a problem that I have been putting to the side: having a long code all in one file. I spent a lot of time refactoring to move some logic into different files and export those as modules.
Here is an example of a test I wrote to make sure that the correct html is returned by the writeHTML
function:
test("writeHTML with Title, Body and Stylesheet should return correct html", () => {
expect(writeHTML("Title", "Body", "Stylesheet")).toBe(
`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="Stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/styles/github.css">
</head>
<body>
<h1>Title</h1>
Body
</body>
</html>`
);
});
Thoughts
I only scratched the surface of unit testing but I do find the process of writing tests pretty complicated, especially when it comes to creating Mock functions. I will need to get more familiar with Jest in the future and I have a lot more tests to add to the project.
Recommend
-
17
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...
-
8
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 e...
-
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