- jest: testing framework
- @testing-library/react: provides utilities for testing React components with simulated user interaction
- @testing-library/jest-dom: provides custom matchers for Jest that allows testing DOM node states
- @babel/preset-env: Babel preset that compiles "fancy" modern JS down to "standard JS" (in order to be compatible with older browsers)
- @babel/preset-react: Babel preset specifically for compiling React-specific code, such as JSX
- babel-jest: Jest transformer that uses Babel to compile JS (this allows Jest to 'understand' JSX and ESM syntax)
- jest-environment-jsdom: Jest environment that simulates a browser-like environment using jsdom, allowing the DOM to be tested in a Node environment (i.e. without a browser)
- identity-obj-proxy: a proxy for CSS modules that allows Jest to mock CSS imports as plain objects, making it easier to test components without loading styles (this prevents errors - you can turn it off in
jest.config.cjs
if you're curious what these errors look like)
- Create vite react template:
npx create-vite@latest my-react-app --template react
- Navigate into your new Vite React app with
cd
and rungit init
to create agit
repo - Install the dependencies:
npm install --save-dev jest @testing-library/react @testing-library/jest-dom @babel/preset-env @babel/preset-react babel-jest jest-environment-jsdom identity-obj-proxy
- Create Babel config in project root:
touch babel.config.cjs
NOTE: this file uses .cjs to denote that it is a CommonJS module, otherwise Vite will throw an error when it tries to treat it as ES module syntax
- Setup Babel config:
//babel.config.cjs
module.exports = {
presets: ["@babel/preset-env", "@babel/preset-react"],
};
- Create a
__mocks__
directory in project root:
mkdir __mocks__
- Create simple SVG Transformer in the
__mocks__
directory:
touch svgTransform.js
Note: this makes it so that SVGs (a common image type) don't mess with Jest's tests - we will integrate it in the upcoming steps
- Setup
svgTransform.js
:
//svgTransform.js
module.exports = {
process() {
return { code: "module.exports = {};" };
},
getCacheKey() {
return "svgTransform";
},
};
- Create Jest config in project root:
touch jest.config.cjs
NOTE: this file uses .cjs to denote that it is a CommonJS module, otherwise Vite will throw an error when it tries to treat it as ES module syntax
- Setup Jest config:
//jest.config.cjs
module.exports = {
testEnvironment: "jsdom",
transform: {
"^.+\\.jsx?$": "babel-jest",
},
moduleNameMapper: {
"\\.(css|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy",
"\\.svg$": "<rootDir>/__mocks__/svgTransform.js",
},
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
};
Note: this tells jest how to transform certain sorts of code and modules, as well as what to do after the testing environment is setup (in this case, run the jest setup file that we're about to create)
- Create Jest setup file in project root:
touch jest.setup.js
- Setup Jest setup file:
// jest.setup.js
import "@testing-library/jest-dom/";
Note: this makes it so we can use the Testing Library methods in Jest tests
- Add
test
script to the other scripts in existingpackage.json
:
//package.json
...
"scripts": {
...
"test": "jest --watchAll"
},
...
- Create
tests
directory (if desired) - Create your first test (follow traditional Jest naming specifications)