DEV Community

Pavel Belokon
Pavel Belokon

Posted on

Using GitHub Actions

This time, I had to set up GitHub Actions for automatic tests on new pull requests and pushes to the main and master branches.

You can see my actions here

Setting this up was a piece of cake; I followed this guide.

Basically, here are the steps you have to take:

First, create a .github/workflows directory, and then inside the workflows directory, add any workflow you want, for example, a test.yml file.

Here's what mine looks like:

name: Run Unit Tests

on:
  push:
    branches: [master, main]
  pull_request:
    branches: [master, main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v2
        with:
          node-version: 18

      - run: npm ci

      - run: npm run test
Enter fullscreen mode Exit fullscreen mode

The first thing you notice is the name of the workflow, which helps identify what process is being run. Then you specify the triggers for this action; in this case, I set it up on push and pull requests to the master and main branches. Finally, you define the jobs that the action should follow. First, you specify the system it should run on; you can even have multiple platform tests. Lastly, you specify the Node version and commands it should execute.

I did encounter failing workflows initially because I followed the wrong guide, not for Node. Then, I discovered that I needed to include file extensions in imports for CI to find them in tests.

This week, I also contributed to one of my classmates by writing a test that would check if the custom output directory works properly. You can find it here. The testing environment was similar since he was using Jest, which is similar to Vitest that I use. So, I did not have any issues understanding how to write tests for this project. Also, since we both use JavaScript for our projects, the general structure was quite similar in some places.

In conclusion, I would say that my perspective on CI did not change as I finished this lab, since I have used it a lot in other classes before, and, what can I say, it does what it should, so nothing to complain about.

Top comments (0)