Shamoon Shamoon - 3 months ago 22
AngularJS Question

How do I fix 404 warnings with Karma for my AngularJS project?

Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.9 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket lJGhX9DakX2lChvLXtju
WARN [web-server]: 404: /lang/en-US.json
WARN [web-server]: 404: /api/v1/user/session
WARN [web-server]: 404: /api/v1/user/auth


That's what I'm getting... My
karma.conf.js
has:

files: [
'public/lib/angular/angular.js',
'public/lib/angular-mocks/angular-mocks.js',
'public/lib/angular-cookies/angular-cookies.js',
'public/lib/angular-resource/angular-resource.js',
'public/lib/angular-route/angular-route.js',
'public/lib/angular-strap/dist/angular-strap.min.js',
'public/lib/angular-animate/angular-animate.min.js',
'public/lib/angular-bootstrap/ui-bootstrap-tpls.js',
'public/lib/angular-bootstrap/ui-bootstrap.js',
'public/lib/angular-translate/angular-translate.min.js',
'public/lib/angular-translate-loader-static-files/angular-translate-loader-static-files.js',
'public/lib/angular-translate-storage-cookie/angular-translate-storage-cookie.js',
'public/js/**/*.js',
'test/karma/unit/**/*.js',
{pattern: 'public/lang/*.*', included: false, served: true}

],


So that should encompass the lang file, but it does not. Regarding the api calls, those aren't actually served, but I have a check in my app init to try to see if a user is authenticated. In my test file, I'm doing:

$httpBackend.when('GET', '/api/v1/user/session').respond({status: 'ok'});


in my
beforeEach
, but that isn't fixing it. Ideas?

rob rob
Answer

If you want to skip your app's config section in a unit test you will need to break your app into multiple modules. That way you can test individual modules without running the config section of another module.

For example if you wanted to unit test your directives without running your app's config section you could do something like this.

var myAppModule = angular.module('myApp', ['myAppDirectives']);

myAppModule.config() {
    //This code won't be run in your unit tests.
    ...
}

var myAppDirectivesModule = angular.module('myAppDirectives', []);

myAppDirectivesModule.directive(/*Define your directives on this module*/);

Then in your unit test where you import the module you can import only the directives module:

beforeEach(module('myAppDirectives'));
Comments