Karma: browser idle - karma-runner

I am trying Karma for the first time, and after several hours I still can't get it to work.
When I run the test by typing karma start karma.conf.js in the terminal, the browser window opens and displays the following (I have also tried with Chrome with the same result):
This is the terminal output:
29 07 2015 16:27:12.835:INFO [karma]: Karma v0.13.3 server started at http://localhost:9876/
29 07 2015 16:27:12.852:INFO [launcher]: Starting browser Firefox
29 07 2015 16:27:15.866:INFO [Firefox 33.0.0 (Windows 7 0.0.0)]: Connected on socket HA1RSN-QsWuAO7NIAAAA with id 26755366
My karma.conf.js file is located at the root of my Node.js project and looks like this:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'tests/unit/test.js'
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Firefox'],
singleRun: false
})
}
My test.js file looks like this (example test from a book, my actual tests will the Angular.js tests):
describe("First Test", function () {
var counter;
beforeEach(function () {
counter = 0;
});
it("increments value", function () {
counter++;
expect(counter).toEqual(1);
});
it("decrements value", function () {
counter--;
expect(counter).toEqual(0);
});
});
I'm using Node.js version 0.12.05.
I appreciate any help as I feel really lost here.

You need to trigger a test run, if you want it to execute. There are two ways to do this
Run karma run karma.conf.js in a second terminal window in the same working directory
Change the option singleRun to true, this way it will start, execute the test and then exit.

Related

Karma and Jasmine: Can't find variable: require

This is my first time attempting to unit test some code and I'm getting this error when using Karma and Jasmine:
PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
ReferenceError: Can't find variable: require
at unit/tests.js:1
I tried npm install karma-browserify --save-dev but that didn't solve the issue.
Any idea how to sort this?
My Karma conf file:
// Karma configuration
// Generated on Tue Nov 08 2016 03:14:50 GMT+0000 (GMT Standard Time)
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: [
// '../shopping-basket.js',
'unit/*.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/**/*.js': ['browserify']
},
// 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: ['PhantomJS'],
//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
})
}
tests.js
var myCode = require('./shopping-basket-functions');
describe('tests', function(){
describe('testFunction', function(){
it('should return 1', function(){
// Call the exported function from the module
myCode.testFunction().should.equal(1);
})
})
})
shopping-basket-functions.js
function testFunction () {
return 1;
}
// If we're running under Node,
if(typeof exports !== 'undefined') {
exports.testFunction = testFunction;
}
Have you tried using the template here: https://www.npmjs.com/package/grunt-template-jasmine-nml as specified in this answer?
Long story short, what it does is allow you to use the CommonJS syntax that NodeJS uses in order to reference and pull in other modules. See an example below of how you would reference it in your jasmine config file (again, per the other user's answer) :
jasmine: {
options: {
template: require('grunt-template-jasmine-nml'),
helpers: 'spec/helpers/**/*.js',
specs: 'spec/**/*.spec.js'
}
}
had same issue trying to build an app, phanotmjs kept on creeping the undefined variable require, solved it by:
npm install grunt-template-jasmine-nml --save-dev
this will update the right template and it works after.
trying:
grunt test -dd finished without error

Selective test execution in Karma Jasmine using pattern matching

I'm having trouble invoking the command line option on karma-jasmine which allows for the execution of only those tests which match a given pattern. My spec reads as follows:
/path/to/single-test/main.spec.js
describe('my first test suite', function() {
it('always passes', function() {
expect(true).toBe(true);
});
it('still always passes', function() {
expect(true).toBe(true);
});
});
I'm assuming the description (for example "still always passes") is the item against which the pattern specified by the grep command line option is matched. When I attempt to run the second example based on the fact that its description is the only example containing the word "still", both examples are executed instead of just the one:
$ karma start -- --grep=still
INFO [karma]: Karma v0.12.35 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket 7Dn7Ez1Reap7ch0Uzsb0 with id 44623726
PhantomJS 1.9.8 (Linux 0.0.0): Executed 2 of 2 SUCCESS (0.002 secs / 0.001 secs)
How do I execute just this one example based on a pattern? The official documentation doesn't give a sample of the usage of the pattern matching option.
I read in the discussion of a pull request, that the grep option can be used in conjunction with "fit" and "fdescribe." This works when tested. However, in the case of using grep with "fit", what's the purpose of the pattern argument to the grep option? (It would be nice to be able to execute tests selectively without the need to augment source code!)
Here is the remainder of the files in my project for reference:
/path/to/single-test/karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: ['*.spec.js'],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
singleRun: true
});
};
/path/to/single-test/package.json
{
"name": "single-test",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"jasmine-core": "^2.3.4",
"karma": "^0.12.35",
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^0.2.0",
"phantomjs": "^1.9.17"
}
}
You have to start a Karma server, then specify the --grep option in a Karma runner. I.e. something along the lines of:
karma start path/to/karma.conf.js
Then in another terminal:
karma run path/to/karma.conf.js -- --grep=still
It is important that you set singleRun: false in the configuration options.
There is karma-jasmine-spec-tags plugin which helps to filter running tests by tags in their names.
Example usage:
$ karma start --tags smoke
$ karma start --skip-tags slow,bench
$ karma start --tags bench --skip-tags slow
$ karma start --tag-prefix 'scope:' --tags critical
Where a spec is following:
describe('Example test', () => {
it('should be a #smoke test', () => {
// ...
});
it('#slow test', () => {
// ...
});
})
describe('Performance test suite #bench', () => {
it('#fast #smoke test', () => {
// ...
});
it('#slow test', () => {
// ...
});
})
describe('Custom tag prefix', () => {
it('test scope:critical', () => {
// ...
});
})

Running protractor tests on multiple browsers with browser stack

I am trying to run E2E tests on multiple browsers on browser stack, I took the reference from
E2E testing on multiple/parallel browsers in Protractor?
and
Internet Explorer Selenium protractor e2e tests
but the error I get every time I try to run the tests -
ERROR - Unable to start a WebDriver session.
C:\MrWhiteMVP\whitemvp-integrationtests_develop\node_modules\gulp-protractor\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113
var template = new Error(this.message);
^
UnknownError: Authorization required
at new bot.Error (C:\MrWhiteMVP\whitemvp-integrationtests_develop\node_modules\gulp-protractor\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18)
at Object.bot.response.checkResponse (C:\MrWhiteMVP\whitemvp-integrationtests_develop\node_modules\gulp-protractor\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:103:11)
But if I run the tests on 1 browser, then it works perfectly fine. this is how my conf file looks like
'use strict';
exports.config = {
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
multicapabilities: [{
'browserstack.user': 'testuser',
'browserstack.key': 'testkey',
// Needed for testing localhost
// 'browserstack.local': 'true',
// Settings for the browser you want to test
'browserName': 'chrome',
'version': '36.0',
'os': 'OS X',
'os_version': 'Mavericks',
'resolution': '1024x768'
},
{
'browserstack.user': 'testuser',
'browserstack.key': 'testkey',
// Needed for testing localhost - 'browserstack.local': 'true',
// Settings for the browser
'browserName': 'firefox',
'os': 'windows'
}],
baseUrl: 'http://origin-develop.mvp.livebranches.com',
// The require statement must be down here, since jasmine-reporters
// needs jasmine to be in the global and protractor does not guarantee
// this until inside the onPrepare function.
onPrepare: function()
{
require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter('xmloutput', true, true));
},
},
};
could anyone please tell me what am I doing wrong here, also we use gulp ti run specs but my problem is it is saying not even going beyond authentication.
I think that first of all, you have an extra comma on your config.
onPrepare: function()
{
require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter('xmloutput', true, true));
},
}, <-----
};

Testing async with karma

I am trying to set up some async tests using karma and jasmine. I am clearly making a very stupid mistake but I need it pointing out to me. After simplifying as much as possible I have the following:
package.json
{
"name": "newtest",
"version": "0.0.0",
"scripts": {
"test": "karma start karma.conf.js"
},
"devDependencies": {
"karma": "^0.12.28",
"karma-chrome-launcher": "^0.1.5",
"karma-jasmine": "^0.2"
}
}
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'tests/**/*.js'
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: true
});
};
tests/dummy.spec.js
describe("Testing async", function() {
it('should fail this test', function(done) {
setTimeout(function(){
expect(1).toBe(2);
done();
}, 1000);
});
it('should not fail this test', function(done) {
done();
});
});
and I am getting the following:
npm test
> newtest#0.0.0 test /home/mark/Projects/newtest
> karma start karma.conf.js
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 39.0.2171 (Linux)]: Connected on socket T7j6LvNAwvS89wUdymCb with id 16891024
Chrome 39.0.2171 (Linux) Testing async should not fail this test FAILED
TypeError: undefined is not a function
at null.<anonymous> (/home/mark/Projects/newtest/tests/dummy.spec.js:12:5)
Chrome 39.0.2171 (Linux): Executed 2 of 2 (1 FAILED) (0.007 secs / 0.005 secs)
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
So the test that I think should fail is passing fine, and vice-versa. Can someone point me to my error(s)?
I guess the first one fail because when the timeout is reach the test is allready finish, so both doesn't work.
It's has you'r not using jasmine 2.
The syntax seems good to me, what I can tell you is the difference with my configuration (wich is working) :
I have put karma-jasmine in dependencies with a ~ :
"dependencies": {
...
"karma-jasmine": "~0.2.0"
},
I'm using PhantomJS :
browsers:['PhantomJS'],
I don't know why I am having the problem, but I have tried on another machine and it works as expected.

karma-runner error: Cannot call method 'invoke' of undefined

My karma runner is confusing me. Some tests run just fine but others, seemingly similar, don't.
This works:
it('should display an error when accessing /business-user without authentication', function() {
browser().navigateTo('../business-user');
expect(element('p#flash').text()).toBe('You must be logged in to access that page.');
});
This does not:
it('should take the user to the index lander upon logout', function() {
browser().navigateTo('../business-user/login');
input('username').enter('matt');
input('password').enter('letmein');
element('input:last').click();
element('a#logout').click();
expect(browser().window().path()).toBe('/');
});
Running the test as displayed gives me an error that a#logout cannot be found. I paused the runner to verify that a#logout was there, and it was. When I resume, I get the "TypeError: Cannot call method 'invoke' of undefined" error.
I would think the object that should be defined is the element, but why wouldn't it say that it can't find the element? And more importantly, why can't it find it in the first place?
Obligatory karma-e2e.conf.js dump:
// Karma configuration
// Generated on Tue Aug 20 2013 17:01:18 GMT-0600 (MDT)
module.exports = function(config) {
config.set({
basePath: '../..',
frameworks: ['ng-scenario'],
files: [
'test/e2e/*.js'
],
exclude: [
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
captureTimeout: 60000,
singleRun: true,
proxies: {
'/': 'http://localhost:3000/'
},
urlRoot: '/_karma_/'
});
};
Any ideas?