Welcome to this tutorial on unit testing in Next.js applications. The goal of this tutorial is to introduce you to the process of writing and running unit tests in a Next.js application.
Throughout this tutorial, you will learn:
- The basics of unit testing
- How to use Jest and React Testing Library to write tests
- How to run tests in your Next.js application
Prerequisites for this tutorial include a basic understanding of JavaScript, React, and Next.js. Familiarity with Jest and React Testing Library would be beneficial but not necessary.
Unit testing is the process of testing individual units of a software application to determine whether they are fit for use. In a Next.js application, a unit could be a function, a component, or even a page.
To write and run unit tests in Next.js, we will use Jest as our testing framework and React Testing Library for rendering components, firing events, etc.
First, install Jest and React Testing Library by running the following command in your terminal:
npm install --save-dev jest @testing-library/react @testing-library/jest-dom
Create a new file in your project root called jest.config.js and add the following code:
module.exports = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
};
Now create a new file called jest.setup.js in your project root and add the following code:
import '@testing-library/jest-dom/extend-expect';
Let's look at a simple component and write some tests for it.
Here's our component, a simple button with a click event:
// Button.js
function Button({ onClick, children }) {
return <button onClick={onClick}>{children}</button>;
}
export default Button;
Here's how we can write a test for this component:
// Button.test.js
import { render, fireEvent } from '@testing-library/react';
import Button from './Button';
test('check button click event', () => {
const handleClick = jest.fn();
const { getByText } = render(<Button onClick={handleClick}>Click me</Button>);
fireEvent.click(getByText('Click me'));
expect(handleClick).toHaveBeenCalledTimes(1);
});
In the above test, we check if the button's click event is working as expected. We use jest.fn()
to create a mock function, and pass it as a prop to the Button component. Then, we simulate a click event on the button and check if the mock function was called.
In this tutorial, you've learned how to write and run unit tests in a Next.js application using Jest and React Testing Library. You've seen how to test a simple component and its event handler.
Next, you could learn more about testing different types of components, such as those with state, props, or lifecycle methods. You could also learn about integration testing and end-to-end testing.
Exercise 1:
Write a test for a component that takes a prop and renders it.
Exercise 2:
Write a test for a component that has a state and changes it on a button click.
Exercise 3:
Write a test for a component that makes an API call.
Solutions for these exercises can be found in the Jest and React Testing Library documentation. Happy testing!