After a bit of a break (I didn’t have time with work), I have finally managed to write a new article. Today, I want to explain some simple unit testing, along with the framework we use in my workplace, Jest!
What is Unit Testing?
Unit testing allows you to test the component parts of your application in order to map, check, and enforce expected behaviours, prior to “putting them together” in order to make your application – we are looking at JavaScript unit testing, within the context of a web application.
Project setup
We are creating our project using yarn here, although you can use pnpm or npm here just as well with some minor tweaks. Set up your node project as you normally would, and run1:
yarn add -D jest jest-environment-jsdom @jest/globals
yarn jest --init
This will allow you to set up your jest project. The questions are fairly straightforward, so answer these as you need to fit your project – the only option you need to be concerned with more than the others is when you choose the test environment, choose jsdom (browser-like). I also use babel rather than v8 provider for coverage, but that’s down to personal preference.
Babel setup
We also need to do some setup for Babel as well – firstly, we need to add the relevant dependencies to our project using
yarn add -D babel-jest @babel/core @babel/preset-env
We then need to create the configuration file babel.config.js
module.exports = {<br> presets: [['@babel/preset-env',{target: {node: 'current'}}]],<br>};
Now that we’ve set up our project, and as this is quite a long process, I’ll continue in part 2.
- [edit] I realised I missed off the @jest/globals here – sorry people, that’s now been fixed! ↩︎

