I’m growing into the best version of myself;

If you visit Github regularly, do you often see a status icon like the one below?

What’s this?

Travis-CI is a continuous integration build project that uses a refreshing little YML syntax that you can think of as a Linux system for the Web, driven by YML syntax. It’s free for open source projects on Github, though private repo is a bit more expensive.

Coveralls is an automated test coverage service.

Assuming you’re comfortable with Github before using these two services, I’ll use Mocha, Chai to build an automated test.

The preparatory work

Using your Github account login Travis -ci.org/, click on the Accounts in the upper right corner to go to a page and click on Sync Account to Sync the repositories publicly available on Github. The steps are detailed on the Travis-CI website. The next step is to select the project you want to integrate continuously, turn on the switch, create the configuration file, and trigger the first hook.

npm install mocha chai --save-dev --verbose
Copy the code

Using your Github account login coveralls. IO /, click ADD REPOS in the upper right corner to open a repository.

npm install coveralls --save-dev --verbose
Copy the code

Note: All examples run on Mac, Windows may have some differences

start

Create a.travis. Yml file in the root directory of the project and write the description:

Language: node_js sudo: true node_js: - '4.1.1' cache: directories: -node_modules before_install: - npm install script: - npm test - npm run dev - npm run build - npm run checkdir after_script: - npm run coverage

We are creating a JavaScript project, so the language selects node_js and selects a version. Travis-CI executes NPM test by default, so we also need to ensure that NPM test is available. For NPM SCRIPT hooks, I recommend reading NPM-scripts.

Travis-CI provides a life cycle for execution environments, such as before_install, after_script, etc. This cycle is designed to ensure that the order of each command is correct. The most important thing for us, of course, is script, which is a sequential execution.

Next we need to install Istanbul, which generates the LCOV file that coveralls. IO/service needs.

npm install istanbul --save-dev --verbose
Copy the code

1. Istanbul Mocha Coveralls better not be installed on a whole Istanbul mocha coveralls

Take the ManagedObject class as an example:

var chai = require('chai'); var Manage = require('.. /.. /src/entity/ManagedObject'); var expect = chai.expect; / / to omit... Describe ('ManagerObject', function() {// omit.... Describe ('$sort sort the data within the entity. The second parameter is to sort the data. Structured expression, the second argument is sort by ',function(){it(' test. Function (){expect(manager.$sort('items','id.)).to.eql(sortTestData)}) it(' test. Function (){expect(managerT.$sort('items','id.>')).to.eql(sortTestDataT)}) it(' function(){ expect(managerT.$sort('items',function(){ return true })).to.eql(sortTestDataT.reverse()) }) }) })

NPM scripts hook:

    "scirpts":{
         "test": "node ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --colors ./configs/mocha/**/*.test.js",
         "coverage": "cat ./coverage/lcov.info | coveralls"
    }

  • Node. /node_modules/. Bin/Istanbul cover
  • ./node_modules/mocha/bin/mocha –colors ./configs/mocha/*/.test.js is associated with the Mocha test driver, and the test case file suffix is.test.js in the configs/mocha directory
  • Cat./coverage/lcov.info Prints at the terminal
  • Coveralls performs the Coveralls service (if it is a public project, there is no need to integrate any token in the.travis. Yml file)

The last

git push origin master
Copy the code

Commit a commit to trigger Travis-CI and you get the following:

During the build process, you may also want to pay attention to some issues:

  • The YML format needs to be written correctly (to make sure every space is written correctly, Travis – CI provides a check tool), and if you submit to Github and your YML format is not written correctly, there will be no style.
  • Do not use Watch mode when using scripts as this will cause the build to fail
  • For NPM installations, there is no need to print the installation information (because too many logs will also fail the build)

Of course, there are many more advanced uses of Travis-CI that need to be explored.