Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda