Skip to main content

Testing

Testing your app

If you created your app with create-sst a vitest config was setup for you. You can run tests using

# With npm
npm test
# Or with Yarn
yarn test

Integration tests

When running integration tests, you often need to test against the deployed resources. You can have SST print out the relevant resource properties, like API endpoints and DynamoDB table names to a JSON file.

To do this, first add them as stack outputs:

const api = new Api(stack, "Api", {
routes: {
"GET /": "src/lambda.main",
},
});

stack.addOutputs({
ApiUrl: api.url,
});

Then when you deploy your app, use the --outputs-file option to write these stack outputs to a JSON file.

npx sst deploy --outputs-file outputs.json
// or
yarn deploy --outputs-file outputs.json

You can now parse the JSON file to get the value of the ApiUrl and use it in your integration tests.

Stacks tests

If you'd like to test your stack code itself without deploying anything you can do so with aws-cdk-lib/assertions

Here is an example test

import { Stack, App } from "@serverless-stack/resources"
import { Template } from "aws-cdk-lib/assertions"

test("queue exists", async () => {
const app = new App()
const stack = new MyStack(app)
const template = Template.fromStack(MyStack);
template.resourceCountIs("AWS::SQS::Queue", 1);
});