Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Considere usar el entorno de prueba "jsdom"

Tengo esta prueba simple:

 import React from 'react' import { render } from '@testing-library/react' import Button from '.' describe('Button', () => { it('renders button without crashing', () => { const label = 'test' render(<Button label={label} />) }) })

Y tengo un jest.config.json con este contenido

 { "setupFilesAfterEnv": [ "<rootDir>/lib/settings/setupTests.ts" ] }

Y en mi setupTests.ts tengo

 import '@testing-library/jest-dom'

Cuando ejecuto npm run test (que solo ejecuta jest ), recibí el siguiente error:

El siguiente error puede deberse al uso de un entorno de prueba incorrecto, consulte https://jestjs.io/docs/configuration#testenvironment-string .

Considere usar el entorno de prueba "jsdom".

¿Que estoy haciendo mal? Esto solía funcionar antes de una actualización.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

En su package.json , o jest.config.js / jest.config.js , cambie el valor de la propiedad testEnvironment a jsdom .

Paquete.json

 "jest":{ "testEnvironment": "jsdom" }

broma.config.[js|ts]

 { "testEnvironment": "jsdom" }

¿Por qué?

Por defecto, jest usa el node testEnvironment. Básicamente, esto hace que cualquier prueba destinada a un entorno de navegador no sea válida.

jsdom es una implementación de un entorno de navegador que admite este tipo de pruebas de interfaz de usuario.

Lectura adicional

jest testEnvironment documentación

over 4 years ago · Santiago Trujillo Report

0

Esto se puede resolver por archivo de prueba agregando un docblock @jest-environment al comienzo de su archivo. Por ejemplo:

 /** @jest-environment jsdom */ import React from 'react' import { render } from '@testing-library/react' import Button from '.' describe('Button', () => { it('renders button without crashing', () => { const label = 'test' render(<Button label={label} />) }) })

Si su proyecto tiene una combinación de archivos UI y no UI, a menudo es preferible cambiar todo el proyecto configurando "testEnvironment": "jsdom" dentro de su paquete.json o Jest config. Al omitir la inicialización del entorno JSDom para pruebas que no son de interfaz de usuario, Jest puede ejecutar sus pruebas más rápido. De hecho, es por eso que Jest cambió el entorno de prueba predeterminado en Jest 27 .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!