Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
7 tasks done
gr2m opened this issue Aug 12, 2021 · 6 comments
Closed
7 tasks done
Labels
show Preparation issue for a live show

Comments

@gr2m
Copy link
Owner

gr2m commented Aug 12, 2021

πŸ’πŸ» 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 gr2m changed the title DO NOT EDIT - Await parsing by GitHub Actions πŸ“… 8/19 @ 10:00am PT - Creating tests for actions for faster iteration Aug 12, 2021
@gr2m gr2m added the show Preparation issue for a live show label Aug 12, 2021
@gr2m

This comment has been minimized.

@gr2m

This comment has been minimized.

@gr2m
Copy link
Owner Author

gr2m commented Aug 19, 2021

It's a wrap! See you next week: #47

@gr2m gr2m closed this as completed Aug 19, 2021
@gr2m
Copy link
Owner Author

gr2m commented Aug 19, 2021

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

@gr2m

This comment has been minimized.

@gr2m
Copy link
Owner Author

gr2m commented Aug 19, 2021

Recording is now available on youtube:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
show Preparation issue for a live show
Projects
None yet
Development

No branches or pull requests

1 participant