Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get angular constants in Karma

Given the app startup:

angular.module("starter", [ "ionic" ])
    .constant("DEBUG", true)
    .run(function() {
        /* ... */
    });

how would I test the value of DEBUG?

When trying with:

describe("app", function() {

    beforeEach(function() {
        module("starter");
    });

    describe("constants", function() {
        describe("DEBUG", inject(function(DEBUG) {
            it("should be a boolean", function() {
                expect(typeof DEBUG).toBe("boolean");
            });
        }));
    });
});

I just get

TypeError: 'null' is not an object (evaluating 'currentSpec.$modules')
    at workFn (/%%%/www/lib/angular-mocks/angular-mocks.js:2230)
    at /%%%/www/js/app_test.js:14
    at /%%%/www/js/app_test.js:15
    at /%%%/www/js/app_test.js:16
like image 387
ReactiveRaven Avatar asked Feb 12 '23 15:02

ReactiveRaven


1 Answers

Make sure it is being instantiated in the right place. In this case, the beforeEach was not being run to load the module, because DEBUG was being inject()ed in the describe block, not the it block. The following works properly:

describe("app", function() {

    var DEBUG;

    beforeEach(function() {
        module("starter");
    });

    describe("constants", function() {
        describe("DEBUG", function() {
            it("should be a boolean", inject(function(DEBUG) {
                expect(typeof DEBUG).toBe("boolean");
            }));
        });
    });
});
like image 114
ReactiveRaven Avatar answered Feb 16 '23 10:02

ReactiveRaven