title: How to test express + dynamodb using Jest?
body: <p>I am new to Jest and unit testing, I have an express API deployed on serverless(Lambda) on AWS.Express api uses dynamodb for crud operations</p>
  <p><strong>Note:- my api is based out of express and not just plain node, because on <a href="https://jestjs.io/docs/dynamodb" rel="nofollow noreferrer">jest website</a> they are telling ways for plain nodejs</strong></p>
  <p>I am able to do unit test on express on the methods which doesnt use dynamodb.However it fails for the methods which are using dynamodb, as to my understanding this has something to do with dynamodb being remote, <strong>because the code present in app.js corresponds to dyanmo db which is hosted on aws using lambda.</strong></p>
  <p>How do I go about it?</p>
  <p>Note:- my api is based out of express and not just plain node</p>
  <pre><code>const isUrl = require('is-url');
  const AWS = require('aws-sdk');
  const { nanoid } = require('nanoid/async');
  const express = require('express');
  
  const router = express.Router();
  const dynamoDb = new AWS.DynamoDB.DocumentClient();
  
  // URL from users
  
  router.post('/', async (req, res, next) =&gt; {
    // urlId contains converted short url characters generated by nanoid
  
    const urlId = await nanoid(8);
    const { longUrl } = req.body;
  
    // Veryfying url Format using isUrl, this return a boolean
    const checkUrl = isUrl(longUrl);
    if (checkUrl === false) {
      res.status(400).json({ error: 'Invalid URL, please try again!!!' });
    }
  
    const originalUrl = longUrl;
    const userType = 'anonymous'; // user type for anonymous users
    const tableName = 'xxxxxxxxxxxxx'; // table name for storing url's
  
    const anonymousUrlCheckParams = {
      TableName: tableName,
      Key: {
        userId: userType,
        originalUrl,
      },
    };
  
    dynamoDb.get(anonymousUrlCheckParams, (err, data) =&gt; {
      const paramsForTransaction = {
        TransactItems: [
          {
            Put: {
              TableName: tableName,
              Item: {
                userId: userType,
                originalUrl,
                convertedUrl: `https://xxxxxxxxxxxxxxxx/${urlId}`,
              },
            },
          },
  
          {
            Put: {
              TableName: tableName,
              Item: {
                userId: urlId,
                originalUrl,
              },
              ConditionExpression: 'attribute_not_exists(userId)',
            },
          },
        ],
      };
      if (err) {
        console.log(err);
        res
          .status(500)
          .json({ error: 'Unknown Server Error, Please Trimify Again!' });
      } else if (Object.keys(data).length === 0 &amp;&amp; data.constructor === Object) {
        dynamoDb.transactWrite(paramsForTransaction, async (error) =&gt; {
          if (error) {
            // err means converted value as userId is repeated twice.
  
            console.log(error);
            res
              .status(500)
              .json({ error: 'Unknown Server Error, Please trimify again. ' });
          } else {
            res.status(201).json({
              convertedUrl: `https://xxxxxxxxxxxx/${urlId}`,
            });
          }
        });
      } else {
        res.status(201).json({
          convertedUrl: data.Item.convertedUrl,
        });
      }
    });
  });
  
  module.exports = router;
  </code></pre>
  <p>my test.js</p>
  <pre><code>const request = require('supertest');
  const app = require('../app');
  
  test('Should convert url from anonymous user ', async () =&gt; {
    await request(app)
      .post('/anon-ops/convert')
      .send({
        longUrl: 'https://google.com',
      })
      .expect(201);
  });
  </code></pre>