Estou tentando escrever um teste unitário para um componente React no meu projeto Next.js usando Jest e React Testing Library. No entanto, estou encontrando o seguinte erro ao executar o teste:
`Testando o componente Home › renderiza um título
The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
Consider using the "jsdom" test environment.
ReferenceError: document is not defined`
Aqui está meu código de teste:
Início.teste.tsx
import { render, screen } from "@testing-library/react";
import Home from "./Home"; // Adjust based on your file structure
describe("Testing Home Component", () => {
it("renders a heading", () => {
render(<Home />);
const text = screen.getByText(/Home/i);
expect(text).toBeInTheDocument();
});
});
Início.tsx
'use client'
import React from 'react'
const Home = () => {
return (
<div>
Home
</div>
)
}
export default Home
Tentei executar um teste Jest para um componente React em um projeto Next.js usando @testing-library/react. O teste renderiza o componente Home e verifica se um título contendo o texto "Home" está presente no documento.
Aqui está o teste que usei:
import { render, screen } from "@testing-library/react";
import Home from "./Home"; // Adjust based on your file structure
describe("Testing Home Component", () => {
it("renders a heading", () => {
render(<Home />);
const text = screen.getByText(/Home/i);
expect(text).toBeInTheDocument();
});
});
Eu esperava que o teste passasse com sucesso, o que significa que o componente Home seria renderizado, e o título com o texto "Home" seria encontrado no documento. No entanto, encontrei o seguinte erro:
ReferenceError: document is not defined