Skip to main content

Unit Testing AWS Infrastructure with Jest

An example of using Jest with Pulumi to write test-driven code that mocks AWS infrastructure.

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set testing-unit-ts-mocks-jest
cd pulumi-examples/testing-unit-ts-mocks-jest

An example of using Pulumi with Jest, the JavaScript testing framework, to write in-memory unit tests that mock AWS infrastructure. The program under test deploys a single AWS Lambda function and an associated Lambda Function URL.

Prerequisites#

  1. Install Pulumi.
  2. Install Node.js.
  3. Configure your AWS credentials.

Deploying the App#

  1. Clone this repo, change to this directory, then create a new stack for the project:

    Terminal window
    pulumi stack init
  2. Specify an AWS region to deploy into:

    Terminal window
    pulumi config set aws:region us-west-2
  3. Install Node dependencies and run the tests:

    Terminal window
    npm install
    npm test

    In a few moments, the tests should pass.

  4. If you’d like to deploy the program as well, run pulumi up. In a few moments, the FunctionUrl of the timeURL Lambda will be emitted as a Pulumi stack output called audioURL:

    Terminal window
    ...
    Outputs:
    audioURL: "https://o3vbc73qd2vxrhtaao5v53yeaa0sricr.lambda-url.us-west-2.on.aws/"
  5. When you’re ready, destroy your stack and remove it:

    Terminal window
    pulumi destroy --yes
    pulumi stack rm --yes

Related

The infrastructure as code platform for any cloud.