We have built a complex web app with AngularJS. Now we need to add some unit tests (OK, you can say, this is too late, but, please, give it a try) and we use Grunt, Jasmine and Karma for this.
Our package.json contains these lines:
{
"devDependencies": [
"grunt-html2js": "0.3.2",
"jasmine-core": "^2.3.4",
"jasmine-spec-reporter": "^2.4.0",
"karma": "0.13.10",
"karma-jasmine": "0.3.6",
"karma-chrome-launcher": "0.2.0",
"karma-firefox-launcher": "0.1.6",
"grunt-karma": "0.12.1",
]
}
Our gruntFile.js:
grunt.loadNpmTasks('grunt-html2js');
grunt.loadNpmTasks('grunt-karma');
Our Karma config file contains:
// Karma configuration
// Generated Sep 2015
module.exports = function(config) {
config.set({
//more options
browserDisconnectTimeout: 4000,
client: {
captureConsole: true
},
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../../',
//no plugins!
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
{ 'pattern': 'dist/js/libs.js', included: true, served: true },
// some more
'test/unit/**/*.spec.js'
],
// more stuff
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
singleRun: true
});
};
This runs, we have created a suitable grunt task for it. Karma starts a server, a web browser (Chrome) and starts testing. I'd guessed, it loads the files in the <head>
of the index.html (or smth. similar), which Chrome browses. It turned out, that the whole stuff apparently happens in an <iframe>
in that web-page. Anyway.
Karma now stops working while claiming, that "require is not defined"
. This required can be found in our libs.js, which includes textAngular-rangy.min.js (created by bower
from textAngular, version 1.4.1). This awesome library is made for browsers or node.js, so I guess it checks, whether window
is there and switches to require
(node mode), if not. This should work fine in the instance of Chrome, which Karma starts.
Does anybody has an idea, why not and has a suggestion for a workaround?
There two related SO-Questions, but, basically, adding the require
module for Karma makes things even worse.