Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RDSDataClient cannot be mocked #283

Open
sthuber90 opened this issue Feb 15, 2023 · 2 comments
Open

RDSDataClient cannot be mocked #283

sthuber90 opened this issue Feb 15, 2023 · 2 comments

Comments

@sthuber90
Copy link

sthuber90 commented Feb 15, 2023

RDSDataClient is not part of the the mock library but should be.

Lambda function example code:

import { injectLambdaContext, Logger } from '@aws-lambda-powertools/logger'
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer'
import { ExecuteStatementCommand, RDSDataClient } from '@aws-sdk/client-rds-data'
import middy from '@middy/core'
import { APIGatewayProxyResult } from 'aws-lambda'

const logger = new Logger()
const tracer = new Tracer()

const database_name = process.env.dbName
const db_cluster_arn = process.env.dbClusterArn
const db_credentials_secrets_store_arn = process.env.dbSecretArn

const client = tracer.captureAWSv3Client(
  new RDSDataClient({
    region: process.env.AWS_REGION,
    maxAttempts: 10,
  }),
)

const lambdaHandler = async (): Promise<APIGatewayProxyResult> => {
  const sql = `SELECT * FROM my_table`

  const command = new ExecuteStatementCommand({
    secretArn: db_credentials_secrets_store_arn,
    resourceArn: db_cluster_arn,
    database: database_name,
    sql,
    formatRecordsAs: 'JSON',
  })

  const result = await client.send(command)

  return {
    statusCode: 200,
    body: JSON.stringify({
      names: result.formattedRecords ? JSON.parse(result.formattedRecords) : [],
    }),
  }
}

export const handler = middy(lambdaHandler)
  .use(injectLambdaContext(logger, { logEvent: true }))
  .use(captureLambdaHandler(tracer))

Test:

import AWSMock from "aws-sdk-mock"
import AWS from "aws-sdk"
import { ExecuteStatementCommand } from "@aws-sdk/client-rds-data"
import { handler } from '../src'

// context redacted

describe("test lambda function handler", () => {
  beforeEach(() => {
    AWSMock.setSDKInstance(AWS)
  })

  afterEach(() => {
    AWSMock.restore()
  })

  it("should run", async () => {
    AWSMock.setSDKInstance(AWS)
    // error in following line that RDSDataClient is not known
    AWSMock.mock("RDSDataClient", "send", (params: ExecuteStatementCommand, callback: Function) => {  // <========================
      console.log("DynamoDB", "getItem", "mock called")
      callback(null, { formattedRecords: JSON.stringify([]) })
    })

    const res = await handler(/*...*/)
    // make asserts
  })
})
@nelsonic
Copy link
Member

@sthuber90 thank you for your feedback.
Could you please give a bit more detail as to your use case and what you have already tried to do?
Thanks.

@sthuber90
Copy link
Author

@nelsonic thank you for getting back to the issue so quickly. I updated the issue text to keep the thread short. Let me know, if you need anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants