Testing with Jest
A very large part of a developer’s day involves writing code that needs to be tested in order to ensure its accuracy. It should go without saying that you want your code to work properly and do its intended job. However when working on large production projects that are different from a much smaller personal project, testing needs to occur to ensure that the updates and changes made to the code base will properly function before pushing the final production code.
When working on smaller scale personal projects for portfolios or for fun, you can usually get away with error-driven development to test that your vision is properly coming to life. In fact, it is arguably considered the best practice when creating something from scratch. However, if you are inheriting a project, working with a lot of legacy code, converting frameworks, or trying to implement a new feature on a project at work, testing with a testing framework is usually the way to go. This way you can make sure that your new implementation is working up to par before pushing into production code.
This is where Jest comes in handy!
Jest is a Javascript testing framework that is proven to ensure a correct codebase, and it’s very easy to use/quick to learn. Not to mention it is used by thousands of companies, including big names like Twitter, Spotify, Facebook, Airbnb, and many more.
You can use Jest to structure, create, or run tests. It was created by Facebook, so it should come to no surprise that it is often used with React based applications. Aside from React, it is also used in conjunction with Node, Angular, Babel, Vue, and of course… Javascript!
Jest is easy to integrate, as well, as it comes as a yarn or npm package and can be installed in any Javascript project. With it, you can write unit tests, UI tests, or integration tests. Generally, you will need to write tests for two main reasons: you are inheriting legacy code and need to test that, or you need to implement a new functionality. For me, I’m learning it for these reasons (plus I wasn’t taught how to write tests in my bootcamp and I know it’s important for my job hunt 🙂).
How do I install it?
Like we discussed earlier, Jest is super easy to install. First, you need to navigate into the directory of your project you would like to test. Once in that directory, type the command below in your command line.
you can install it with yarn
yarn add --dev jestcan also be installed with npm
npm i jest --save-dev
This will install Jest into your project. Something to remember is that the Jest documentation mainly uses yarn commands, but npm commands work, as well.
Then, add the following code into your package.json:
{"scripts": {"test": "jest"}}
This code will make sure you can run the tests in your command line.
How do I write a basic test?
Setting up a test is fairly simple, but you will need to create two new files. The first will be a file that has the testable code, and then the second file will contain the actual test requirements containing the expected outcome.
Using the Jest docs for reference, let’s set up a basic function that multiplies two numbers. First, create a multiply.js file. In this file, write a function that takes in two arguments, and returns the two numbers multiplied.
function multiply(a, b) {return a * b;}module.exports = multiply;
Next, create a multiply.test.js file that will hold the expected outcome and the test file.
const multiply = require('./multiply');test('multiplies 1 * 2 to equal 2', () => {expect(multiply(1, 2)).toBe(2);});
And that’s it! You have written your first Jest test. There are many other fun things to do with Jest, … but we will save that for another time :) Feel free to go through the Jest docs as listed below, and learn more for yourself!