How to re-use some of your E2E tests as smoke tests
When creating automated tests for my serverless API backends, I create a suite of end-to-end (E2E) tests which mostly involve making requests to the deployed API endpoint (API Gateway or AppSync). When run as part of a CI/CD pipeline, these E2E tests run after deployment of a stack to a target AWS environment (test or staging). However, I usually don’t want to run all these E2E tests in production due to potential side effects that they may cause such as creation of data or the triggering of async message flows which may push data to third-party services.
However, there is still a risk of production-environment specific bugs creeping in, such as a new feature that requires a secret which is missing from the prod environment. So it can be nice to run a few sanity/smoke tests to verify that some key behaviours are as expected. These smoke test cases need to be low-impact (ideally readonly) so that you can be sure they won’t negatively impact on the production system.
If you’re authoring your tests with Jest, rather than writing separate smoke test cases, you can re-use existing E2E test cases as smoke tests using Jest’s —testNamePattern CLI option, and then tagging your test case titles (or the higher-level describe
constructs) with ”[SMOKE]”, like so:
it('The user can fetch their profile with getMyProfile [SMOKE]')
Then in your package.json scripts section, you can add a command to run your smoke tests:
{
"scripts": {
"test:e2e": "jest ./tests/test-cases/e2e -c jest.config.aws.js",
"test:smoke": "jest ./tests/test-cases/e2e -c jest.config.aws.js -t '^(.*(\\[SMOKE\\])).*$'"
}
}
Note that both commands reference the same test cases folder and config file, the only difference is the -t
(testNamePattern) argument. Jest will flag any tests which don’t match this pattern as “skipped”. One thing to be aware of is that Jest will still execute any before*
and after*
handlers which are within scopes that match your provided regex pattern.
Paul Swail
Indie Cloud Consultant helping small teams learn and build with serverless.
Learn more how I can help you here.
Join daily email list
I publish short emails like this on building software with serverless on a daily-ish basis. They’re casual, easy to digest, and sometimes thought-provoking. If daily is too much, you can also join my less frequent newsletter to get updates on new longer-form articles.