Closed DevonGifford closed 10 months ago
This pull request sets up Jest and React Testing Library (RTL) for unit and integration testing in our application. The goal is to ensure that individual components and their interactions are working as expected.
Install Jest and related libraries: Install jest
, jest-environment-jsdom
, @testing-library/react
, and @testing-library/jest-dom
. These libraries are essential for setting up Jest with Next.js and React.
Create Jest Configuration File: Create a jest.config.mjs
file in your project’s root directory. This file is used to configure Jest’s behaviour.
Update ESLint
Configuration: Update your .eslintrc.json
file to include the newly added plugins for Jest and Testing Library.
Currently using version 5.16.5 of @testing-library/jest-dom
, which is an older stable version that works well with Next.js, especially the latest version
It’s worth noting that there are no known vulnerabilities for this version of @testing-library/jest-dom
.
However, as with any library, it’s important to keep an eye on the official GitHub repository for any open issues or updates.
Naming Tests:
Test files should be named in a way that clearly describes what they are testing. For example, if you have a component named MyComponent
, the test file could be named MyComponent.test.js
.
Saving Tests:
Test files should be saved in a __tests__
directory. This makes it easy to locate them and keeps your project organized.
The environment has been set up to accommodate both Server-Side Rendered (SSR) and Client-Side Rendered (CSR) components:
// Setup for testing client side rendered components
const clientTestConfig = {
...sharedConfig,
testEnvironment: 'jest-environment-jsdom',
testMatch: ['**/__tests__/*components.test.tsx','**/__tests__/page-*']
};
// Setup for testing server side rendered components
const serverTestConfig = {
...sharedConfig,
testEnvironment: 'jest-environment-node',
testMatch: ['**/__tests__/*.server.test.tsx']
};
Server-Side Rendered (SSR) Components:
To test SSR components, you can use the render
function from @testing-library/react
. This function renders your component in a way that simulates how it would be rendered on a server.
Client-Side Rendered (CSR) Components:
To test CSR components, you can use the render
function from @testing-library/react
. However, since CSR components may rely on browser APIs, you might need to use a library like jest-environment-jsdom
to provide those APIs in your testing environment.
The naming convention of your test files affects how Jest identifies and runs your tests. In the configuration above, the testMatch
option is used to specify the patterns Jest uses to locate test files.
For CSR components, Jest looks for test files in the __tests__
directory with a filename that ends with components.test.tsx
or starts with page-
.
For SSR components, Jest looks for test files in the __tests__
directory with a filename that ends with .server.test.tsx
.
By following these naming conventions, you ensure that Jest correctly identifies and runs your tests based on whether they’re for CSR or SSR components.
Description
The objective is to establish a robust testing environment using Jest, configure folder structures, and set up necessary config files. This task centers on creating a standardized testing setup, ensuring the environment is correctly configured for efficient testing within the application.
Actionable Items
Testing Considerations