karma.conf.js uncaught referencerror: google no defined - javascript

when i try running the karma test runner, i'm getting a error as the following from one of my files, saying that my library google is undefined???
Chrome 36.0.1985 (Mac OS X 10.9.4) ERROR
Uncaught ReferenceError: google is not defined
at /Users/giowong/rails_project/doctible_pre_treatment/app/assets/javascripts/angular-google-maps.min.js:7
my karma.conf.js file
// Karma configuration
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '/Users/giowong/rails_project/doctible_pre_treatment/',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/assets/components/angular/angular.js',
'app/assets/components/angular-mocks/angular-mocks.js',
'app/assets/components/angular-resource/angular-resource.js',
'app/assets/components/angular-payments/lib/angular-payments.js',
'app/assets/components/ng-file-upload/angular-file-upload.js',
'app/assets/components/ngDialog/js/ngDialog.js',
'app/assets/components/angular-route/angular-route.js',
'app/assets/components/angular-loading-bar/src/loading-bar.js',
'app/assets/javascripts/angular/filters/widget-filters.js',
'app/assets/components/angular-animate/angular-animate.js',
'app/assets/javascripts/angular/directives/loader.js',
'app/assets/javascripts/angular/directives/focus.js',
'app/assets/javascripts/angular/directives/checkout-form.js',
'app/assets/javascripts/angular/directives/provider-form.js',
'app/assets/components/angular-ui-utils/ui-utils.js',
'app/assets/components/angular-sanitize/angular-sanitize.js',
'app/assets/components/angular-bootstrap/ui-bootstrap.js',
'app/assets/components/angular-ui-map/ui-map.js',
'app/assets/components/underscore/underscore.js',
'app/assets/components/jquery-1.11.1.min.js',
'app/assets/javascripts/*.js',
'app/assets/javascripts/**/**/*.js',
'spec/javascripts/*.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: true,
// 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: true
});
};
i tried google already and no luck so far. I've tried making a test file and defining google itself. Any help is appreciated

You get this error because you use google maps v3 API on your application but do not have any code, which will initialize window.google property, when you run your tests.
Solution 1
Compile this mock to javascript and reference compiled *.js in files of your config.
Solution 2
Create your own google-mock.js
Add it to files array of your karma.config
Add stab to google-mock.js for each Google Maps API call which you use in your app
like in example below:
(function(){
window.google = window.google || {
maps: {
Map: function(){},
// and so on...
}
};
})();
Solution 3
Open this link
Save ALL text from this page to file google-maps-api.js
Add path to this file to your files array in karma.config

In my case in the files section of karma.conf.js I needed the google api with "js" extension (not .ts!):
files:[
'src/app/shared/mocks/google-api.js'
],
I have copied and pasted the real google API from the url:
"https://maps.googleapis.com/maps/api/js?sensor=false"
If you pass the url it won´t work.
It´s not the a cool solution, but for now it works at least.

Related

karma testing - services injection error

i have inhereted a angular project that uses npm, grunt, bower ... and karma + jasmin.
i have been asked to setup some tests for the project using karma and jasmin.
karma has already been setup in the project but never used.
when i run 'grunt test' i get injection errors on all of the services, like the following.
Error: [$injector:unpr] Unknown provider: excelparserserviceProvider <- excelparserservice
http://errors.angularjs.org/1.2.6/$injector/unprp0=excelparserserviceProvider%20%3C-%20excelparserservice
there is already a karma.conf.js looking like this.
i havent changed anything in the karma.conf file, except adding some of the libaries that used in the project into the list under Files: [].
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-mocks/angular-mocks.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',
// i manually added the ones from here
'app/bower_components/jquery/dist/jquery.js',
'app/bower_components/geocoder-js/dist/geocoder.js',
'app/bower_components/js-xlsx/dist/xlsx.core.min.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'app/bower_components/d3/d3.js',
'app/bower_components/angular-file-upload/angular-file-upload.js',
// to here.
'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,
// 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
});
};
the paths,
'app/scripts/*.js' --> leads to app.js and a config.js
'app/scripts/**/*.js' -->leads to all services controllers and directives
'test/mock/**/*.js' --> is non existent
'test/spec/**/*.js'--> contains all the test files
there are test files corresponding to every part of the applikation. Which i have been told has been auto generated. So i find it weird if they should contain the error. but the one related to the excpelparserservice injection error is.
'use strict';
describe('Service: excelparserservice', function () {
// load the service's module
beforeEach(module('batchUploadApp'));
// instantiate service
var Excelparserservice;
beforeEach(inject(function (_excelparserservice_) {
Excelparserservice = _excelparserservice_;
}));
it('should do something', function () {
expect(!!Excelparserservice).toBe(true);
});
});
the declaration of the service looks like this.
'use strict';
angular.module('batchUploadApp')
.service('ExcelParserService',
function ExcelParserService($q, ExcelvalidationService, GeoLocationService) {
the application in general works.
hope i my explication is usefull :)
thank you.
You define and register your service like this:
angular.module('batchUploadApp').service('ExcelParserService', ...
meaning that you register it under the name ExcelParserService. On the other hand, when you try to inject the service into you test, you use its name in lowercase:
beforeEach(inject(function (_excelparserservice_) {
Both names must match, thus the solution is to change the name of the parameter:
beforeEach(inject(function (_ExcelParserService_) {

Karma gives me errors when running tests (related to backbone.js)?

My error is related to backbone.js 'undefined' is not an object (evaluating '_.each') it's pointing to line 227 which is inside my test.js script which was compiled by browserify.
This line is a built-in block within backbone, so the actual issue can't be within that line obviously.
So my setup... I am testing a simple module here, it's Inside ./client/src/views/.
// intro.js
var Backbone = require('backbone');
module.exports = IntroView = Backbone.View.extend({
el: '.intro',
initialize: function() {
this.render();
},
render: function() {
this.$el.height($(window).height() - 10);
}
});
Then I have the actual test module inside ./client/spec/views/
// intro.test.js
var IntroView = require('../../src/views/intro.js'),
$ = require('jquery');
describe('view:intro', function() {
var introView = new IntroView();
it('just testing if I can get a success', function() {
expect(1 + 2).toEqual(3);
});
it('should render the view', function() {
introView.render();
});
});
Okay so I run gulp browserify to compile the script like this
gulp.task('browserify:tests', function() {
return gulp.src('./client/spec/views/intro.test.js')
.pipe(tasks.browserify({
transform: ['hbsfy', 'browserify-shim']
}))
.pipe(tasks.concat('tests.js'))
.pipe(gulp.dest('./test'));
});
Then inside my karma.config.js
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: [
'./test/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: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['PhantomJS'],
// 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: true
});
};
Lastly I run karma start in the command line. So I hope I am using karma the right way, I think I am but if I want to test my code which uses backbone I need to get past this hump.
So my question is what am I doing wrong, why is the error occurring within the backbone.js code? Also am I doing it the right way?
I had the same error, but it was caused by using modules maping in my requirejs config. I've solved it by using paths instead (i've overwrite my browser config).
Your problem is most likely that you didn't have requirejs config. See requirejs config page for further instructions.

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