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

328
Visualizações
How to mock DynamoDBDocumentClient constructor with Jest (AWS SDK V3)

I'm working with Jest to mock my AWS services, and more specifically DynamoDB and DynamoDBDocumentClient.

My code is currently similar to this :

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

and the test spec looks like this:

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),
  })),
}))

Unfortunately Jest returns me this error : TypeError: lib_dynamodb_1.DynamoDBDocumentClient.from is not a function and I believe it's because I'm incorrectly mocking DynamoDBDocumentClient, as its constructor is a static method.

Thanks in advance for your help!

EDIT: Just noticed that it used the AWS SDK v3 for JavaScript, if that helps.

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

0

In your example, you import your modules and run call your constructor right away (before entering any method in your script), this means that the object you are trying to mock needs to be available before entering any test methods. Try to place your mocks before (and outside of) your tests something like this:

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... */
  });
});

Now your functions should be available when they are needed.

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