npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-lambda-test-case

v0.7.0

Published

AWS Lambda Test Case

Downloads

274

Readme

aws-lambda-test-case

NPM version NPM downloads

You can register AWS Lambda Functions on a case-by-case basis and easily test them, and also receive a report on the results.

🚀Test by invoking the Lambda function deployed on the Dev server.

Install

Install package as development dependency.

npm i aws-lambda-test-case --save-dev

 

Quick start

const { AWSLambdaTestCase } = require('aws-lambda-test-case')

const test = new AWSLambdaTestCase({ service: 'my-repository' })


/**
 * Add test case
 */
test.case('lambdaFunctionName1', 'log title1', (prevRes) => ({
  queryStringParameters: {
    storeId: 1
  },
  failure: AWSLambdaTestCase.BREAK
}))

test.case('lambdaFunctionName2', 'log title2', (prevRes) => ({
  body: {
    productId: prevRes.body.productId
  },
  failure: AWSLambdaTestCase.BREAK
}))

//Test case batch run
test.run()

Console Result

########## AWSLambdaTestCase - Start (total: 2)

1) ===== log title1 =====
 - Status: ✅SUCCESS
 - Function: lambdaFunctionName1
 - RequestId: 609766-c26b-4d86-9493-63a56789d
 { statusCode: 200, body: { result: 'test' } } 
 
2) ===== log title2 =====
 - Status: 🚫FAIL
 - Function: lambdaFunctionName2
 { statusCode: 400, body: { message: 'error!' } } 
 
########## AWSLambdaTestCase - Finished (success: 1, failure: 1)

 

AWSLambdaTestCase

constructor

All options are optional.

  • If you set serverless = true, you configure Lambda FunctionName with <service>-<stage>-<functionName>.
  • To specify a separate ~/.aws/credentials profile alias other than [default], you must set profile.

| Param | Type | Description | | --- | --- | --- | | option | Object | @param {String} service repository name@param {String} stage default: 'dev'@param {String} region default: 'ap-northeast-2'@param {String} profile default: 'default'@param {Boolean} serverless default: true |

 

const test = new AWSLambdaTestCase({
  service: 'my-repository',
  profile: 'my-profile'
})

 

case(functionName, title, generator) : {AWSLambdaTestCase}

Add test case

| Param | Type | Description | | --- | --- | --- | | functionName | String | Lambda function name | | title | String | log title | | generator | Function | @returns {Object} { failure, success, valid, queryStringParameters, body, pathParameters ... } |

 

const test = new AWSLambdaTestCase({
  service: 'my-repository'
})

/**
 * "generator" function dynamically returns Lambda event + request option.
 * @param {Object}  prevRes     Response from previous test case
 * @param {Object}  prevRawRes  Raw response from previous test case
 * @returns {Object}
 * - {Function} valid		Dynamically determines status, if it returns true, it is treated as success (Optional)
 * - {Enum}	failure		  Set whether to continue after a result fails (default: AWSLambdaTestCase.CONTINUE)
 * - {Enum}	success		  Set whether to continue after a successful result (defalut: AWSLambdaTestCase.CONTINUE)
*/
test.case('myFunctionName', 'log title', (prevRes, prevRawRes) => ({
  /** --- Lambda event --- */
  queryStringParameters: {
    storeId: 1
  },
  body: {
    productId: 10
  },

  /** --- Request options --- */
  /**
   * valid function dynamically determines the status.
   * @param {Object}  res   Lambda response result
   * @param {String}  rawRes   Lambda response raw string result
   * @param {Object}  etc
   *  - {Boolean} isApiGatewayFormat  !!(statusCode + body: String)
   * @returns {Boolean} 
  */
  valid: (res, rawRes, { isApiGatewayFormat }) => res.body.result === 'test',
  success: AWSLambdaTestCase.CONTINUE,
  failure: AWSLambdaTestCase.BREAK
}))

 

run(targetCases) : {Promise}

Test case batch run
If you specify a TestCase in the "targetCases" array, only that TestCase will be run.
Returns console log and report data

| Param | Type | Description | | --- | --- | --- | | targetCases | Array | Test cases to be run. (Optional) |

 

Report data

status = success | fail | error

{
  "total": 1,
  "success": 1,
  "failure": 0,
  "reports": [
    {
      "id": "iuydf786as",
      "status": "success",
      "title": "log title1",
      "functionName": "lambdaFunctionName1",
      "requestId": "609766-c26b-4d86-9493-63a56789d",
      "request": {
        "queryStringParameters": {
          "storeId": 1
        }
      },
      "response": "{\"headers\":{\"Access-Control-Allow-Origin\":\"*\"},\"statusCode\":200,\"body\":\"{\"result\":\"test\"}\"}"
    }
  ]
}

 

Example

const { AWSLambdaTestCase } = require('aws-lambda-test-case')
const test = new AWSLambdaTestCase({ service: 'my-repository' })


const case1 = test.case('lambdaFunctionName1', 'log title1', (prevRes) => ({
  queryStringParameters: {
    storeId: 1
  },
  failure: AWSLambdaTestCase.BREAK
}))

const case2 = test.case('lambdaFunctionName2', 'log title2', (prevRes) => ({
  body: {
    //You can set the request for the next case based on the response from the previous case.
    productId: prevRes?.body.productId
  },
  valid: (res) => res.body.list.length > 0,
  failure: AWSLambdaTestCase.BREAK
}))

const case3 = test.case('lambdaFunctionName2', 'log title3', (prevRes) => ({
  queryStringParameters: {
    productId: 3
  },
  failure: AWSLambdaTestCase.BREAK
}))


//Run by specifying a test case
test.run([case2, case3])

 

⚠️ Caution

To use AWS Lambda, you need to set up an IAM policy.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "lambda:InvokeFunction"
      ],
      "Resource": [
        "arn:aws:lambda:<region>:<accountId>:function:*"
      ]
    }
  ]
}