Next.js with Jest + React Testing Library
Tags: react, nextjs, testing
Published: 2023-02-07 19:52:10

I have to admit that Jest with React Testing Library is my favorite way of doing unit testing. Enzyme was never my thing. I love the approach of React Testing Library that tries to make you test like an user, it even helps you detect accessibility issues.

Well what I'm trying to say is that I'm really excited to try to do this integration. As always I'm going to follow the documented published in Next.js site and hope for the best, here we go.

There are different ways to get your Next.js project with Jest and React Testing Library running, first you can just create a project from zero with this command.

`npx create-next-app@latest --example with-jest with-jest-app`

But, I want to try to integrate it to the same project that I have been working on. So according to the documentation I can set up Jest with Rust or Babel.

> Rust is a memory-safe compiled programming language for building high-performance systems. It has the simplicity of high-level languages (Go, Python), but the control of low-level languages (C, Cpp)

If you want to know more about Rust, here is a small [video by Fireship](https://www.youtube.com/watch?v=5C_HPTJg5ek) that explains it. Also the [github](https://github.com/fireship-io/rust-in-100) associated to the video is really good.

> Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments (https://babeljs.io/)

I think I'm going to go with Rust.

> Since the release of Next.js 12, Next.js now has built-in configuration for Jest

So I'm going to install jest, jest-environment-jsdom, @testing-library/react, @testing-library/jest-dom using the following command

`npm install --save-dev jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom`

Then we have to create the following `jest.config.js` file

![jest config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnzme07phcc2nztiyo5e.png)

> Under the hood, next/jest is automatically configuring Jest for you, including:
> - Setting up transform using SWC
> - Auto mocking stylesheets (.css, .module.css, and their scss variants), image imports and @next/font
> - Loading .env (and all variants) into process.env
> - Ignoring node_modules from test resolving and transforms
> - Ignoring .next from test resolving
> - Loading next.config.js for flags that enable SWC transforms

The I'll add a script for testing in the `package.json`

![Package json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n93xj6co4g3usb1oyckd.png)

Following Jest's convention I'll create a `__tests__` folder and add a test to check if the `<Home />` component successfully renders a heading:

![testing home](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqe45zq8zp1o29zepsb2.png)

And I'm going to run the testing script to see if everything is ok.

![npm run test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkl79csl6a01itf48ppl.png)

The documentation also says that I can add snapshots tests, I know that many people don't like them but for I have to admit that more than once snapshots allowed me to catch changes that I didn't made on purpose. So I want to see how to include them now. So I'll create a new file called `snapshot.js`

![snapshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq35d82xtuc7zwoj6mth.png)

And now I run my testing script again

![testing with snapshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0sl6cwuj1kfsl9ku7ww.png)

If you are reading this as part of the serie of [Trying Next.js](https://dev.to/celest67/trying-nextjs-4ee4) you can see that before this I tried [Next.js with Cypress](https://dev.to/celest67/nextjs-with-cypress-3a5b) so I think I'll do something similar and test the same, I'll change my index with a message that reads "This is 0" and a button that everytime you click it the number in the message increments by one. And of course every test fails.

![new index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cafxfhky0yflke4kbgc.png)

So let's update the test file for the index and update the snapshot

![new test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lroxrqi85yyr9iih2gf.png)

And there you have it, tests running OK and I think that is it for now.

Any comments or questions are welcome

Cheers!