Skip to content

Instantly share code, notes, and snippets.

@nlarrosa
Last active January 16, 2024 16:57
Show Gist options
  • Save nlarrosa/022ff39d51b62d2f2d5cbd86c4b75f27 to your computer and use it in GitHub Desktop.
Save nlarrosa/022ff39d51b62d2f2d5cbd86c4b75f27 to your computer and use it in GitHub Desktop.

Instalación de Jest + React Testing Library

En proyectos de React + Vite

  1. Instalaciones YARN / NPM:
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react 
yarn add --dev @testing-library/react @types/jest jest-environment-jsdom
npm i --save-dev jest babel-jest @babel/preset-env @babel/preset-react 
npm i --save-dev @testing-library/react @types/jest jest-environment-jsdom
  1. Opcional: Si usamos Fetch API en el proyecto:
yarn add --dev whatwg-fetch
npm i  --save-dev whatwg-fetch
  1. Actualizar los scripts del package.json
"scripts: {
  ...
  "test": "jest --watchAll"
  1. Crear la configuración de babel: babel.config.cjs
module.exports = {
    presets: [
        [ '@babel/preset-env', { targets: { esmodules: true } } ],
        [ '@babel/preset-react', { runtime: 'automatic' } ],
    ],
};
  1. Opcional, pero eventualmente necesario, crear Jest config y setup: jest.config.cjs
module.exports = {
    testEnvironment: 'jest-environment-jsdom',
    setupFiles: ['./jest.setup.js']
}
  1. Solo en caso de necesitar la implementación del FetchAPI: jest.setup.js
import 'whatwg-fetch'; // <-- yarn add whatwg-fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment