DEV Community

cychu42
cychu42

Posted on

Auto-Run Tests: Test Via Continuous Integration

What is Continuous Integration(CI)?

It's a way to make sure new codes are continuously integrated via running automated tests to make sure new codes follow certain standards and don't break anything.

How to do it?

I used Github Action which made the process pretty easy.

  1. In your GitHub repo, click the Action tab and choose a template designed for your language. This will add an YAML file for configuration of this.
  2. Edit the YAML file to suit your needs: Image description
    • on lets you set when to run tests, such as: on any push to main branch or any PR to main
    • runs-on sets the OS of virtual machine to run the tests, such as: windows-latest,macos-latest, or ubuntu-latest
    • strategy's matrix sets versions of the language or tool, like node-version [16.x, 18.x] for Node.js versions of 16 or 18
    • Under steps:
      • uses checks out code to run
      • name sets up version of node to run
      • run: npm cl installs dependency
      • run: npm build runs npm build according to your build script in package.json (optional), like:- run: npm run build --if-present
      • run: npm test runs npm test command according to your test script in package.json

Another CI

There was another repo I tried to add test for, and it uses the same setup as mine. It uses JavaScript and achieve CI via GitHub Action, except that it also run test for version 14.x and 16.x of Node.js. Given both repo use Jest for testing, It's pretty easy to write test for the repo. I needed to spend time to understand the code and then run test coverage and see the existing tests to see what's needed to be done.

Final Thought

I think CI is pretty great, as it provides an easy way to run tests and make sure new codes conform to certain standards before being added. It also saves time for developers from running tests manually, and CI makes sure no test is forgotten.

Top comments (0)