Testing a custom action
Use the following instructions to add unit tests and also test your custom actions in CodeCatalyst workflows.
Important
Currently, only verified partners can test unpublished action versions in workflows.
Adding unit tests
The ADK CLI bootstraps actions with an empty unit test using Jest's testing
framework. You can use the empty unit test as a starting point to write sophisticated unit
tests. The tests are executed when an action is built, and the action build fails if the tests
fail or if the test coverage doesn't meet the expected percentage. You can configure the Jest
configuration file (jest.config.js
) generated by the ADK CLI to
incorporate test coverage and reporting, as well as other forms of testing.
The following JavaScript example uses the Jest testing framework to define a test for an outgoing webhook action:
// @ts-ignore import * as core from '@aws/codecatalyst/adk-core'; import { expect, test, describe } from '@jest/globals'; import { getHeadersInput } from '../lib/utils/input-util'; import { WEBHOOK_HEADERS_MALFORMED_MESSAGE } from '../lib/constants'; const SAMPLE_INPUT_URL = 'https://hooks.sample.com'; const SAMPLE_INPUT_BODY = '{"Sample": "BODY"}'; describe('Outgoing Webhook Action', () => { test('Raises Validation error if webhook headers aren not JSON format', async () => { core.getInput = jest.fn().mockImplementation(inputName => { switch (inputName) { case 'WebhookRequestURL': { return SAMPLE_INPUT_URL; } case 'WebhookRequestHeaders': { return 'invalidHeaders'; } case 'WebhookRequestBody': { return SAMPLE_INPUT_BODY; } default: { throw new Error('Unknown input provided'); } } }); expect(() => { getHeadersInput(); }).toThrowError(WEBHOOK_HEADERS_MALFORMED_MESSAGE); }); });
Testing actions in workflows
To test your action before publishing as a verified partner, you can run it within the workflow and view the run's details. Your workflow generally runs automatically due to a trigger that can include one or more events, such as a code push or pull request. If a trigger isn't defined in the workflow, the workflow can only be started manually. For more information, see Creating, editing, and deleting a workflow.
The ADK generates a continuous integration (CI) workflow that is ready to be
used in CodeCatalyst. By default, a bootstrapped action produces a dist/
folder
with an artifact that contains the dependencies the workflow requires to run successfully in
CodeCatalyst. You must build the actions locally and push the content of the
dist/
folder to the action's source repository before testing the actions
in a workflow.
An action is determined by an action identifier, which consists of the action name and
action version. In the workflow definition file, this information indicates which action and
version to run in the workflow. When an action is not published, .
is used as an
action identifier in the CI workflow, which is generated by the ADK, while testing.
This can help to reference an action that is in the same repository as the workflow file
(.codecatalyst/workflows/
).
actionName
-CI-Validation.yml
Name: MyAction-CI-Validation SchemaVersion: "1.0" Triggers: - Type: PullRequest Events: [ open, revision ] Branches: - feature-.* Actions: ValidateMyAction: Identifier: . Inputs: Sources: - WorkflowSource Configuration: WhoToGreet : 'TEST' HowToGreet : 'TEST'
After making any changes to your source code, build your action locally and push your code again to your CodeCatalyst repository before testing the action in a workflow.
To build and push action source code and the bundle
-
Run the following npm commands to build your action:
npm install
npm run all
-
Run the following commands to commit the changes to your remote repository:
Important
Make sure the code you're pushing doesn't contain any sensitive information that you don't want to be shared publicly.
git add .
git commit -m "
commit message
"git push
If you're making changes using a Dev Environment with a supported IDE, you can also use the source control options available in the IDE.
The action can now be tested with the ADK-generated workflow. By default, the
workflow's name is
.ActionName
-CI-Validation
To test an action within a ADK-generated workflow
Open the CodeCatalyst console at https://codecatalyst.aws/
. -
Navigate to the CodeCatalyst project page.
-
In the navigation pane, choose CI/CD, and then choose Workflows.
-
From the repository and branch dropdown menus, select the repository and feature branch in which you created the action and its workflow.
-
Choose the workflow you want to test.
-
Choose Run to perform the actions defined in the workflow configuration file and get the associated logs, artifacts, and variables.
-
View the workflow run status and details. For more information, see Viewing workflow run status and details.
To test an action in a new workflow
Open the CodeCatalyst console at https://codecatalyst.aws/
. -
Navigate to the CodeCatalyst project page.
-
In the navigation pane, choose CI/CD, and then choose Workflows.
-
From the repository and branch dropdown menus, select the repository and feature branch in which you created the action.
-
Choose Create workflow, confirm the repository and feature branch in which you created the action, and then choose Create.
-
Choose + Actions, choose the Actions dropdown menu, and then choose Local to view your custom action.
-
(Optional) Choose the name of the custom action to view the action's details, including the description, documentation information, YAML preview, and license file.
-
Choose + to add your custom action to the workflow and configure the workflow to meet your requirements using the YAML editor or the visual editor. For more information, see Build, test, and deploy with workflows in CodeCatalyst.
-
(Optional) Choose Validate to validate the workflow's YAML code before committing.
-
Choose Commit, and on the Commit workflow dialog box, do the following:
-
For Workflow file name, leave the default name or enter your own.
-
For Commit message, leave the default message or enter your own.
-
For Repository and Branch, choose the source repository and branch for the workflow definition file. These fields should be set to the repository and branch that you specified earlier in the Create workflow dialog box. You can change the repository and branch now, if you'd like.
Note
After committing your workflow definition file, it cannot be associated with another repository or branch, so make sure to choose them carefully.
-
Choose Commit to commit the workflow definition file.
-
-
View the workflow run status and details. For more information, see Viewing workflow run status and details.