-
-
Notifications
You must be signed in to change notification settings - Fork 11
Description
ππ» Automating gr2m/helpdesk: Creating tests for actions for faster iteration
π
Thursday, August 19, 2021
π 10:00am Pacific Time
ποΈ no guests
π https://www.twitch.tv/gregorcodes
π·οΈ testing
Subscribe to this issues to get a notification before the show begins and a summary after the show concludes.
Creating tests for actions for faster iteration
Creating tests for local actions is a huge time saver. It enables quick iteration and avoid regressions once problems occur.
Outline
I will create tests for the existing actions in this repository
TODOs
Before the show
- 30 minute announcement tweet (https://twitter.com/gr2m/status/1428396668236574723)
- 30 minute announcement comment (π 8/19 @ 10:00am PT - Creating tests for actions for faster iterationΒ #46 (comment))
When show begins
- start of show tweet (https://twitter.com/gr2m/status/1428404047254544386)
- comment on issue (π 8/19 @ 10:00am PT - Creating tests for actions for faster iterationΒ #46 (comment))
- Set twitter profile url (https://twitter.com/gr2m)
After the show
- Reset twitter profile after the show
(https://twitter.com/gr2m) - recording available tweet
Recording
Shownotes
- I created a test for the
get-schow-schedules.js
. The final test file:test/get-show-schedules-test.js)
- I used
mockdate
for mocking theDate
API, so that tests won't start failing next week :) - I used
uvu
as a simple test runner, so that I can run all tests withnpm test
- My GitHub Action test workflow:
.github/workflows/test.yml
Important takeaway: wrap your lock action scripts like this:
// my-script.js
import core from "@actions/core";
import { Octokit } from "@octokit/core";
if (process.env.GITHUB_ACTIONS && process.env.NODE_ENV !== "test") {
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
run(process.env, octokit, core);
}
export async function run(env, octokit, core) {
// use `env` instead of `process.env`
core.info("I did a thing")
}
And then your test file can look like this
// test/my-script-test.js
import { run } from "../my-script.js";
// mock environment variables
const mockEnv = {};
// mock octokit
const mockOctokit = {};
// mock core
const outputLogs = [];
const mockCore = {
info(message) {
outputLogs.push(message);
}
};
// run action
await run(mockEnv, mockOctokit, mockCore);
// assertions
deepEqual(outputLogs, [
"I did a thing",
]);
That way, you can run your tests as part of your CI using GitHub actions, but make sure to set NODE_END
to "test"
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
cache: npm
- run: npm ci
- run: node test/my-script.js
env:
NODE_ENV: test