Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
¿Llamar a jest.clearAllMocks() antes de que cada uno borre las llamadas dentro de una prueba?

Veo un problema en Jest en el que llamar a jest.clearAllMocks() dentro de una devolución de llamada beforeEach parece eliminar las llamadas a una función simulada que no se realizan antes sino DENTRO de una prueba específica. Puedo reproducirlo de la siguiente manera:

ESTO PASA:

__tests__/testy.test.js

 const AWS = require("aws-sdk"); const { handler } = require("../index.js"); describe("Lambda function tests", () => { it("sent the expected CSV data to S3", async () => { // RUN THE LAMBDA, await completion await handler(); // The S3 constructor should have been called during Lambda execution expect(AWS.S3).toHaveBeenCalled(); }); });

ESTO ME DA UN FALLO:

__tests__/testy.test.js

 const AWS = require("aws-sdk"); const { handler } = require("../index.js"); describe("Lambda function tests", () => { beforeEach(() => { jest.clearAllMocks(); }); it("sent the expected CSV data to S3", async () => { // RUN THE LAMBDA, await completion await handler(); // The S3 constructor should have been called during Lambda execution expect(AWS.S3).toHaveBeenCalled(); }); });

El mensaje de error en la consola:

 expect(jest.fn()).toHaveBeenCalled() Expected number of calls: >= 1 Received number of calls: 0

Estoy usando la versión 27.4.3 de jest en Node v14.18.1. En caso de que importe, me estoy burlando aws-sdk así:

__mocks__/aws-sdk.js

 const AWS = jest.createMockFromModule("aws-sdk"); AWS.S3 = jest.fn().mockImplementation(() => ({ upload: jest.fn(() => ({ promise: jest.fn() })), headObject: jest.fn(() => ({ promise: jest.fn(() => ({ ContentLength: 123 })), })), })); module.exports = AWS;

¿Qué podría estar pasando aquí?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Resolví esto reestructurando mi código principal. Anteriormente, estaba instanciando S3 fuera de la función del handler :

index.js

 const AWS = require("aws-sdk"); const S3 = new AWS.S3(); exports.handler = async () => { S3.upload({ ... }); }

Ahora lo estoy haciendo de esta manera, y el problema está resuelto:

index.js

 const AWS = require("aws-sdk"); exports.handler = async () => { const S3 = new AWS.S3(); S3.upload({ ... }); }

En retrospectiva, tiene sentido. index.js se ejecuta antes de que se ejecute beforeEach , no cada vez que se ejecuta una prueba.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!