Skip to content

Instantly share code, notes, and snippets.

@CarmeloRicarte
Forked from Klerith/vite-testing-config.md
Created November 3, 2022 08:14
Show Gist options
  • Save CarmeloRicarte/539e0f3467b2f7ef92a14e6f1201e630 to your computer and use it in GitHub Desktop.
Save CarmeloRicarte/539e0f3467b2f7ef92a14e6f1201e630 to your computer and use it in GitHub Desktop.
Vite + Jest + React Testing Library - Configuraciones a seguir

Instalación y configuracion 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
@CarmeloRicarte
Copy link
Author

CarmeloRicarte commented Nov 3, 2022

modificar el nombre de los archivos por cjs para evitar este error: You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment