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

367
Views
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 answers
Answer question

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 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!