Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

366
Visualizações
How to test the document.documentElement with Jest?

Here is the mini react project: https://codesandbox.io/s/shy-monad-n32vv

I try my best to make it as small as I can. To let tailwindcss open dark mode, we need to add "dark" to the document.documentElement's classList. So the mini project will add or remove (aka, toggle) the class name "dark" to classList after we click the button.

It works really well. But the Jest test failed.

Here is the test file in the mini project:

import { render, fireEvent } from "@testing-library/react";
import App from "./App.js";

test("<App />", () => {
  const { getByText } = render(<App />);
  const darkToggle = getByText("Toggle");
  document.documentElement.classList.remove("dark");
  expect(document.documentElement.classList.contains("dark")).toBe(false);

  fireEvent.click(darkToggle);
  expect(document.documentElement.classList.contains("dark")).toBe(true);
  fireEvent.click(darkToggle);

  // it failed here!!!!
  expect(document.documentElement.classList.contains("dark")).toBe(false);

  fireEvent.click(darkToggle);
  expect(document.documentElement.classList.contains("dark")).toBe(true);
});

I read a lot but cannot find something useful. Mocking DOM by JSDOM looks do not work -- Looks like that the new version Jest's default env is JSDOM. I also failed to test localStorage(I remove those in the mini project), I hope those failed because the same bug.

My guess: after use Redux, the test failed. I think the test failed because of createAsyncThunk. But I still have no idea.

EDITED: I find this will help: https://stackoverflow.com/a/51045733/13031497

EDITED: I resolve it. But looks like the codesandbox still raise error?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After try, I find await new Promise(process.nextTick); does well.

I think because createAsyncThunk's function will change the data after test -- really bad. If there is some function that can sleep some time after the data has already changed, it will be the BEST!!! I find this page: https://stackoverflow.com/a/51045733/13031497, it tell my change the test file like:

  import { render, fireEvent } from "@testing-library/react";
  import App from "./App.js";
  
  test("<App />", () => {
    const { getByText } = render(<App />);
    const darkToggle = getByText("Toggle");
    document.documentElement.classList.remove("dark");
    expect(document.documentElement.classList.contains("dark")).toBe(false);
  
    fireEvent.click(darkToggle);
 +  await new Promise(process.nextTick);
    expect(document.documentElement.classList.contains("dark")).toBe(true);
 +
    fireEvent.click(darkToggle);
- 
-   // it failed here!!!!
 +  await new Promise(process.nextTick);
    expect(document.documentElement.classList.contains("dark")).toBe(false);
  
    fireEvent.click(darkToggle);
 +  await new Promise(process.nextTick);
    expect(document.documentElement.classList.contains("dark")).toBe(true);
  });

Then the test pass.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda