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

195
Visualizações
How do I force timeouts in Jest to test timeout errors?

We have a function in our React project to fetch a list of stores. If the fetch takes longer than 10 seconds we abort the request and show an error.

const controller = new AbortController();
const getTimeout = setTimeout(() => controller.abort(), 10000);

const fetchStores = storeId => (
    ourFetchStoresFunction(`http://my-api/${storeId}`, {
        headers: { 'x-block': 'local-stores' },
        signal: controller.signal
    })
    .then((results) => {
        clearTimeout(getTimeout);
        return results
    })
    .catch((err) => { throw err; })
);

Here is the basic test for the fetchStores function:

it('fetchStores should return a stores array', () => {
    storesAPI.fetchStores(MOCK_STORES)
    .then((stores) => {
        expect(Array.isArray(stores)).toBe(true);
     })
    .catch();
});

How do I mock this timeout in Jest?

using setTimeout in the .then block while calling that method did not work. Additionally, I would rather NOT have to wait 10 seconds during the test. I looked up jest.useFakeTimers but got nowhere.

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

0

I could test global timers, read here: jestjs.io/docs/timer-mocks You can use jest spyOn function. For example:

let timeoutSpy; 
beforeEach(() => { 
  timeoutSpy = jest.spyOn(global, 'setTimeout'); 
}); 
afterEach(() => { 
 jest.restoreAllMocks(); 
})

Then in your test, you can expect(tymepoutSpy).toHaveBeenCalledTimes(1); Something like this should work, worked in my case.

In your case, you can mock about controller too, again using jest.spyOn, could be something like this:

let abortSpy;

In the same beforeEach you can set it:

abortSpy = jest.spyOn(AbortController.prototype, 'abort');

And I suppose ourFetchStoresFunction function is similar to fetch?

And finally, you can mock fetch function, so it takes lots f time to load, it can be done in this way:

> global.fetch = jest.fn(() =>   Promise.resolve({ // set some time })
> );
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