Connect Karma Runner to Jenkins CI - angularjs

Help me understand how to connect my Angular-Jasmine-Karma stack to Jenkins. I have an Angular.js web app that I test with Karma (né Testacular) and Jasmine. It looks just like the Angular Tutorial. I want to test it using Jenkins Continuous Integration.
So far I have installed Angular, Jasmine and Karma according to the tutorial. I have installed Jenkins. I can get each working independently. From what I've gleamed, it seems as though Karma should output an XML file that Jenkins ingests, but Karma is not consistently outputting a file, and I do not understand this conceptually. At what point does Jenkins call Karma?
A good answer would outline the pieces needed to do Karma testing in Jenkins.
Just in case, here is my Karma config. It has been mutilated in the name of debugging.
module.exports = function(config){
config.set({
basePath : '../',
files : [
'app/lib/angular/angular.js',
'app/lib/angular/angular-*.js',
'app/js/**/*.js',
'test/unit/**/*.js'
],
exclude : [
'app/lib/angular/angular-loader.js',
'app/lib/angular/*.min.js',
'app/lib/angular/angular-scenario.js'
],
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-script-launcher',
'karma-jasmine'
],
reporters : ['dots', 'junit', 'coverage'],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
coverageReporter : {
type: 'cobertura',
dir: 'coverage/',
file: 'coverage.xml'
}
});
};

First, you need a karma.conf.js file that lists the following:
reporters: ['progress', 'coverage', 'dots', 'junit'],
junitReporter: {
outputDir: 'karma-results',
outputFile: 'karma-results.xml'
},
browsers: ['PhantomJS'],
singleRun: true
The most important item under the reporters key is junit. This is the add-on that will translate your Karma outputs into an XML file. Your test outputs must be in a specific XML format for Jenkins to parse it. You configure the output location of this XML file using the junitReporter key. In the browsers key, make sure you are specifying PhantomJS since it is most likely your Jenkins server will not have an instance of Chrome or Firefox. The singleRun key makes sure the Karma server is launched before tests are run and shut down when tests are finished.
Next, make sure all of the following node modules are installed on your server by running these commands:
npm install -g karma-cli
npm install -g karma --save-dev
npm install -g phantomjs
npm install -g karma-jasmine --save-dev
npm install -g karma-phantomjs-launcher --save-dev
npm install -g karma-coverage
Visit your Jenkins server through a browser. You can reach your Jenkins server at
http://server-ip-address:8080
Before moving forward, make sure you have the "
Environment Injector Plugin" and "Junit Plugin" installed. Once that is there, click on New Item on the left-hand side of the Jenkins homepage. Have the following parameters set for your job:
The "Properties Content" allows you to assign Jenkins a PATH on your server and allows you to use the karma keyword in the "Command" section below it. The "Command" section tells Jenkins to cd to the folder where your karma.conf.js file resides and start Karma.
If you use the outputDir and outputFile values in my karma.conf.js example above, then you can keep the "Test report XMLs" input value. Otherwise, change that to reflect the new path to where your XML results file will be generated.
Now, whenever you run this job in Jenkins, you'll be able to see whether or not it passed and also the line item results from your tests.

Do you use maven as a build tool? If so, take a look at: https://github.com/eirslett/frontend-maven-plugin. It runs the tests, so Jenkins can show the results.

Related

Bitbucket - No binary for Chrome browser on your platform

I'm working on basic react project and I'm able to run test with karma and mocha on my mac with chrome.
But bitbucket pipeline says that I do not have a chrome, so the question is how to install chrome there and will I have to install it every time with build?
my yml
image: node:7.10.0
pipelines:
default:
- step:
script:
- npm install -g bower
- bower install --allow-root
- npm install
- npm test
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha'],
files: [
'./tests/*.js'
],
exclude: [],
preprocessors: {
'./tests/*.js': ['webpack']
},
// webpack configuration
webpack: require('./webpack.dev.js'),
webpackMiddleware: {
stats: 'errors-only'
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'], //run in Chrome
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
concurrency: Infinity
});
};
The line image: node:7.10.0 in your bitbucket-pipelines.yml file specifies a Docker image to use. In your case, it’s a plain node version 7.10.0 image, so there is no Chrome contained in it.
There are two things you can do:
Read about Docker, learn how to create your own image that includes Chrome (or whatever other software) and then use that image in your pipeline
Or, probably far easier: search for an existing Docker image created by someone else which includes node, Chrome and possibly other software you might need. Then, use that image in the image: <image-name> configuration line.
In either case, once you have a suitable image, this will be needed when your pipeline is run and Chrome will be available immediately, and you will not need any kind of “installation”.

React telling me I am using minified in dev ...

So this is my task:
gulp.task('prod', function() {
browserify({entries: [
'resources/assets/js/app.js'
]})
.transform("babelify")
.transform(envify({
NODE_ENV: 'production'
}))
.bundle()
.pipe(source('all.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.on('error', gutil.log)
.pipe(gulp.dest('public/js/'));
});
yet the console still sais:
all.js:30 Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See [url here that stack doesn't like] for more details.
So I am confused. Whats the deal?
When running build process, set NODE_ENV like this (linux):
NODE_ENV=production gulp build
Or (Windows)
SET NODE_ENV=production
gulp build
If you added build script to your package.json - you can run npm:
npm run --production build
Or you can try to modify your scrips:
...
.transform(envify({
'process.env.NODE_ENV': 'production'
}))
...
But this depends on what version of envify you are using
You should use minified react version for production bundles. Minifying by yourself react code is not a solution, because there are many conditions like: process.env.NODE_ENV !== 'production' in it.
You can take a look here:
We provide two versions of React: an uncompressed version for development and a minified version for production. The development version includes extra warnings about common mistakes, whereas the production version includes extra performance optimizations and strips all error messages.
So you should use react.min.js from /node_modules/react/dist instead of /node_modules/react/lib/React for production bundles.

Protractor tests reports using jasmine-reporters

I'm trying to export protractor test results to xml files, for that I have installed jasmine-reporters using
npm install -g jasmine-reporters.
Protractor version is Version 2.1.0.
jasmine-reporters version 2.0.7
This is my protracotr config file:
exports.config = {
seleniumAddress: 'http://localhost:4455/wd/hub',
capabilities: {
'browserName': 'chrome'
},
specs: [
'student_spec.js'
],
onPrepare: function() {
require('jasmine-reporters');
jasmine.getEnv().addReporter(
new jasmineReporters.JUnitXmlReporter(null, true, true, '/test/e2e/JasmineReporter')
);
},
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 50000
}
};
When I run the protractor, I am getting this error
Error: Cannot find module 'jasmine-reporters'
Help me, where I'm doing wrong.
Make sure you have installed jasmine-reporters and the proper path of jasmine-reporters is provided. If it was installed properly then run the below command to see if you get the version of it -
npm list -g jasmine-reporters
If there was a problem installing it, use below command to install it which is compatible with Jasmine 2.x versions -
npm install --save-dev jasmine-reporters#^2.0.0
Update your conf.js file to include proper global scope variable jasmineReporters as mentioned in the package file -
framework: 'jasmine2',
onPrepare: function() {
var jasmineReporters = require('path_of_installed_jasmine-reporters-plugin');
//update proper path, in my case its ('/usr/local/lib/node_modules/jasmine-reporters')
jasmine.getEnv().addReporter(
new jasmineReporters.JUnitXmlReporter(null, true, true, '/test/e2e/JasmineReporter')
);};
Did you try:
var jasmineReporters = require('jasmine-reporters').jasmineReporters;
?
I think something like that should work.
Best regards
I also got the same issue as I installed jasmine locally.But when I installed jasmine as Globally it works fine for me.
npm install –g jasmine-reporters#^2.0.0
then,
npm list -g jasmine-reporters
After that run the conf.js again as mentioned above

Run karma tests in linux terminal

I'm building automation proccess for some AngularJS project. As part of flow I have to ran karma tests via grunt test on linux (CentOS) machine without GUI interface. I have no idea how karma works. Can I run it this way?
The karma-runner for grunt has an example of how to configure karma to run with phantom in CI mode(Run the tests once and then exit). You just have to use this configuration in your Gruntfile:
karma: {
unit: {
configFile: 'karma.conf.js',
runnerPort: 9999,
singleRun: true,
browsers: ['PhantomJS'],
logLevel: 'ERROR'
}
}
The browsers array tells karma that you want to use only PhantomJS(No GUI necessary).

Run distribution code using grunt

i am new in gruntjs. I have installed grunt. Also have app. I minified the code using grunt build. It created the /dist directory in which all minified versions of file is there.
How to run this distribution code(/dist) for production using grunt. Not able to figure it out. grunt serve command take /app directory by default
Use a package like grunt-contrib-connect (enter link description here). First install it:
$ npm install --save-dev grunt-contrib-connect
In your Gruntfile.js
grunt.loadNpmTasks('grunt-contrib-connect');
// ...
// In your config object
grunt.initConfig({
// ...
connect: {
dist: {
options: {
port: 8000,
base: 'dist',
keepAlive: true
}
}
}
// ...
});
Then run your grunt task:
$ grunt connect:dist
And navigate to http://localhost:8000 (or change port in config).
Note: if you use watch, set keepAlive: false.

Resources