Sub topic 12
Testing an action
This chapter assumes you can write a basic workflow, have built the JavaScript and Docker actions from earlier chapters, and know how uses: ./ and step outputs work. You will test an action at three levels: unit tests, a workflow in the same repository, and a local run of the workflow.
Why test at three levels
An action runs inside a runner. It reads inputs from environment variables and writes outputs to files. A bug can sit in your logic, in your action.yml, or in how the runner wires the two together. No single test finds all three.
| Level | What it checks | Speed |
|---|---|---|
| Unit test | Your JavaScript logic | Seconds |
| Workflow test | `action.yml`, inputs, outputs, runtime | Minutes |
| Local run | The workflow before you push | Seconds to minutes |
Unit testing a JavaScript action
Keep the logic in a function that does not touch the runner. Then test that function directly.
// greet.js
function buildGreeting(name) {
if (!name || !name.trim()) {
throw new Error('who-to-greet must not be empty');
}
return `Hello ${name.trim()}!`;
}
module.exports = { buildGreeting };// greet.test.js
const test = require('node:test');
const assert = require('node:assert');
const { buildGreeting } = require('./greet');
test('greets a trimmed name', () => {
assert.strictEqual(buildGreeting(' Mona '), 'Hello Mona!');
});
test('rejects an empty name', () => {
assert.throws(() => buildGreeting(' '), /must not be empty/);
});Run it with the Node built-in test runner:
node --testYour index.js then stays thin. It calls core.getInput, passes the value to buildGreeting, and calls core.setFailed in the catch block.
Testing in a workflow
A workflow in the action's own repository is the most realistic test. It checks out the code, runs the action with uses: ./, and asserts on the result.
name: Test hello action
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: node --test
integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: 'Mona'
- name: Check the output
run: |
if [ -z "${{ steps.hello.outputs.time }}" ]; then
echo "time output was empty"
exit 1
fiWithout the actions/checkout step, uses: ./ fails because the runner has no copy of your action. The assertion step fails the job with a non-zero exit status when the output is empty. Add a second step with continue-on-error: true if you also want to test that a bad input fails the action.
Testing locally
The community tool act runs workflows in Docker containers on your machine. It is not an official GitHub product, and it does not copy the hosted runner exactly. Treat a pass as a good sign, not proof.
act push -j integrationHow the pieces fit
Common errors
- Forgetting
checkout:uses: ./cannot findaction.yml. - Stale bundle: you changed the source but did not rebuild the bundled file. The workflow test runs the old code.
- Testing only the happy path: also test an empty or invalid input, and confirm the step fails.
- Relying on
actalone: hosted runners differ in images and tools.
Exercise
Your unit tests pass, but the workflow test shows the output time is empty. Name two places to look.
Show answer
First, check that the action's logic is actually run and sets the output, for example that index.js calls core.setOutput('time', ...) and that the bundled file is rebuilt. Second, check that the workflow reads the output through the correct step id (steps.hello.outputs.time) and that action.yml declares the time output.
Exercise
Why does the integration job need actions/checkout before uses: ./?
Show answer
The runner starts with an empty workspace. uses: ./ looks for action.yml in the checked-out repository, so without checkout it finds nothing to run.