0

My logout function sends a request to the logout api and clears all cookies effectively ending the session. It works really well but when I run the function in test, I get:

ReferenceError: $ is not defined

I know this comes from when, in the logout function, I try to clear all of my cookies by running:

for(var cookie in $.cookie()) {
    $.removeCookie(cookie);
}

Thanks for taking a look! The rest of the code is below:

Controller

simulatorApp.controller('homeController', function homeController($scope, $http, $cookies, localAPI) {
$scope.logout = function() {
    var logoutRequest = {
        method: 'POST',
        url: localAPI.url + 'auth/logout/',
        headers: requestHeaders
    };
    $http(logoutRequest);
    for(var cookie in $.cookie()) {
        $.removeCookie(cookie);
    }
};

Test

describe('homeController', function() {
    beforeEach(module('simulatorApp'));

    var controller;
    beforeEach(inject(function(_$controller_) {
        controller = _$controller_;
    }));

    var $scope = {};
    beforeEach(inject(function($controller, $httpBackend) {
        $httpBackend.whenPOST("http://0.0.0.0:8000/api/logout/").respond("Good for you");
        controller('homeController', {
            $scope: $scope
        });
        $scope.logout();
        $httpBackend.flush()
    }));
});
Peter Graham
  • 2,467
  • 2
  • 24
  • 29

1 Answers1

1

$ is JQuery's keyword, so the problem here your karma runtime have no idea what $ is.

Adding your jquery source in karma.conf.js's file section would solve this issue.

Rebornix
  • 5,272
  • 1
  • 23
  • 26