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-cdk-lambda-test-runner

v2.0.1

Published

Create an AWS Lambda Function that runs a suite of tests written using Jasmine. It then stores test results in AWS CloudWatch Logs and Amazon DynamoDB

Downloads

23

Readme

aws-cdk-lambda-test-runner

Install

From TypeScript CDK project run:

npm install aws-cdk-lambda-test-runner

This will install the CDK construct required to deploy TestRunner into your AWS account.

Developing Tests Locally

It is not strictly required to install Jasmine in your CDK project to run integration tests because TestRunner will automatically package and execute tests in the Lambda environment. However, to speed up test development time, you can install Jasmine in your dev dependencies and execute the tests locally.

First install Jasmine and ts-node. ts-node will transpile your test code at runtime, allowing you to write tests in TypeScript without rebuilding before running tests.

npm install --save-dev jasmine @types/jasmine ts-node

Then create a script in your package.json

"scripts": {
    "test:integ": "ts-node node_modules/jasmine/bin/jasmine.js --config=/your-test-folder/support/jasmine.json"
}

For more information on writing tests see: Jasmine: Getting Started

Constructs

TestRunner

Create a suite of unit tests using the Jasmine unit testing framework. Then use the TestRunner construct to deploy them into an AWS Lambda function.

Upon execution the TestRunner Lambda can be configured to publish metrics, write test results into a DynamoDB table, or you can parse the response to determine if the tests passed.

Test execution logs are written into CloudWatch Logs as JSON data using a customized test reporter.

Because tests are executed inside an AWS Lambda function, permissions can be managed through the IAM role assigned to the Function. TestRunner implements IGrantable. This allows it to be use grant functions inside the CDK to grant permission to it directly.

TestRunnerAction

Extends LambdaInvokeAction and integrates the TestRunner with an AWS CodePipeline

Getting Started

To create a TestRunner, initialize the construct. This example creates a Lambda which will be triggered every 30 minutes.

First create a set unit tests. Refer to Jasmine Documentation for a full list of features.

Create a test file like spec-directory/sample.spec.js

describe("A sample test file", () => {
    it("should be able to do addition", () => {
        expect(2 + 2).toBe(4);
    });
})

Then in your CDK stack initialize the TestRunner construct by pointing it to the new directory with test file.

new TestRunner(this, "test-runner", {
    functionName: "MyTestRunner",
    specDir: "spec-directory",
    metricNamespace: "MyCustomMetricNamespace",
    environment: {
        E1: "V1",
        E2: "V2",
    },
    resultsTable: {
        tableName: "MyTestResults",
        dataRetentionDays: 14,
    },
    schedule: events.Schedule.rate(core.Duration.minutes(30)),
    lambdaOptions: {
        logRetention: logs.RetentionDays.TWO_WEEKS,
    },
});

Grant Permissions

TestRunner implements IGrantable. This allows you to grant the TestRunner access to resources in your AWS account by passing it to any resource which accepts an IGrantable

import { Bucket } from "aws-cdk-lib/aws-s3";

var testRunner = new TestRunner(this, ...);

var myBucket = new Bucket(this, "my-resource-bucket")

myBucket.grantReadWrite(testRunner);

Integrating with AWS CodePipeline

TestRunner can be used inside of an AWS CodePipeline to run integration tests during pipeline execution by using the TestRunnerAction construct. The construct can be added to CodePipeline Stage and can detect when it is being run inside CodePipeline

To add TestRunner to an AWS CodePipeline, first create an instance of TestRunner

const integrationTestRunner = new TestRunner(this, "integ-test-runner", { ...props });

Then you can insert it as an action to a CodePipeline stage

const pipeline = new codepipeline.Pipeline(this, "sample-pipeline", { ...pipelineProps });
const integrationTestStage = pipeline.addStage({ ...stageProps });

integrationTestStage.addAction(new TestRunnerAction({
    testRunner: integrationTestRunner,
    lambdaInvokeActionProps: {
        actionName: "RunIntegrationTests"
    }
});

TestRunner Properties

TestRunner Constructor Props

  • specDir: Path to the directory where tests are stored (e.g. "spec" or "test")
  • functionName: Custom name to give to the AWS Lambda function created
  • environment (Optional): Record<string, string> of extra environment properties to pass to the Lambda function environment
  • resultsTable: TestResultsTableProps object. Allows you to configure the DynamoDB table name and dataRetentionDays for TTL on test results.
  • lambdaOptions: TestRunnerLambdaOptions object of extra configuration options for your Lambda for settings like Timeout or Memory Size
  • schedule (Optional): events.Schedule

TestRunner properties

  • testRunnerTable: TestRunner exposes the DynamoDB created where it stores metrics
  • testRunnerLambda: TestRunner Lambda CDK construct
  • functionUrl: TestRunner creates a Lambda function URL to make requests to it directly

TestRunnerAction Constructor Props

  • testRunner: Instance of TestRunner used to execute tests
  • lambdaInvokeActionProps: Configure other LambdaInvokeActionProps from underlying LambdaInvoke action

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.