Karma tests failing in Webstorm Debugger - angularjs

I recently updated an AngularJS1.5 application to use typescript. My tests now run fine when running them using grunt test and they also run fine when running them through Webstorm. But when I try to debug any of the tests I get the Error: $injector:nomod error. Here is the Karma config I am using
{
webpack,
// base path, that will be used to resolve files and exclude
basePath: cwd,
captureTimeout: 1200000,
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: sourceFiles,
exclude: [],
// web server port
port: 3000,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// coverage reporting breaks Web Storm debugging
reporters: ['progress', 'junit', 'html' /*, 'coverage-istanbul'*/],
// 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: false,
preprocessors: {
[ scriptsEntry ]: [ 'webpack', 'sourcemap' ]
},
junitReporter: {
outputFile: '_dist/test-reports/unit-test-results/unit_tests.xml',
suite: ''
},
sourcemap: true,
mime: {
'text/x-typescript': ['ts','tsx']
},
htmlReporter: {
outputFile: '_dist/test-reports/unit-test-results/unit_tests.html'
}

Related

Angular unit tests with Karma/Jasmine - browser not defined

Whenever I run a unit test I get the error:
browser is not defined.
My config file is below - can anyone suggest the issue
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: [
'www-dev/js/ionic.bundle.js',
'node_modules/angular-mocks/angular-mocks.js',
'www-dev/js/app.js',
'www-dev/js/templates.js',
'www-dev/css/*.css',
{pattern: 'tests/unit/unit.js', nocache: true, watched: true}
],
// 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: {
'./www-dev/js/app.js': ['coverage']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress','coverage'],
coverageReporter: {
type : 'html',
dir : './tests/unit/'
},
// 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', 'Firefox', 'IE'],
// 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
})
}
To actually open a browser instance, Karma needs a browser launcher, which comes in the form of a plugin.
From Karma's browser docs:
Note: Most of the browser launchers need to be loaded as plugins.
Have you installed the correct launcher plugins?
https://github.com/karma-runner/karma-chrome-launcher
https://github.com/karma-runner/karma-firefox-launcher
https://github.com/karma-runner/karma-ie-launcher

Karma Not Running - cannot find module gulp

I'm trying to set up karma tests with Jasmine on my Angular project.
I can't get basic test
`it("dummy", function() {
expect(true).toBe(true);`
to run, and I'm getting the error:
PhantomJS 1.9.8 (Linux 0.0.0) ERROR
Error: Cannot find module 'gulp'
This is my gulp test task:
gulp.task('test', function(coverage) {
gulp.src('dummy')
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
});
});
And this is my karma conf file:
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: ['browserify', 'jasmine'],
plugins : [
'karma-browserify',
'karma-jasmine',
'karma-phantomjs-launcher'
],
preprocessors: {
'src/app/**/*.spec.js': [ 'browserify' ],
'src/components/**/*.spec.js': [ 'browserify' ],
'src/services/**/*.spec.js': [ 'browserify' ]
},
// list of files / patterns to load in the browser
files: [
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/**/*.js',
'src/**/*.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: ['PhantomJS'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
Please help me stackoverflow you're my only hope!
The most common question: did you install gulp correctly?
npm install gulp
Also check which files you are loading into the files property in karma.conf.js. There should be only your 3rd party libraries js files, your custom js files and your test files.
There's also a chance that your gulp task is not correct but since I often use grunt, I can't really help in that part :P

karma jasmine tests do not run on Firefox 32.0.x

my karma jasmine unit tests do not run with Firefox verion 32.0.x.
here is my karma.conf
module.exports = function (config) {
config.set({
preprocessors: {
// make html files accessable within a karma test
'app/views/**/*.html': ['ng-html2js'],
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'app/scripts/**/*.js': ['coverage']
},
// 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/json3/lib/json3.js',
'app/bower_components/es5-shim/es5-shim.js',
'app/bower_components/jquery/dist/jquery.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-translate/angular-translate.js',
'app/bower_components/bison-corporatedesign/directives/headerbar.js',
'app/bower_components/angular-grid/ng-grid-2.0.13.debug.js',
'app/bower_components/angular-ui-utils/ui-utils.js',
'app/bower_components/angular-ui-router/release/angular-ui-router.js',
'app/bower_components/angular-animate/angular-animate.js',
'app/bower_components/AngularJS-Toaster/toaster.js',
'app/bower_components/angular-google-chart/ng-google-chart.js',
'app/bower_components/socket.io-client/dist/socket.io.min.js',
'app/bower_components/angular-dynamic-locale/src/tmhDynamicLocale.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'app/scripts/directives/*.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/spec/**/*.js',
'app/views/**/*.html'
],
reporters: ['progress', 'junit', 'coverage'],
// list of files / patterns to exclude
exclude: ['./**/*.js.map'],
// web server port
port: 9001,
// 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: ['PhantomJS', 'Firefox'],
junitReporter: {
outputFile: 'distribution/junit/TESTS-results.xml'
},
coverageReporter: {
type: 'cobertura',
dir: 'distribution/cobertura/'
},
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});};
i use the karma-firefox-launcher.
When i run grunt karma firefox will be startet up but the test failed because there is no communication.
error-log on Firefox console:
Error: Permission denied to access object
Error: Permission denied to access property 'result'
any idea how to solve this? older versions worked well.

Webstorm karma/jasmine angularjs testing - reference error module is undefined

I'm trying to set up WebStorm to develop AngularJS. I've followed the instructions from JetBrains and I'm trying to run the test from that tutorial.
describe('PhoneCat controllers', function() {
beforeEach(module('phonecatApp'));
describe('PhoneListCtrl', function(){
it('should create "phones" model with 3 phones', inject(function($controller) {
var scope = {},
ctrl = $controller('PhoneListCtrl', { $scope: scope });
expect(scope.phones.length).toBe(3);
}));
});
});
I'm getting the following error
"C:\Program Files (x86)\nodejs\node.exe" "C:\Program Files (x86)\JetBrains\WebStorm 7.0.3\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\L\node_modules\karma --serverPort=9876 --urlRoot=/
Testing started at 21:02 ...
ReferenceError: module is not defined
at null.<anonymous> (C:/Users/L/WebstormProjects/AngularJS/test/unit/ControllerTests.js:4:16)
at C:/Users/L/WebstormProjects/AngularJS/test/unit/ControllerTests.js:3:1
Process finished with exit code 0
My directory layout is
test/unit/ControllerTest.js
test/karma.conf.js
karma.conf.js below
// Karma configuration
// Generated on Sun Feb 16 2014 20:49:06 GMT+0000 (GMT Standard Time)
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: [
{pattern: '*.js', included: true},
{pattern: 'unit/*.js', included: true}
],
// 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 (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: ['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
});
};
angular-mocks.js is missing
See http://devnet.jetbrains.com/thread/452824?tstart=0
in karma.conf.js should change to :
basePath: './',
$files: [
{pattern: './*.js', included: true},
{pattern: './unit/*.js', included: true}
],

AngularJS + Grunt + Karma + E2E

I tried to run E2E tests with Grunt Karma, but no success. I've look many solutions, but no one worked!
My karma-e2e.conf.js:
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '../',
// frameworks to use
frameworks: ['ng-scenario'],
// list of files / patterns to load in the browser
files: [
'test/e2e/**/*.js',
'test/e2e/*.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: 9877,
// 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: ['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,
// Uncomment the following lines if you are using grunt's server to run the tests
proxies: {
'/': 'http://localhost/test/e2e/'
},
// URL root prevent conflicts with the site root
urlRoot: '/_karma_/'
});
};
PS: My app run at port 80 (Apache default).
My spec is as follows:
describe('E2E: Testing Routes:', function () {
'use strict';
beforeEach(function() {
browser().navigateTo('/');
});
it('should jump to the /videos path when / is accessed', function() {
browser().navigateTo('#/');
expect(browser().location().path()).toBe("/main");
});
})
So, when a I ran this spec, I got this message:
"Type Error: undefined is not a function (evaluating $document.injector())"
This error occurs at line "expect(browser().location().path()).toBe("/main");"
Any idea?
Make sure your running your web server an also karma. Try changing your proxy settings to something like this:
proxies: {
'/': 'http://localhost:8000/'
},
You need
plugins: [
'karma-ng-scenario',
],
in your config file.
Do
npm install karma-ng-scenario --save-dev
first

Resources