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

143
Visualizações
Javascript Mocking functions that return Readable

I have a simple function that calls my s3 service which will return a Promise

  downloadFile(fileName: string): Promise<Readable> {
    console.log(
      "Downloading an object with key - ",
      fileName
    );

    return this.s3service.getObject(fileName);
  }

I would like to mock the return of the object and assert it in the test like this:

it("should return readable if file present", async () => {
  const readable = new Readable();
  readable.push("test");

  jest
    .spyOn(s3service, "getObject")
    .mockImplementation(() => Promise.resolve(readable));

  const file = await someService.downloadFile(
    TestConstants.reportName
  );

  expect(file).toBe(readable);
});

When i run the test, I face this error:

Error [ERR_METHOD_NOT_IMPLEMENTED]: The _read() method is not implemented at Readable.read (internal/streams/readable.js:642:9) at Readable.read (internal/streams/readable.js:481:10) at maybeReadMore (internal/streams/readable.js:629:12) at processTicksAndRejections (internal/process/task_queues.js:82:21) { code: 'ERR_METHOD_NOT_IMPLEMENTED'

Would like some help on how I can properly mock a readable for example in this example

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

0

Your production code just forward response from the service to outside, then just mock getObject to return anything, and expect the result should be getObject result and the service function will have called with correct parameter:

it("should return readable if file present", async () => {
  const readable = "readable-object" as unknown as Readable;
  jest.spyOn(s3service, "getObject").mockResolvedValue(readable);

  const actual = await someService.downloadFile(TestConstants.reportName);

  expect(actual).toBe(readable);
  expect(s3service.getObject).toHaveBeenCalledWith(TestConstants.reportName);
});
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