

# Testing an 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.

**Contents**
+ [

## Adding unit tests
](#test-action-locally)
+ [

## Testing actions in workflows
](#test-action-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](https://docs.aws.amazon.com/codecatalyst/latest/userguide/workflows-create-edit-delete-workflow.html). 

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

1. 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/](https://codecatalyst.aws/).

1. Navigate to the CodeCatalyst project page.

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

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

1. Choose the workflow you want to test.

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

1. View the workflow run status and details. For more information, see [Viewing workflow run status and details](https://docs.aws.amazon.com/codecatalyst/latest/userguide/workflows-view-run.html).

**To test an action in a new workflow**

1. Open the CodeCatalyst console at [https://codecatalyst.aws/](https://codecatalyst.aws/).

1. Navigate to the CodeCatalyst project page.

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

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

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

1. Choose **\$1 Actions**, choose the **Actions** dropdown menu, and then choose **Local** to view your custom action.

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

1. Choose **\$1** 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](https://docs.aws.amazon.com/codecatalyst/latest/userguide/flows.html).

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

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

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

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

   1. Choose **Commit** to commit the workflow definition file.

1. View the workflow run status and details. For more information, see [Viewing workflow run status and details](https://docs.aws.amazon.com/codecatalyst/latest/userguide/workflows-view-run.html).