Sub topic 12
Testing an action
This chapter assumes you can write an action.yml, a JavaScript or Docker action, and a basic workflow that calls it with uses: and with:. It also assumes you know npm scripts and docker build/docker run. Here you will test the Hello World action from earlier chapters at three levels: unit tests, local runs that imitate the runner, and a workflow that runs the real action.
Why actions are awkward to test
An action only works inside a runner. The runner supplies inputs as environment variables, gives you files to write outputs to, and reads your exit status. The same runner also does the checkout and downloads the action. A bug can sit in your logic, in the contract with the runner, or in how the workflow calls the action. One kind of test rarely catches all three.
| Layer | What it proves | Speed | Where it runs |
|---|---|---|---|
| Unit test | Your logic, including failure branches | Milliseconds | Your machine and CI |
| Local simulation | The runner contract: `INPUT_*` variables, `GITHUB_OUTPUT`, exit code | Seconds | Your machine |
| Workflow test | `action.yml`, runtime, real context, real outputs | Minutes | GitHub-hosted runner |
Make the JavaScript testable
The action from earlier chapters runs its code at the top level of index.js. Importing that file in a test runs it immediately. Move the logic into a function and keep index.js as a thin entry point.
// main.js
const core = require('@actions/core');
function run() {
try {
const nameToGreet = core.getInput('who-to-greet', { required: true });
core.info(`Hello ${nameToGreet}!`);
core.setOutput('time', new Date().toTimeString());
} catch (error) {
core.setFailed(error.message);
}
}
module.exports = { run };// index.js
require('./main').run();action.yml still points at index.js, so nothing in the metadata changes. The required: true option makes getInput throw when the value is empty. That gives you a failure path to test. The required field in action.yml is documentation only, so the runner does not enforce it.
Unit tests with a mocked toolkit
Jest can replace @actions/core with an automatic mock. The mock records calls without touching the environment or setting the process exit code. You then assert on what your code asked the toolkit to do.
{
"name": "hello-action",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0"
},
"devDependencies": {
"jest": "^29.7.0"
}
}// main.test.js
jest.mock('@actions/core');
const core = require('@actions/core');
const { run } = require('./main');
beforeEach(() => {
jest.resetAllMocks();
});
test('greets the configured name and sets the time output', () => {
core.getInput.mockReturnValue('Mona');
run();
expect(core.info).toHaveBeenCalledWith('Hello Mona!');
expect(core.setOutput).toHaveBeenCalledWith('time', expect.any(String));
expect(core.setFailed).not.toHaveBeenCalled();
});
test('fails the step when the input is missing', () => {
core.getInput.mockImplementation(() => {
throw new Error('Input required and not supplied: who-to-greet');
});
run();
expect(core.setFailed).toHaveBeenCalledWith(
'Input required and not supplied: who-to-greet'
);
expect(core.setOutput).not.toHaveBeenCalled();
});Run it with npm test. Always write at least one test per failure branch. Failure handling is the part of an action that users notice most, and the happy path is the part you already check by hand.
What mocks cannot tell you
A mocked core proves your code calls the toolkit correctly. It does not prove the toolkit reads the variable names the runner really sets. It also cannot show that action.yml points at the right file. Those gaps are what the next two layers cover.
Simulating the runner locally
The runner passes each input as an environment variable named INPUT_ plus the input name in upper case. Spaces become underscores, but hyphens stay. The input who-to-greet therefore becomes INPUT_WHO-TO-GREET.
Outputs go to the file named by GITHUB_OUTPUT. Point that variable at a temporary file, run the action, and read the file back:
out="$(mktemp)"
env "INPUT_WHO-TO-GREET=Mona" GITHUB_OUTPUT="$out" node index.js
echo "exit status: $?"
cat "$out"The file holds a record such as time<<ghadelimiter_..., then the value, then the same delimiter. @actions/core uses this delimited form so values can span several lines. If the file is empty, the action never set the output. Also try running without the input variable and confirm the exit status is 1.
The process exit code matters here. core.setFailed sets process.exitCode = 1, and the runner marks the step failed when the process exits non-zero.
Testing a container action locally
For the Docker action, build the image and run it as the runner would. The runner passes args from action.yml as arguments to the entrypoint, so you pass the input as the first argument. The output file lives inside the container, so mount a host directory to read it afterwards.
docker build -t hello-action .
mkdir -p out
docker run --rm \
-v "$PWD/out:/out" \
-e GITHUB_OUTPUT=/out/github_output \
hello-action Mona
echo "exit status: $?"
cat out/github_outputYou should see Hello Mona printed, then time=... in the output file. This run also catches common Dockerfile mistakes. One is a missing chmod +x. Another is Windows line endings in entrypoint.sh, which show up as a no such file or directory error at start-up.
Testing in a real workflow
The most reliable test is a workflow in the action's own repository that uses the action with uses: ./. This exercises action.yml, the runtime, the context, and the output plumbing together. The workflow below also runs the unit tests and tries several inputs in a matrix.
name: CI
on:
push:
pull_request:
jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
integration:
runs-on: ubuntu-latest
strategy:
matrix:
who: ['Mona', 'Octo Cat']
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: ${{ matrix.who }}
- name: Assert the output is set
env:
TIME: ${{ steps.hello.outputs.time }}
run: |
test -n "$TIME"
echo "time output was: $TIME"Three details matter here:
actions/checkoutmust come first. Withuses: ./the runner looks foraction.ymlin the checked-out workspace, and without a checkout it fails to find the action.- The assertion step passes the output through
envinstead of pasting${{ ... }}into the shell script. That avoids script injection if the value ever contains shell syntax. - A matrix runs the same action against several inputs. Include values with spaces and special characters, because those expose quoting bugs in container entrypoints.
Asserting that an action fails
Sometimes the behavior under test is the failure. Mark the step with continue-on-error: true and give it an id. The job then continues, and a later step can check steps.<id>.outcome.
| Property | Meaning |
|---|---|
| `outcome` | Result before `continue-on-error` is applied. It is `failure` if the step failed. |
| `conclusion` | Result after `continue-on-error` is applied. It is `success` for the same step. |
A follow-up step with if: steps.bad.outcome != 'failure' can then run exit 1 to fail the job when the action unexpectedly succeeded.
Testing bundled JavaScript
If you bundle with a tool such as @vercel/ncc, the file the runner executes is the generated one, not your source. Unit tests on main.js do not prove the bundle works. The integration job above does, because it runs whatever action.yml names. Add a check that the committed bundle matches the source, assuming your package.json has a build script that regenerates it:
npm run build
git diff --exit-code -- distIf the command exits non-zero, someone changed the source without rebuilding. Run it in CI so a stale bundle never reaches a release tag.
Common mistakes
- Testing only the happy path, so a broken
setFailedbranch ships unnoticed. - Forgetting
actions/checkoutbeforeuses: ./. - Running the integration job only on
main, so pull requests merge without the real test. - Setting
INPUT_variables with underscores where the input name has hyphens. - Reading an output in a later step from
steps.<id>.outputswhen the step has noid.
Exercise
You run INPUT_WHO_TO_GREET=Mona node index.js locally and the action prints Hello !, but INPUT_WHO-TO-GREET=Mona is not accepted by your shell. What is wrong, and how do you set the correct variable?
Show answer
core.getInput('who-to-greet') reads INPUT_WHO-TO-GREET, because only spaces become underscores and the hyphens stay. INPUT_WHO_TO_GREET is a different variable, so the input comes back empty. Bash cannot export a name containing a hyphen, so use env: env "INPUT_WHO-TO-GREET=Mona" node index.js.
Exercise
Your integration job contains uses: ./ but fails with an error that action.yml cannot be found. What is the most likely cause and the fix?
Show answer
The job never checked out the repository, so the workspace has no action.yml. Add uses: actions/checkout@v4 as a step before the step that uses ./.
Exercise
A step runs your action with continue-on-error: true and the action fails. What are steps.<id>.outcome and steps.<id>.conclusion?
Show answer
outcome is failure, the real result before continue-on-error applies. conclusion is success, the result after it applies. Use outcome when asserting that the action failed.