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

322
Visualizações
Mocking dynamodb `ValidationException` in Jest unit test

I have a class MyCar which has a function let's say getAllCars().

export default class MyCar {
  function getAllCars(){
    try {
      /**
       * logic and query to dynamodb database to get all cars
       * These Car objects will be fetched from db.
      */
      const result = [Car, Car, Car, Car]; 
      return result;
    catch (error) {
      if(error.code === 'ValidationException') {
        throw error;
      }
    }
  }
}

This function has logic to query the dynamodb table and return a paginated response. In one of the scenario dynamodb throws a ValidationException. This exception is caught by the catch() block. I want to mock this ValidationException in my unit test case so that if this exception occurs, I can appropriately return a custom error message.

The returned exception object is this,

{
  "message": "The provided starting key does not match the range key predicate",
  "code": "ValidationException",
  "time": {},
  "requestId": "xxxx-xxxx-xxxx-xxxx-xxxx",
  "statusCode": 400,
}

What I have done for now is this,

jest.spyOn(MyCar.prototype, 'getAllCars')
  .mockImplementation(() => {
    throw { code: 'ValidationException' };
  });

and my expectation is below,

expect(async () => {
  return myCar.getAllCars()
}).rejects.toHaveProperty('code', 'ValidationException');

What I want to do is, instead of throwing the object { code: 'ValidationException' } in the mockImplementation(), I want to throw the actual ValidationException from the dynamodb. Is there any way in which I can do so?

For other exception such as ItemNotFoundException, I can directly import it from @aws/dynamodb-data-mapper. Something like this,

import { ItemNotFoundException } from '@aws/dynamodb-data-mapper';

jest.spyOn(MyCar.prototype, 'getAllCars')
  .mockImplementation(() => {
    throw ItemNotFoundException;
  });

It would be ideal if I could import the exception and mock it or find any package which would let me do so.

Here is a link of Exceptions for dynamodb Error Handling with DynamoDB

about 4 years ago · Juan Pablo Isaza
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