mercredi 31 août 2016

Typescript issue with accessing global namespace functions in jasmine test

So this is a strange one that I have looked all over the internet for and have had no luck. I am adding in tests to a clients website with Jasmine, Chutzpah and Visual Studio 2015, so I am modifying existing code. So this is the issue, I have a client side written entirely in typescript and this makes calls to an API on the server. When I create the test files I cannot access functions in a global namespace. So the file I am trying to test looks like this, we will call this file foo.ts

foo.ts

    namespace foo { 

        export function isANumber(n) {
            !isNaN(parseFloat(n)) && isFInite(n);     
        }  

    }

So this is one function I am trying to test inside foo.test.ts. This is my foo.test.ts file. I keep it in the same folder as the foo.ts just so I could see if it was a file placement issue.

foo.test.ts

/// <reference path="foo.ts" />

    describe("Foo Tests",
    function() {

        describe("Is A Number",
            function () {

                var isANumberTest;
                beforeEach(function () {      
                    isANumberTest= foo.isANumber;
                });
                afterEach(function() {
                    isANumberTest= 'undefined';
                });
                it("should return true if a number",
                    function () {
                        expect(isANumberTest(1)).toBe(true);
                    });

                it("should return false if not a number",
                    function() {
                        expect(isANumberTest('n')).toBe(false);
                    });
            });
     });

So the error I get when the tests run is ReferenceError: 'foo' is undefined and TypeError: Object expected. I can see in the debug that foo is undefined. When I tried wrapping the tests in the global namespace I just get the TypeError: Object expected. I can see that foo is now there but it doesn't have isANumber function in it. I am coming from a C# background into javascript. I am assuming that isANumber hasn't been created yet and that is why the tests can't find it. I am new to typescript, javascript and writing test in them. I am not sure if this is a compile issue and something is missing from a config file or if I am even writing the tests right. Any help would be greatly appreciated!! Let me know if you need more info.

Aucun commentaire:

Enregistrer un commentaire