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

106
Views
Jest advanceTimersByTime making test run slow

I am testing a countdown clock in my React app with React Testing Library that renders a message at countdown end, and it seems that my usage of jest.advanceTimersByTime is making the test take between 6 and 10 seconds...

I'm wondering if there is either a way in which I can speed this up, or if there is another alternative for doing the same thing?

import MockDate from 'mockdate';
import {addHours} from "date-fns";
import {act, render, screen} from "@testing-library/react";

describe('my tests', () => {
    beforeAll(() => {
            MockDate.set('2022-01-01');
            jest.useFakeTimers();
        });

        beforeEach(() => {
            const endDate = addHours(new Date(), 1);
            render(<MyAlarm alarm={endDate} />);
        });

        afterEach(() => {
            act(() => {
                jest.runOnlyPendingTimers();
            });
        });

        afterAll(() => {
            jest.useRealTimers();
            MockDate.reset();
        });

        it('Should decrement countdown by 1 second', () => {
            // Initially 01 hour, 00 minutes
            expect(screen.queryAllByText('01')).toHaveLength(1);
            expect(screen.queryAllByText('00')).toHaveLength(1);

            act(() => {
                jest.advanceTimersByTime(1000);
            });

            // Should be 00 hours, 59 minutes
            expect(screen.queryByText('01')).toBeFalsy();
            expect(screen.queryAllByText('00')).toHaveLength(1);
            expect(screen.queryAllByText('59')).toHaveLength(1);
        });

        // Note I have 'fit' to focus on this test solely
        fit('Should display expired message', () => {
            // Initially 01 hour, 00 minutes
            expect(screen.queryAllByText('01')).toHaveLength(1);
            expect(screen.queryAllByText('00')).toHaveLength(1);
            expect(screen.queryByText(/EXPIRED/)).toBeFalsy();

            act(() => {
                jest.advanceTimersByTime(60 * 60 * 1000);
            });

            expect(screen.queryByText(/EXPIRED/)).toBeTruthy();
        });

})

If I run this test suite it will run my expired message test only, and I can see that I get a test pass, however it does seem to suggest that the test takes a while...

PASS src/tests/MyTest.spec.js (6.59s)

Whereas if I run the suite specifying to run the other test only, it does not mention to time taken, suggesting it runs much quicker.

about 4 years ago · Juan Pablo Isaza
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!