Written By :Appsierra

Mon Apr 15 2024

5 min read

Unit testing with Mocha and Travis & its Build Stages

Home >> Blogs >> Unit testing with Mocha and Travis & its Build Stages
unit testing with mocha and integrate travis ci

Let’s get started with Unit testing with Mocha and Travis CI’s build stages: A unit test is to test the small unit (maybe function or class) in the program, the purpose is to ensure that each small screw in the program has a good operation, as long as each small unit is ok, then the entire large system is less problematic.

We haven’t written the test programs. The program was written and used without errors. It was found to be too convenient. Every time you change the code, you can know whether there is an error or not till the next instruction. Many people may never write a test, so this article will use a simple example to take everyone to write a test and integrate it into Travis CI.

Different Stages of Unit Testing with Mocha and Travis CI's

1. Project Architecture

This is the existing directory structure. This small project is very simple, with only three small features average, max with min, respectively, written in lib the three are files. We are going to add tests for these three small functions.

2. Add Unit Tests with Mocha

Mocha and travis CI

Mocha is a unit testing framework, the use of a fairly simple finish after the test will help you generate a report, in addition to Mocha other than we need should, he makes it easier to describe the content you want to test. After installing a new directory in the test folder, add three files inside average.js, max.jswemin.js will write the test in their respective js file.

3. Start Writing Tests

First to write average.js the test, mocha the syntax is:

test/average.J’s

We want to average be the average value in the array, if the array is empty then return NaN.

test/max.js

Then write max tests, we hope the maximum array can be obtained, if the array is empty then returns undefined.

test / min.js

Finally, to write min the test min with max is much the same, as long as the maximum into a minimum just fine.

So we have finished testing these three functions. 

4. Running Test

How do we run after writing the test? Very simple, just run back to the project directory mocha on it.

As shown, he will automatically run the test folder for all the tests, if there is an error, then president of this.

5. Integration into Travis CI

Travis CI is a platform that provides continuous integration services. If you are using Github you can integrate your project with Travis CI. Any updates to Travis CI will automatically test for you.

First, log in with Github in the upper right corner of the Travis CI website.

After logging in, it will list all your repositories on Github, and choose you to do the CI project.

Then add a file .travis.yml to write Travis settings in the project directory.

Under this paradigm .travis.ymlis an automatic test 6,7,8 three node version.

Because Travis will be using the npm test for testing, so should package.json the test script changes mocha, which would run mocha tests on Travis, this is my package.json In this way, all settings are set. As long as you push Travis on Github, you will help with the test. After the actual push, take a look at Travis. All three versions have passed the test. You can put the above build status in the README, which will make the project look more reliable. If someone sends a pull request, you can also run a test to verify the correctness.

Conclusion

This article uses a very simple example to take you through mocha and should follow Travis CI. The actual case to be tested may not be so simple, but the methods are similar. For a more in-depth study, you can look at their files:

  • mocha
  • should.js
  • Travis CI DOC

Although writing a test is a bit cumbersome, it only needs to be written once. There will inevitably be omissions and a waste of time when you manually test. It is often impossible to find real bugs. 

Related Articles:

Unit testing with xUnit

Key to Unit Testing

Contact Us

Let our experts elevate your hiring journey. Message us and unlock potential. We'll be in touch.

Phone
blog
Get the latest
articles delivered to
your inbox

Our Popular Articles