Testing Templates
January 2, 2024 ยท View on GitHub
Manually testing the functionality of your new template locally
-
Make sure you have the Twilio CLI installed.
-
Enter the directory of your template. For example:
cd demo
- Run the local development server:
twilio serverless:start
- If you are using environment variables for your Function, make sure you set them in your normal environment and then run instead the command:
twilio serverless:start --load-local-env
Running automated unit tests
The tests are written using Jest. You can run the test suite by running:
npm test
If you are developing and want to run in watch mode, you can run either:
npm test -- --watch
or alternatively:
npx jest --watch
E2E tests using Cypress
See an example in the /verify-totp-sms template.
Creating your first tests
- Add an
e2e.jsfile to your template with the following content:
const { runE2eTestSuite } = require("../_helpers/test-suite");
runE2eTestSuite({
env: {
// put any environment variables for Twilio Functions here
}
})
You can use the object to also define custom Cypress configuration options.
-
Create a directory
cypress/integrationinside your template directory and add your Cypress test files there. -
In the
package.jsonof your template add the following:
{
"version": "1.0.0",
"private": true,
- "dependencies": {}
+ "dependencies": {},
+ "scripts": {
+ "e2e": "node e2e.js"
+ }
}
- In the project root
package.jsonadd your template name to theworkspacesarray. For example:
"workspaces": [
"hello-world",
+ "my-template"
]
}
Running your E2E test suite
If you only want to run your own template, in the template directory run npm run e2e.
To run all E2E test suites, run in the root npm run e2e. This might take a while.
Fix any repository verification test failures
The majority of the test failures you will see from an npm test run will be in the unit tests you have written for your app. Occasionally, you may see a failure that originates in all-templates.test.js, which contains a suite of verifications that run against the entire function-templates codebase and help ensure that your code will successfully deploy once merged into the repository. Common failure cases for this test suite are:
-
An app has a missing or poorly-formed entry in
templates.json. Usually this is the result of modifying thetemplates.jsonfile by hand. Compare the failing app's entry intemplates.jsonagainst an entry for an app that passes and ensure all fields are present, and that all field names and the app's ID are correctly spelled. A syntax error parsingtemplates.jsoncan also cause a failure here; use a verification tool to track down any syntax issues that may exist in the file. -
Your app's
package.jsonfile has dependencies that are not in thefunction-templatesrepository rootpackage.json. Usually this is the result of adding dependencies by hand instead of using thenpm run add-dependencyscript recommended in this document. To fix this, either usenpm run add-dependencyor edit the repository rootpackage.jsonto contain the correct dependency for your app. -
Your app does not have one unit test file per Function. We encourage thorough testing for all of our apps, and generally prefer each JavaScript file in the
functionsdirectory to have a matching test file in thetestsdirectory. This may not be ideal for the structure of every app; if your app is thoroughly tested but fails this verification, it may make sense to add its ID to theincompleteTestsvariable intest/all-templates.test.jsso that it skips this verification. This test also only applies to files in the top level of thefunctionsdirectory, so placing any JavaScript files that fail verification in a subdirectory offunctionswill also skip this test for those files.
Deploy and test to a hosted Twilio serverless environment
- Create a profile/api key, if you don't already have one
twilio login
- List existing profiles
twilio profiles:list
- Activate the profile
twilio profiles:use <your_profile_id>
- Deploy
twilio serverless:deploy
Test the installation of your template
If you want to test how your new template works with the Twilio CLI, make sure you have the latest version of @twilio-labs/plugin-serverless installed.
Afterwards make sure you push your changes to a different branch or fork of the repository. Your changes have to be uploaded to GitHub for you to be able to test them.
For example if I'm working on the verify template, I might push my changes to a new branch called update-verify under my personal fork of the function-templates repository, located at: https://github.com/dkundel/function-templates.
In order to test if my changes are working, I can invoke the twilio serverless:init command with the following flags:
TWILIO_SERVERLESS_TEMPLATE_BRANCH="update-verify" \
TWILIO_SERVERLESS_TEMPLATE_REPO="dkundel/function-templates" \
twilio serverless:init example --template="verify"