Karma: Continuous Integration mode shuts down browser - javascript

I'm using karma for my angularjs test. When I run npm test my tests are executed, but then, the browser shuts down. I've tested it with Chrome, PhantomJS, Safari and Firefox. Here is my karma.conf.js.
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
"app/bower_components/jquery/jquery.js",
"app/bower_components/es5-shim/es5-shim.js",
"app/bower_components/angular/angular.js",
'app/bower_components/angular-mocks/angular-mocks.js',
// 'app/bower_components/angular-scenario/angular-scenario.js',
"app/bower_components/json3/lib/json3.min.js",
"app/bower_components/sass-bootstrap/dist/js/bootstrap.js",
"app/bower_components/angular-resource/angular-resource.js",
"app/bower_components/angular-cookies/angular-cookies.js",
"app/bower_components/angular-sanitize/angular-sanitize.js",
"app/bower_components/angular-route/angular-route.js",
"app/bower_components/angular-touch/angular-touch.js",
"app/bower_components/ngstorage/ngStorage.js",
"app/bower_components/underscore/underscore.js",
"app/bower_components/fastclick/lib/fastclick.js",
'app/scripts/*.js',
'app/scripts/**/*.js',
// 'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome', 'Firefox', 'Safari', 'PhantomJS'],
plugins : [
'karma-junit-reporter',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-script-launcher',
'karma-jasmine'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
}
});
};
When changing the logLevel: config.LOG_DEBUG I get the following output on my terminal:
PhantomJS 1.9.7 (Mac OS X): Executed 1 of 1 SUCCESS (0.007 secs / 0.028 secs)
DEBUG [karma]: Run complete, exitting.
DEBUG [launcher]: Disconnecting all browsers
DEBUG [launcher]: Process PhantomJS exited with code 0
I thought, that the singleRun: false would prevent the browser instance from shutting down. What I'm doing wrong?

I have just tested that on my PC and on my Mac an in both cases the browser does not close. Moreover, if I close the Chrome window, Karma opens up a new one right away.
Are you using the latest version of Karma?
In your case does the karma process exit?

Related

ReferenceError: browser is not defined - Using Karma and Jasmine

I am getting ReferenceError: browser is not defined when trying to set up some basic tests with Jasmine and Karma.
Here is my Karma config file
// Karma configuration
// Generated on Wed Mar 08 2017 13:29:09 GMT+0000 (GMT)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'./spec/**/*.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma- preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
};
And then I have one test file called loginSpec.js shown here:
describe('login page', function() {
beforeEach(function(){
browser().navigateTo('/');
});
it('should have the correct title', function() {
expect(browser.getTitle()).toEqual('Title');
});
});
Whenever I run the test in the Karma tool window I get the following error:
ReferenceError: browser is not defined
at Object.<anonymous> (spec/loginSpec.js:11:9)
ReferenceError: browser is not defined
at Object.<anonymous> (spec/loginSpec.js:15:16)
I don't understand why browser is suddenly not defined as I had tests working before setting up Karma (using WebdriverIO and selenium-standalone). These tests were written in the same way and there were no errors regarding browser
I have also researched and found that a lot of other people have had the same issue, but they are having it because of issues with Angular, which i am not using?
Using karma,jasmine there is no handler/object as browser available like WebdriverIO and selenium-standalone.
If you are looking to write a unit test case using jasmine to test the route, it can be done via injecting $location service of angular as below:
describe('login page', function() {
beforeEach(angular.mock.inject((_$rootScope_, _$location_) => {
_$location_.path('/');
_$rootScope_.$digest();
}));
it('should have the correct title', function() {
expect(browser.getTitle()).toEqual('Title');
});
});

AngularJS with Karma: $controller not defined error

I'm very new to testing but thought it was a good idea to start out some testing with this project. When I run grunt karma:watch I get the error with this configuration files.
I have a config file containing:
module.exports = function(config) {
config.set({
basePath: '../..',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'src/js/vendors/angular.js',
'src/js/vendors/angular-mock.js',
'src/js/app/navigation/*js',
'src/**/*.js',
'src/js/vendors/*.js',
'src/test/unit/**/*.spec.js',
'dist/templates/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
And my unit test looks like this:
describe('NavCtrl', function(){
var scope;
beforeEach(angular.mock.module('integra'));
beforeEach(angular.mock.inject(function($rootScope,$controller){
scope = $rootScope.$new();
$controller('NavCtrl', {$scope: scope});
}));
$scope.type = {
language: "English",
i18n: "en_EN"
};
$scope.option(type);
expect($scope.type.i18n).toEqual('en_EN');
})
This is the error:
Chrome 33.0.1750 (Linux) ERROR
Uncaught ReferenceError: $controller is not defined
at ~/Project/theApp/src/test/unit/app/navigation/NavCtrl.spec.js:2
Why isn't the $controller defined? Where should I define it? The controller I wan't to test is located in /src/js/navigation/NavCtrl.js
Error in path to controller's file in the configuration file.
Used
files: [
...,
'src/js/app/navigation/*js',
...
],
Instead of
files: [
...,
'src/js/app/navigation/*.js',
...
],

Tests with Karma not finishing

I have been trying to run some tests with karma but have not been able to get it to work. After trying to get it to work with my application, I tried running it with the most simple possible test, and yet, it still does not finish.
Here is my karma.conf.js file:
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
// 'app/bower_components/angular/angular.js',
// // 'app/bower_components/angular/angular-mocks.js',
// 'app/scripts/*.js',
// 'app/scripts/controllers/main.js',
// // 'test/mock/**/*.js',
// 'test/spec/**/*.js',
// // 'app/scripts/**/*.js,'
'testing.js'
],
// list of files / patterns to exclude
exclude: ['angular-scenario.js'],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
and here is the testing.js file :
describe("hello", function(){
it("Should fail automatically", function(){
expect(false).toBe(true)
});
});
The result I get without fail is:
$ karma start karma.conf.js
INFO [karma]: Karma v0.10.9 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 30.0.1599 (Mac OS X 10.7.5)]: Connected on socket dGANukHhgkPC3YyhyUrU
INFO [Chrome 30.0.1599 (Mac OS X 10.7.5)]: Connected on socket HC8iGMv-VmeYX2UAyUrV
It never seems to go on and tell me how many of my tests succeeded or not.
Thanks for all of the help. Hopefully I will have some hair left at the end of this, haha.
This is the expected behaviour.
single_run : true means run the tests once and exit.
single_run : false means don't exit (this is not obvious).
autowatch : true means trigger running tests when a file changes.
If you have both autowatch and single_run set to false (like you do), then running
karma start
will just have karma initialize and wait for something to trigger it to run.
In order to trigger running the tests you need to open a new console and do
karma run
(Or set one of the above mentioned flags to 'true')
I ran into the same problem. I ended up installing the version of node that was suggested here: http://karma-runner.github.io/0.10/intro/faq.html. So now, I am running node.js version 0.10.38 with karma server version 1.4.28 and Chrome is happy with that.

Karma opens three times in Chrome. Karma Bug?

For some reason, Karma tries three times to open Chrome, and thinks it's unsuccessful. It works fine once it loads, and shows that it connects on two sockets.
I have Chrome installed in a different location than Karma defaults to, so I had to link to the absolute path of Chrome. I suspect this may have something to do with why it doesn't "see" that Chrome is running. Is this a bug or is there something I can do to fix this?
Here is my config:
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'../../Scripts/angular.js',
'../../Scripts/angular-*.js',
'*.spec.js',
'*.js'
],
// list of files to exclude
exclude: [
'../../Scripts/angular-scenario.js',
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ["C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
For me, closing the browser window by pressing the X button would cause this issue to occur. There is a flag called retryLimit which is set to 2 by default, see http://karma-runner.github.io/2.0/config/configuration-file.html.
If you set that to zero, by adding the line
retryLimit : 0,
somewhere in your config.set structure, the browser doesn't pop up after you close it. Though, the fact that that flag exists makes me think that you are normally supposed to close karma by some other means...
I think instead of providing the absolute path, you should set the path to chrome binary in your environmental variables.
You can set it manually, or follow the instructions here:
http://karma-runner.github.io/2.0/config/browsers.html

Mocha - Chai Karma "suite is not defined"

I'm quite new to jscript tdd and got a problem, hope someone can show me what I'm doing worng.
Running the Tests in a browser (via HTML File) everything works fine. running them through node and karma i got the following exception
I want to use Mocha and Chai within karma in node.js host.
I installed via npm install [...] --save-dev mocha and karma-mocha
I've a testlibrary like this
suite('first suite', function () {
test('SuccessTest', function () {
expect(1).to.equal(1);
});
test('FailTest', function () {
expect(1).to.equal(2);
});
});
in node i used karma init to create the config file in which i set frameworks to
frameworks: ['mocha','chai'],
now when I run karma it got this error
"suite is not defined"
I assumed that declaring mocha and chai as frameworks this should have worked?
I also installed in node the karma-mocha and karma-chai plugins.
What do I wrong and what do I have to do ?
where the whole karma config file
// Karma configuration
// Generated on Mon Sep 23 2013 17:24:19 GMT+0200 (Mitteleuropäische Sommerzeit)
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['mocha','chai'],
// list of files / patterns to load in the browser
files: [
'tests.js'
],
// list of files to exclude
exclude: [
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
I also tried to add mocha.js and chai.js to the file load list but this didn't help
files: [
'mocha.js',
'chai.js',
'tests.js'
],
When I change tests to jasmine it works.
This is because there is no "chai" framework/plugin for Karma, but I think it's a good idea to have one.
You need to do this in some of your included files, in order to use "tdd" mocha style ("bdd" is the default one):
// in config-mocha.js
window.mocha.setup({ui: 'tdd'});
You need to load "chai" manually:
module.exports = function(config) {
config.set({
files: [
'path/to/chai.js',
'config-mocha.js',
// .. your source and test files
]
});
};

Categories