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

181
Visualizações
mock dynamodb partiql using jest

I didn't find a way to mock the new feature of dynamodb "PartiQL" defined in a lambda function.

In PartiQL we can use executeStatement to run sql like queries which are a kind of DynamoDBCustomizations. But he configuration below return an error:

UnrecognizedClientException: The security token included in the request is invalid

which means that the configuration of jest-mock is not correct.

This is the code to be tested

'use strict';


var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const TABLE_NAME = process.env.TABLE_NAME;

async function queryTest() {
  let params = {
    Statement: `select * from ${TABLE_NAME}`
  };
  let statementResult = await ddb.executeStatement(params).promise();
  return statementResult.Items;
}
module.exports.get = async (event) => {
  var result = queryTest();
  if (!result) {
    return {
      statusCode: 200,
      body: JSON.stringify(result)
    };
  }
  else {
    return {
      statusCode: 400,
      body: JSON.stringify(result)
    };
  }
};

and the testing script:

'use strict';

const AWS = require('aws-sdk');
const jestPlugin = require('serverless-jest-plugin');
const lambdaWrapper = jestPlugin.lambdaWrapper;
const mod = require('./handler');

function mock_aws() {
  let mockDynamoDBClient = {
    executeStatement: {
      promise: jest.fn()
    },
    promise: jest.fn(),
  };
  jest.mock("aws-sdk", () => {
    const config = {
      update: jest.fn(),
    };
    return {
      DynamoDB: jest.fn().mockImplementation(() => {
        return {
          executeStatement: () => mockDynamoDBClient.executeStatement,
        };
      }),
      config: config
    };
  });

  return mockDynamoDBClient;
}

describe('test', () => {
  const OLD_ENV = process.env;
  var mDynamoDb;
  var wrapped;
  beforeAll(async () => {
    jest.resetModules();
    wrapped = await lambdaWrapper.wrap(mod, { handler: 'get' });
    process.env = { ...OLD_ENV };
    process.env.TABLE_NAME = "TEST";
    mDynamoDb = mock_aws();
  });
  test('implement tests here', async (done) => {
    const mResult = [{ 'TEST': 1 }] ;
    const response = await wrapped.run({})
    expect(response).toEqual({
      "statusCode": 200,
      "body": JSON.stringify(mResult)
    });
    done();
  });
});

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