Quisiera ayuda para resolver este error.
Estoy usando Jest V28.0.0.
Así es como se ven el script de prueba y las devDependencies de mi paquete.json
"scripts":{ ..., "test": "jest --env=node --watchAll --coverage --verbose", }, "devDependencies": { ... "@babel/preset-env": "^7.16.11", "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.0", "@types/jest": "^27.4.1", "babel-jest": "^28.0.0", "jest": "^28.0.0", "jest-environment-jsdom": "^28.0.1", "jsdom": "^19.0.0", } }También tengo un archivo jest.config.js que se ve así
module.exports = { roots: ['<rootDir>/tests/'], testEnvironment: 'jsdom', testMatch: ['**/?(*.)+(test).js'], transform: { '^.+\\.js?$': 'babel-jest', }, moduleNameMapper: { ... }, }Mi archivo de prueba real se ve así
import React from 'react' import { render, cleanup, screen } from '@testing-library/react' import renderer from 'react-test-renderer' import '@testing-library/jest-dom' /** * @jest-environment jsdom */ //components import MyComponent from '../../../../src/website/Components/MyComponent' test('should have the Add recipients text label', () => { const addRecipientsLabel = screen.getByTestId('label-element') expect(true).toBe(true) render(<MyComponent />) expect(addRecipientsLabel).toHaveTextContent('Add Recipients') })Debe incluir @jest-environment en la parte superior del archivo. Por encima incluso de las importaciones.
https://jestjs.io/docs/configuration#testenvironment-string
P.ej.:
/** * @jest-environment jsdom */ import React from 'react' //components import MyComponent from '...' test('should have the Add recipients text label', () => { // test })