Skip to content

πŸ“… 8/19 @ 10:00am PT - Creating tests for actions for faster iterationΒ #46

@gr2m

Description

@gr2m

πŸ’πŸ» 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    showPreparation issue for a live show

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions