Skip to content

Instantly share code, notes, and snippets.

@JosePascualGomez
Forked from Klerith/vite-testing-config.md
Last active November 13, 2022 01:28
Show Gist options
  • Save JosePascualGomez/1a5418ab01682d72dc18caccea669e79 to your computer and use it in GitHub Desktop.
Save JosePascualGomez/1a5418ab01682d72dc18caccea669e79 to your computer and use it in GitHub Desktop.
Vite + Jest + React Testing Library - Configuraciones a seguir

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
  1. Opcional: Si usamos Fetch API en el proyecto:
npm add --dev whatwg-fetch
  1. Actualizar los scripts del package.json
"scripts: {
  ...
  "test": "jest --watchAll"
  1. 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' } ],
    ],
};
  1. 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
  1. 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
  1. Opcional: Si usamos Fetch API en el proyecto:
yarn add --dev whatwg-fetch
  1. Actualizar los scripts del package.json
"scripts: {
  ...
  "test": "jest --watchAll"
  1. Crear la configuración de babel babel.config.js
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.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment