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

169
Visualizações
How to test retries to an API call using testcafe

I am using Testcafe for my integration tests, and I want to test the scenario where my app retries an API call after receiving an error. I am using the async-retry library to make my calls. Retry is a utility I created to wrap the API call so I could wrap boilerplate code for calling async-retry:

 const response = await Retry(
        () => {
            return fetch(
                buildUrl(env, csrf, '/api/myCustomCall', queryParams),
                options
            );
        },
        'getRecommendations',
        {
            onRetry: () => {
                console.log('RETRYING');
            }
        }
    );

For posterity, this is the Retry utility:

import retry, { AsyncRetryOptions } from 'async-retry';

export const Retry = (
    func: () => Promise<any>,
    name: string,
    opts: AsyncRetryOptions = {}
): Promise<any> => {
    const retryOptions = {
        retries: opts.retries || 3,
        factor: opts.factor || 2,
        minTimeout: opts.minTimeout || 3000,
        maxTimeout: opts.maxTimeout || Infinity,
        randomize: opts.randomize || true,
        onRetry: (error: Error, attempt: number) => {
            console.error(
                `${new Date().toString()} - ${name} failed ${attempt} times, trying again`
            );
        }
    };

    return retry(func, retryOptions);
};

This is my test:

test.requestHooks(
    RequestMock()
        .onRequestTo(/myCustomCall/)
        .respond({ error: 'Bad value for request parameter' }, 400, responseHeaders)
)('Recommendation request retries 3 times', async (t) => {
    await playgroundInit(t);
    await t.expect(recommendationLogger.requests.length).eql(4);
});

playgroundInit is a utility function that does things like login and navigating to the page I am testing. When I was developing, I used the Chrome devtools to block the API request in order to test the retries, which was successful. I saw the retries working. However, I'd like to mimic this in my test to automate this behavior. How do you mock a request in testcafe to trigger retries?

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

0

As I understand it, you are making your API call from the TestCafe test code, but not from the tested page.

In this case, the RequestMock mechanism will not be applied. RequestMock works from API calls that were made from the website page. In your case, you initiate a request yourself, so the TestCafe proxy does not process it.

If you wish to make RequestMock work you can try making your API calls from the browser but not test side. To do this, you can use the ClientFunction mechanism that allows you to inject JS code to the website page.

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