I have an apache cordova project setup and I'm trying to use Karma for the first time. I've started the project like this:
karma start test/karma.conf.js --verbose
But all I'm getting is:
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Windows 8.1 0.0.0)]: Connected on socket 0_IDnS6qvPg4OhGd7oq4 with id 66015767
Chrome 43.0.2357 (Windows 8.1 0.0.0): Executed 0 of 0 ERROR (0.002 secs / 0 secs)
I've double-checked what I've done against the docs for configuring requirejs for karma.
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '..',
frameworks: ['jasmine', 'requirejs'],
files: [
{pattern: 'www/lib/ionic/js/*.min.js', included: false},
{pattern: 'www/lib/ionic/js/angular/*.min.js', included: false},
{pattern: 'www/lib/ionic/js/angular-ui/*.min.js', included: false},
{pattern: 'www/js/**/*.js', included: false},
{pattern: 'test/specs/*Spec.js', included: false},
{pattern: 'test/test-app.js', included: true}
],
exclude: [
'www/js/app.js'
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
test-app.js
var tests = [];
for(var file in window.__karma__.files) {
if(window.__karma__.files.hasOwnProperty(file)) {
if(/Spec\.js$/.test(file)) {
tests.push(file);
}
}
}
requirejs.config({
baseUrl: 'www',
paths: {
'angular': 'lib/ionic/js/angular'
,'angular-ui': 'lib/ionic/js/angular-ui'
},
shim: {
'angular': {exports: 'angular'},
'www/js/controllers': { deps: ['angular']}
},
deps: tests,
callback: window.__karma__.start
})
helloSpec.js
define('helloTests', ['angular'], function() {
describe('UnitTest: Hello', function() {
it('is defined', function() {
expect([1,2,3].length).toEqual(3);
});
});
});
And here's the resulting debug.html:
<!doctype html>
<html>
<head>
<title>Karma DEBUG RUNNER</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
window.__karma__ = {
info: function(info) {
if (info.dump && window.console) window.console.log(info.dump);
},
complete: function() {
if (window.console) window.console.log('Skipped ' + this.skipped + ' tests');
},
store: function() {},
skipped: 0,
result: window.console ? function(result) {
if (result.skipped) {
this.skipped++;
return;
}
var msg = result.success ? 'SUCCESS ' : 'FAILED ';
window.console.log(msg + result.suite.join(' ') + ' ' + result.description);
for (var i = 0; i < result.log.length; i++) {
window.console.error(result.log[i]);
}
} : function() {},
loaded: function() {
this.start();
}
};
window.__karma__.config = {"args":[],"useIframe":true,"captureConsole":true};
// All served files with the latest timestamps
window.__karma__.files = {
'/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/requirejs/require.js': 'f9d7ac1ba78c53a51fd346e3901a5f406f060f44',
'/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-requirejs/lib/adapter.js': 'f2d8d5976c2bbe89ebe046ac51f393f5547bbc3b',
'/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/jasmine-core/lib/jasmine-core/jasmine.js': '578a1e5ff14db21b04e2d6db7fd0eda37042440c',
'/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/boot.js': '997181251903c5bcc9659d92edc872a2a4abfa7b',
'/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js': '0a69ad1fa10cc8be0a12b241227d3b834f82db4f',
'/base/www/lib/ionic/js/ionic-angular.min.js': '44040e6eeb17077cc79b0985118b15bf1420217d',
'/base/www/lib/ionic/js/ionic.bundle.min.js': '4a4bfece5434ae5c4cebab055aaae274f82050d9',
'/base/www/lib/ionic/js/ionic.min.js': '3e42a6e6700ba9982583b6b5da48861d202d8cfe',
'/base/www/lib/ionic/js/angular/angular-animate.min.js': '6df7f913a45a779638fd60cce18eee17268460d2',
'/base/www/lib/ionic/js/angular/angular-resource.min.js': 'eca9d7e3b12d62b9da13216f1d3b39fcd411f860',
'/base/www/lib/ionic/js/angular/angular-sanitize.min.js': 'b8cf1b04b40df803c16edda80c431ec422b63ad8',
'/base/www/lib/ionic/js/angular/angular.min.js': '09028d3553206017f5ced7249be5641baaea2020',
'/base/www/lib/ionic/js/angular-ui/angular-ui-router.min.js': '0973f9c46d3be867276e941e2e6af9c662a92333',
'/base/www/js/controllers/game.js': '4b67bf783d76ae9f67ac5d569e78319be4a85321',
'/base/www/js/controllers/setup.js': 'b466bd52a326d35700517383a6ceb5b25b3ab44c',
'/base/www/js/directives.js': 'fbc832e4aa0c55a597a57cd4474aa9435bc17bf1',
'/base/www/js/services.js': 'df5339fa9aa37d92974fe59c392b8fb53a71497d',
'/base/test/specs/helloSpec.js': 'b8f2b498922be08d7ff36bafd094132b9c75d357',
'/base/test/test-app.js': '432128c0bcd0257679fb9f4f31ff75a1060f64db'
};
</script>
<!-- Dynamically replaced with <script> tags -->
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/requirejs/require.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-requirejs/lib/adapter.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/boot.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js"></script>
<script type="text/javascript" src="/base/test/test-app.js"></script>
<script type="text/javascript">
window.__karma__.loaded();
</script>
</body>
</html>
I'm assuming that the list of served documents is everything pulled in by requirejs and the script tags are what's added by karma.
The console output is:
Skipped 0 tests
So it looks like it just doesn't see the test definitions. But when I step through the debug and set a breakpoint at the requirejs.config I can see that the "tests" array contains my test file:
/base/test/specs/helloSpec.js
I can also see it's been loaded by looking at Chrome's Network panel. Which lists:
debug.html
boot.js
jasmine.js
adapter.js
require.js
test-app.js
adapter.js
helloSpec.js
Earlier I had been getting the error:
Mismatched anonymous define() module: ...
So I had changed the define function call from
define(['angular'], function(angular) {
....
});
to
define('helloTests', ['angular'], function(angular) {
....
});
That got rid of the error details, but didn't fix anything. It turns out it also wasn't helping since I could no longer see the error message change. After posting I tried removing it on a whim since I couldn't see it being used anywhere else. Then the error message changed to:
Chrome 43.0.2357 (Windows 8.1 0.0.0) ERROR: 'There is no timestamp for www/lib/ionic/js/angular.js!'
WARN [web-server]: 404: /www/lib/ionic/js/angular.js
Chrome 43.0.2357 (Windows 8.1 0.0.0) ERROR
Uncaught Error: Script error for: angular
http://requirejs.org/docs/errors.html#scripterror
at c:/Users/Mark/AppData/Roaming/npm/node_modules/requirejs/require.js:141
Since I was using the min files (easier to filter out the debug versions by including "*.min.js" in the file patterns) I changed my requirejs.config paths and appended ".min" like this:
requirejs.config({
baseUrl: 'www',
paths: {
'angular': 'lib/ionic/js/angular/angular.min'
,'angular-ui': 'lib/ionic/js/angular-ui/angular-ui.min'
},
...
})
At this point the error message only changed to say there was no timestamp for angular.min.js. At this point I noticed the path was relative and started with "base/wwww". That's when I finally noticed something I'd missed in the docs. So I changed the "baseUrl" parameter in my requirejs.config to add "base/":
requirejs.config({
baseUrl: 'base/www',
...
})
Now the error is gone and the test is running:
Chrome 43.0.2357 (Windows 8.1 0.0.0): Executed 1 of 1 SUCCESS (0.015 secs / 0 secs)
EDIT: One more thing, the test started running but I didn't notice I was still getting a "There is no timestamp" message. Changing my requirejs.config baseUrl parameter from relative to absolute (added "/" to the beginning) fixed it.
requirejs.config({
baseUrl: '/base/www',
...
})
Now I'm up and running.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With