Testing a custom action - Amazon CodeCatalyst

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

  1. Run the following npm commands to build your action:

    npm install
    npm run all
  2. 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

  1. Open the CodeCatalyst console at https://codecatalyst.aws/.

  2. Navigate to the CodeCatalyst project page.

  3. In the navigation pane, choose CI/CD, and then choose Workflows.

  4. From the repository and branch dropdown menus, select the repository and feature branch in which you created the action and its workflow.

  5. Choose the workflow you want to test.

  6. Choose Run to perform the actions defined in the workflow configuration file and get the associated logs, artifacts, and variables.

  7. 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

  1. Open the CodeCatalyst console at https://codecatalyst.aws/.

  2. Navigate to the CodeCatalyst project page.

  3. In the navigation pane, choose CI/CD, and then choose Workflows.

  4. From the repository and branch dropdown menus, select the repository and feature branch in which you created the action.

  5. Choose Create workflow, confirm the repository and feature branch in which you created the action, and then choose Create.

  6. Choose + Actions, choose the Actions dropdown menu, and then choose Local to view your custom action.

  7. (Optional) Choose the name of the custom action to view the action's details, including the description, documentation information, YAML preview, and license file.

  8. 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.

  9. (Optional) Choose Validate to validate the workflow's YAML code before committing.

  10. Choose Commit, and on the Commit workflow dialog box, do the following:

    1. For Workflow file name, leave the default name or enter your own.

    2. For Commit message, leave the default message or enter your own.

    3. 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.

    4. Choose Commit to commit the workflow definition file.

  11. View the workflow run status and details. For more information, see Viewing workflow run status and details.