title: Mocking dynamodb `ValidationException` in Jest unit test
body: <p>I have a class <code>MyCar</code> which has a function let's say <code>getAllCars()</code>.</p>
  <pre><code>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;
        }
      }
    }
  }
  </code></pre>
  <p>This function has logic to query the dynamodb table and return a paginated response.
  In one of the scenario dynamodb throws a <code>ValidationException</code>. This exception is caught by the catch() block. I want to mock this <code>ValidationException</code> in my unit test case so that if this exception occurs, I can appropriately return a custom error message.</p>
  <p>The returned exception object is this,</p>
  <pre><code>{
    &quot;message&quot;: &quot;The provided starting key does not match the range key predicate&quot;,
    &quot;code&quot;: &quot;ValidationException&quot;,
    &quot;time&quot;: {},
    &quot;requestId&quot;: &quot;xxxx-xxxx-xxxx-xxxx-xxxx&quot;,
    &quot;statusCode&quot;: 400,
  }
  </code></pre>
  <p>What I have done for now is this,</p>
  <pre><code>jest.spyOn(MyCar.prototype, 'getAllCars')
    .mockImplementation(() =&gt; {
      throw { code: 'ValidationException' };
    });
  </code></pre>
  <p>and my expectation is below,</p>
  <pre><code>expect(async () =&gt; {
    return myCar.getAllCars()
  }).rejects.toHaveProperty('code', 'ValidationException');
  </code></pre>
  <p>What I want to do is, instead of throwing the object <code>{ code: 'ValidationException' }</code> in the mockImplementation(), I want to throw the actual <code>ValidationException</code> from the dynamodb. Is there any way in which I can do so?</p>
  <p>For other exception such as <code>ItemNotFoundException</code>, I can directly import it from <code>@aws/dynamodb-data-mapper</code>. Something like this,</p>
  <pre><code>import { ItemNotFoundException } from '@aws/dynamodb-data-mapper';
  
  jest.spyOn(MyCar.prototype, 'getAllCars')
    .mockImplementation(() =&gt; {
      throw ItemNotFoundException;
    });
  </code></pre>
  <p>It would be ideal if I could import the exception and mock it or find any package which would let me do so.</p>
  <p>Here is a link of Exceptions for dynamodb <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html" rel="nofollow noreferrer">Error Handling with DynamoDB</a></p>