gr2m / helpdesk

Answering all your GitHub API/automation questions live on Twitch
https://twitch.tv/gregorcodes
Creative Commons Zero v1.0 Universal
22 stars 11 forks source link

📅 8/19 @ 10:00am PT - Creating tests for actions for faster iteration #46

Closed gr2m closed 3 years ago

gr2m commented 3 years ago

💁🏻 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

When show begins

After the show

Recording

image

Shownotes

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
gr2m commented 3 years ago

Going live in 30 minutes at https://twitch.tv/gregorcodes

gr2m commented 3 years ago

I'm now live on https://twitch.tv/gregorcodes

gr2m commented 3 years ago

It's a wrap! See you next week: https://github.com/gr2m/helpdesk/issues/47

gr2m commented 3 years ago

Show is done for today, thank you all! Recording is coming up in a moment

gr2m commented 3 years ago

Recording is up on twitch:

image

gr2m commented 3 years ago

Recording is now available on youtube: image