Question

I'm doing TDD for a javascript app with Jasmine. For some reason I can't reference functions I declared in the source portion of the SpecRunner.html file if it's nested inside two describe functions.

Here's the code:

describe("DidiButton", function(){
    var didiButtonDiv = document.createElement("div");
    didiButtonDiv.id = "DidiButtonDiv";

    it("should throw an UndefinedElementError if it isn't given a DOM", function(){
        try{
            var didiButton = createDidiButton();
        }
        catch(e){
            expect(e.name).toEqual("UndefinedElementError");
        }
    });

    it("should be able to associate itself with a DOM", function(){
        var didiButton = createDidiButton(didiButtonDiv);
        expect(didiButtonDiv).toEqual(didiButton.element);
    });

    describe("Link Functions", function(){
        var didiButton = createDidiButton(didiButtonDiv);
        var link = "http://www.google.com";

        it("should be able to set a link", function(){
            didiButton.setLink(link);
        });

        it("should be able to get the link set to it", function(){
            expect(link).toEqual(didiButton.getLink()); 
        });
    });

});

This is the error I'm getting:

ReferenceError: createDidiButton is not defined in file:///Users/.../spec/DidiButtonSpec.js (line 20)

If I remove the describe("Link Functions", function(){...}); block, the tests pass. I did a simple test with nested functions to see if I was going crazy; I wrote the script below and called funcHi() . It worked, like I thought it should, and each of the alerts were able to find message() .

function message(){
    return "Hi";
}

function funcHi(){
    alert(message());
    function func1(){
        alert(message() + 1);
        function func2(){
            alert(message() + 2);
            function func3(){
                alert(message() + 3);
            }
            func3();
        }
        func2();
    }
    func1();
}

Why isn't jasmine able to find my function?

Was it helpful?

Solution

I figured it out: Jasmine doesn't understand references to files included in the source scripts of the SpecRunner.html file unless they're in a spec, or it(...) function. The inner describe block works if I change it to the following:

describe("Link Functions", function(){
    var didiButton;
    var link = "http://www.google.com";

    it("should be able to set a link", function(){
        didiButton = createDidiButton(didiButtonDiv);
        didiButton.setLink(link);
    });

    it("should be able to get the link set to it", function(){
        expect(link).toEqual(didiButton.getLink()); 
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top