Configuring Jenkins to run Unit tests and Lint checks using Karma and Eslint
This document is part of a series of documents covering Continuous Integration setup for a UI5
Jenkins is used as continuous integration server. In the previous article, we already touched on Setting up Jenkins with GitHub.
Now, we will show how to configure our Jenkins project build to run unit tests and lint checks on a project that is hosted on GitHub. Jenkins is then responsible for running all the tests you specify in the build. If the build fails, it notifies all the developers so that they fix the error that caused the build to fail as soon as possible. Once the build passes after a previous failure, all the developers are notified again that the build is now fine and new code can be pushed to the repository.
Figure 1: Continuous Integration setup with Jenkins
For Unit Tests we are using Jasmine framework for test driven development. Let’s understand briefly what Lint Checks are:
Linting is the process of checking the source code for programmatic, syntactic, stylistic errors. It makes the whole program in a uniform format. It can be achieved by writing some rules. There are so many linters are available like JSLint, JSHint, ESlint.
In this we are configuring Jenkins with ESlint. The reason is,
— JSLint we should follow all the rules strictly.
— JSHint is customizable. We can enable or disable the existing rule
— But in ESlint we can write our own rules. It is completely customizable.
Eslint is written using Node.js to provide a fast run time environment and easy installation.
Now I would explain what steps need to be followed for configuring Jenkins.
1. Install Node.js on your Jenkins server and configure in your project
2. Configuring dependencies in your package.json
Specify all the dependencies in package.json. So that we need not to install manually.
We have to specify the commands as scripts.
We use Karma test runner, for actually running the unit tests and to format the results we use junit reporter.
3. Make these changes in your karma.conf.js
- Specifythe junit in reporters
- Make singleRun as true so that it will run once and exit
- Specify the output directory to store the results
When you run “npm install”, it will install all the dependencies in package.json
This will store linting errors in eslint.xml and unit tests in TESTS*.xml in TEST_RESULTS folder.
Here * will be replaced by the name of the browsers where the tests are running
Eg : TESTS-Chrome_46.0.2490_(Windows_8.1_0.0.0).xml
4. Execute this shell script
5. Configure path for “Publish JUnit test result report” plugin
6. CheckStyle plugin to display the test results in a Graph Format:
- Install checkstyle plugin
- Specify the linting results file to the checkstyle which is in junit format
By default, this plug-in runs only for stable or unstable builds, but not for failed builds. If this plug-in should run even for failed builds then activate this check box.
7. Notifying the Developers whenever build fails
Open Jenkins Dashboard
Go to: Manage Jenkins > Configure System > Email Notification
In your project configuration, under E-mail Notification. Specify the emails of the recipients to whom mail has to be sent when builds fails.
After you build your project with the above configuration, you will able to see the test results in this graph format with build number and time taken to compute them.
In this article we have shown how to run unit tests and linting checks using karma and eslint.
In the next article, we will talk about how to trigger the build automatically whenever the code is pushed to GitHub and also we will explain what is the need of automatic builds.
This article was written with the contribution of : RAVI SURESH MASHRU