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

479
Views
Uso de spyOn con Jest para simular una llamada de publicación de AWS SNS

Estoy tratando de usar spyOn en Jest para simular una llamada de publish de AWS SNS. No estoy bien versado en el ecosistema de AWS, ni tengo mucha experiencia escribiendo pruebas de Jest, así que estoy un poco atascado. En el siguiente código de prueba, cuando se llama a spyOn para el método de colocación de DynamoDB put no recibo ningún error, pero cuando se llama para el método de publish de SNS, recibo el siguiente error. La mayor parte de este código fue escrito por otra persona, y estoy tratando de seguir la misma plantilla para el simulacro de publish .

 const lambda = require('../../../src/handlers/sign-up.js'); const dynamodb = require('aws-sdk/clients/dynamodb'); const AWS = require("aws-sdk"); AWS.config.update({region: 'us-east-1'}) const sns = new AWS.SNS(); const allowOrigin = process.env.ALLOW_ORIGIN describe('Test signUpHandler', function () { let putSpy; let publishSpy; beforeAll(() => { putSpy = jest.spyOn(dynamodb.DocumentClient.prototype, 'put'); publishSpy = jest.spyOn(sns.prototype, 'publish') }); afterAll(() => { publishSpy.mockRestore(); putSpy.mockRestore(); }); it('should add email to the table', async () => { const returnedItem = { email: 'test@example.com' }; putSpy.mockReturnValue({ promise: () => Promise.resolve(returnedItem) }); const event = { httpMethod: 'POST', headers: { origin: allowOrigin }, body: '{"email": "test@example.com"}' }; const result = await lambda.signUpHandler(event); const expectedResult = { statusCode: 200, headers: {"Access-Control-Allow-Origin": allowOrigin}, body: JSON.stringify({"success": true}) }; expect(result).toEqual(expectedResult); }); });

Cuando ejecuto la prueba, me sale este error:

 Cannot spyOn on a primitive value; undefined given 20 | putSpy = jest.spyOn(dynamodb.DocumentClient.prototype, 'put'); > 21 | publishSpy = jest.spyOn(sns.prototype, 'publish') | ^ 22 | });

¿Me estoy acercando a esto mal? Cualquier ayuda sería muy apreciada.

over 4 years ago · Santiago Trujillo
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!