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

230
Visualizações
Fastify inject test with jest timing out when sending text/plain body

I have the following basic fastify server:

import Fastify from 'fastify';
import expressPlugin from 'fastify-express';
import cors from 'cors';


async function build() {
  const fastify = Fastify();

  await fastify.register(expressPlugin);
  fastify.use(cors());

  fastify.post(
    '/someurl',
    async (request, reply) => {
      console.log('PATATAPATATA');
      void reply.send({message: "Hey, I'm stuck :D"})
    }
  );
   
  return fastify;
}

And then I have the following test:

  it.only('Should reply with a 200 status code when sending a string body', async () => {
    // after this line, jest gives a timeout (5000 ms)
    const response = await getResponse({
      headers: {
        Accept: 'application/json',
        'Content-Type': 'text/plain',
        'Content-Length': 1,
      },
      payload: 'a',
    });

    expect(response.statusCode).toEqual(200);
  });

  async function getResponse(options?: InjectOptions) {
    const app = await build();
    return app.inject({
      method: 'POST',
      url: `/someurl`,
      ...options,
    });
  }

The content-length header should not be needed, but I have added it after trying different things to fix it. If you delete it you get the timeout anyway. Any idea why this is happening? If I delete the content-type header then I get a 415 http error.

Side note: the code console.log('PATATAPATATA'); is never executed, so the problem has to be in some fastify internals or the light my request module.

Update It seems this only happens when I use payload in the inject options, like this:

  async function getResponse(options?: InjectOptions) {
    const app = await build();
    return app.inject({
      method: 'POST',
      url: `/someurl`,
      payload: {},
    });
  }

I have also discovered that:

  • Using payload: '' works.
  • Using payload: '{}' does not work. It does not give a timeout, but it does not run the post method either.
  • Using payload: '{}' and content-type set to application/json or text/plain, also gives the timeout.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems fastify-express it's not compatible with inject -> https://github.com/fastify/fastify/issues/3739

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