- Instalaciones:
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react
yarn add --dev @testing-library/react @types/jest jest-environment-jsdom
- Opcional: Si usamos Fetch API en el proyecto:
yarn add --dev whatwg-fetch
- Actualizar los scripts del package.json
"scripts: {
...
"test": "jest --watchAll"
- Crear la configuración de babel babel.config.js
module.exports = {
presets: [
[ '@babel/preset-env', { targets: { esmodules: true } } ],
[ '@babel/preset-react', { runtime: 'automatic' } ],
],
};
- 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
en mi caso me funcionó creando como babel.config.cjs, jest.config.cjs, jest.setep.js. Además, tuve unos problemas al hacer testing, pues no reconocía el css. Se solucionó al colocar en el jest.config.cjs
module.exports={
testEnvironment:'jest-environment-jsdom',
setupFiles:['./jest.setup.js']
,moduleNameMapper: {
'\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'/mocks/fileMock.js',
'\.(css|less)$': 'identity-obj-proxy',
}
}
Por todo lo demás, excelente Fernando!