Skip to content

Instantly share code, notes, and snippets.

@josevargasweb
Last active February 8, 2025 19:02
Show Gist options
  • Save josevargasweb/b69d964ce2f0048706c1b0c84e22fbba to your computer and use it in GitHub Desktop.
Save josevargasweb/b69d964ce2f0048706c1b0c84e22fbba to your computer and use it in GitHub Desktop.

Instalación y configuracion de vitest + React Testing Library + JSDOM

En proyectos de React + Typescript + Vite

  1. Instalaciones:
npm install --save-dev vitest
npm install --save-dev @testing-library/react @testing-library/dom @types/react @testing-library/jest-dom @testing-library/user-event  @types/react-dom
npm install --save-dev jsdom
  1. Actualizar los scripts del package.json
"scripts: {
  ...
  "test": "vitest -u"
  1. Crear la configuración de vitest para prevenir efectos secundarios entre pruebas setup.ts esta puede ir en una carpeta raiz llamada /tests
import { afterAll } from "vitest";
import { cleanup } from "@testing-library/react";
import '@testing-library/jest-dom';

afterAll(() => {
  cleanup();
})
  1. vitest config y setup: vite.config.ts
/// <reference types="vitest/config" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "./tests/setup.ts",
  },
})

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