4
votes

I'm testing my AngularJS app with Jasmine at the moment, and it's passing specs. I tried to run the tests with Karma, and it's failing. It appears that Karma can't see the $scope being defined in the Jasmine spec, but the Jasmine runner sees it fine.

My karma.conf.js:

frameworks = ['jasmine'];
colors = true;
singleRun = true;
files = [
    JASMINE,
    JASMINE_ADAPTER,
    ANGULAR_SCENARIO,
    ANGULAR_SCENARIO_ADAPTER,
    'lib/jquery-1.9.1.min.js',
    'lib/angular.min.js',
    'lib/angular-mocks.js',
    'lib/jquery.scrollTo.js',
    'lib/jquery-ui.min.js',
    'lib/calendar.js',
    'lib/date.js',
    'src/app.js',
    'spec/*.spec.js'
];
browsers = [
    'PhantomJS',
    'Firefox'
];

My test.spec.js:

describe('Application startup', function(){
    beforeEach(module('app'));
    describe('MainCtrl', function(){
        var scope, ctrl;
        beforeEach(inject(function($rootScope, $controller){
            scope = $rootScope.$new();
            ctrl = $controller('MainCtrl', {
                $scope: scope
            });
        }));
        it("should initialize $scope.testLoad to 42", function(){
            expect(scope.testLoad).toBe(42);
        });
});
});

The output I'm getting:

expect undefined toBe 42

As Jasmine is passing this with no problems, it's obviously either Karma or my config file. What's going on?

1
I removed ANGULAR_SCENARIO and ANGULAR_SCENARIO_ADAPTER and it's running fine now. Found the appropriate docs. Made a bad assumption. - andy
This is because ANGULAR_SCENARIO is intended for e2e tests and this require a separate karma.conf file from the one you are using for unit tests - Tomas Romero
Where are the docs located for using all of these together? - vcardillo
@andy If you've answered your own question, please submit the solution as an actual answer. That way, this question won't show up in the 'unanswered' section ;-) - Tyler Eich

1 Answers

1
votes

I removed ANGULAR_SCENARIO and ANGULAR_SCENARIO_ADAPTER and it's running fine now. Found the appropriate docs. Made a bad assumption.

Thanks to @canotto90 and @Tyler Eich for help and clarification.