Let’s setup a Node project with Unit tests:
Initialize your project with
Here is the code of our app. Put it in an
index.js file at the root of the project.
Install Mocha with NPM:
Let’s write basic unit tests for our code.
test folder and a
index-spec.js file inside that folder.
By default, Travis will execute
npm test when running tests. Let’s set the
test command to
You can now run the tests with
Make sure the tests are running and passing.
Check out the full source if you run into some issues.
.travis.yml file at the root of the project.
This file will hold the travis configuration:
node_js: - "stable"indicates that the project should be tested against the latest stable version of Node
You can add more Node versions to the list if needed.
At this stage, push all your project code to your GitHub repository. Go to your project Settings then Webhooks and Services. Click on Add Service and choose Travis CI:
If it is the first time you are using Travis, you will be asked to login using your GitHub Credentials.
Finish by pressing the Add Service button once more.
Go to travis-ci.org and on the left, click the
+ to add a new repository.
Sync with Github if needed and turn the current repository on.
At this stage, a new build will be started each time changes are pushed to the repository.
If you want to trigger a build manually:
- Go to your repository on github
- Settings > Webhooks & Services > Travis CI
- Click the Test Service Button on the top right
Go back to travis-ci.org to observe the results. If the build failed you will also receive an email.
You may have noticed that we only installed
mocha locally, yet our NPM test script is simply
This is because when evaluating
scripts, NPM will go an look for
In our case, NPM will effectively execute
I hope this will prevent any confusion.