How to 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

What I get ... Mine karma.conf.jshas:

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 this should cover the lang file, but it is not. As for api calls, they are not actually served, but I have a check in my init application to try to check if the user is authenticated. In my test file, I do:

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

in mine beforeEach, but this is not a fix. Ideas?

+4
source share
1 answer

If you want to skip the application configuration section in unit test, you need to split the application into several modules. Thus, you can test individual modules without starting the configuration section of another module.

, unit test , , - .

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*/);

unit test, , :

beforeEach(module('myAppDirectives'));
+1

Source: https://habr.com/ru/post/1527572/


All Articles