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

326
Views
Cómo burlarse del constructor DynamoDBDocumentClient con Jest (AWS SDK V3)

Estoy trabajando con Jest para simular mis servicios de AWS y, más específicamente, DynamoDB y DynamoDBDocumentClient.

Mi código es actualmente similar a este:

 import { DynamoDBClient } from "@aws-sdk/client-dynamodb" import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb" const ddbClient = new DynamoDBClient({}) const ddbDocumentClient = DynamoDBDocumentClient.from(ddbClient, config)

y la especificación de prueba se ve así:

 jest.mock("@aws-sdk/client-dynamodb", () => ({ DynamoDBClient: jest.fn(() => ({ put: (params) => mockAwsResponse("DynamoDBClient", "GetCommand", params), put: (params) => mockAwsResponse("DynamoDBClient", "PutCommand", params), })), })) jest.mock("@aws-sdk/lib-dynamodb", () => ({ DynamoDBDocumentClient: jest.fn(() => ({ get: (params) => console.log("In DynamoDBClient get", params), put: (params) => mockAwsResponse("DynamoDBDocumentClient", "PutCommand", params), from: (params) => mockAwsResponse("DynamoDBDocumentClient", "from", params), })), }))

Desafortunadamente, Jest me devuelve este error: TypeError: lib_dynamodb_1.DynamoDBDocumentClient.from is not a function y creo que es porque me estoy burlando incorrectamente de DynamoDBDocumentClient , ya que su constructor es un método estático.

¡Gracias de antemano por tu ayuda!

EDITAR: Acabo de darme cuenta de que usaba AWS SDK v3 para JavaScript, si eso ayuda.

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

0

En su ejemplo, importa sus módulos y ejecuta la llamada a su constructor de inmediato (antes de ingresar cualquier método en su secuencia de comandos), esto significa que el objeto que está tratando de simular debe estar disponible antes de ingresar cualquier método de prueba. Intente colocar sus simulacros antes (y fuera de) sus pruebas de la siguiente manera:

 import { DynamoDBClient } from "@aws-sdk/client-dynamodb" import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb" /* whatever constants you need for your mocks */ jest.mock('@aws-sdk/client-dynamodb', () => { return { DynamoDBClient: jest.fn().mockImplementation(() => { return {}; }), }; }); jest.mock('@aws-sdk/lib-dynamodb', () => { return { DynamoDBDocumentClient: { from: jest.fn().mockImplementation(() => { return { send: jest.fn().mockImplementation((command) => { let res = 'something'; if(command.name == 'GetCommand'){ res = 'something else (a constant from earlier...)'; } /* return whatever needs to be returned... */ return Promise.resolve(res); }), }; }), }, /* Return your other docClient methods here too... */ GetCommand: jest.fn().mockImplementation(() => { return { name: 'GetCommand' }; }), }; }); // then you can implement your tests describe('Endpoint something', () => { it('Should pass or something...', async () => { /* your test here... */ }); });

Ahora sus funciones deberían estar disponibles cuando se necesiten.

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!