# Instalación y configuracion con **npm** de Jest + React Testing Library ## En proyectos de React + Vite 1. Instalaciones: ``` npm add --dev jest babel-jest @babel/preset-env @babel/preset-react npm add --dev @testing-library/react @types/jest jest-environment-jsdom ``` 2. Opcional: Si usamos Fetch API en el proyecto: ``` npm add --dev whatwg-fetch ``` 3. Actualizar los scripts del __package.json__ ``` "scripts: { ... "test": "jest --watchAll" ``` 4. Crear la configuración de babel __babel.config.js__ Si no existe el archivo crearlo en la raiz ``` module.exports = { presets: [ [ '@babel/preset-env', { targets: { esmodules: true } } ], [ '@babel/preset-react', { runtime: 'automatic' } ], ], }; ``` 5. Opcional, pero eventualmente necesario, crear Jest config y setup: Si no existe el archivo crearlo en la raiz __jest.config.js__ ``` module.exports = { testEnvironment: 'jest-environment-jsdom', setupFiles: ['./jest.setup.js'] } ``` __jest.setup.js__ ``` // En caso de necesitar la implementación del FetchAPI import 'whatwg-fetch'; // <-- yarn add whatwg-fetch ``` 6. Ejecutar pruebas, en la terminal ``` npm test ``` Si da error en __package.json__ ``` { "name": "gif-expert-app", "private": true, "version": "0.0.0", "type": "commonjs", ``` cambiar por: ``` { "name": "gif-expert-app", "private": true, "version": "0.0.0", "type": "modulejs", ``` # Instalación y configuracion con **yarn** de Jest + React Testing Library ## En proyectos de React + Vite 1. Instalaciones: ``` yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react yarn add --dev @testing-library/react @types/jest jest-environment-jsdom ``` 2. Opcional: Si usamos Fetch API en el proyecto: ``` yarn add --dev whatwg-fetch ``` 3. Actualizar los scripts del __package.json__ ``` "scripts: { ... "test": "jest --watchAll" ``` 4. Crear la configuración de babel __babel.config.js__ ``` module.exports = { presets: [ [ '@babel/preset-env', { targets: { esmodules: true } } ], [ '@babel/preset-react', { runtime: 'automatic' } ], ], }; ``` 5. Opcional, pero eventualmente necesario, crear Jest config y setup: __jest.config.js__ ``` module.exports = { testEnvironment: 'jest-environment-jsdom', setupFiles: ['./jest.setup.js'] } ``` __jest.setup.js__ ``` // En caso de necesitar la implementación del FetchAPI import 'whatwg-fetch'; // <-- yarn add whatwg-fetch ```