使用Chutzpah,Typescript和Jasmine运行测试时模块未定义

时间:2018-06-04 07:57:15

标签: typescript jasmine visual-studio-2017 chutzpah

我一直在努力让Chutzpah与Typescript和Jasmine合作约一个小时。在将我的代码移动到Typescript之前,测试都适用于.js个文件。很可能是因为他们不需要转换

预期行为:

以下测试应该运行并通过。

实际行为:

问题是我要么尝试使用未导入的模块(as per this example),因为模块未导入而导致模块爆炸。或者我尝试导入它,在这种情况下,已编译的代码中未定义requireexports

我认为最简单的解决方法是实现require.js或类似的东西,但我所遵循的示例中没有一个 - 包括存储库的示例,表明{{1需要使Chutzpah工作。

重现问题的最小代码:

libCodeTypeScriptTest.ts

require.js

libCode.ts:

/// <reference path="../Code/libCode.ts" />
/// <reference path="../Scripts/typings/jasmine/jasmine.d.ts"/> 
/// <chutzpah_reference path="../Code/libCode.js" />

describe("Topup Test - ", function() {
    it("Returns an address object", function() {
        var testObject = [
            {
                name: "Address.Street",
                value: "Hello"
            },
            {
                name: "Address.Zip",
                value: "world"
            }
        ];
        var expectedObject = {
            Address: {
                Street: "Hello",
                Zip: "world"
            },
            TopUpItems: [{}]
        };

        var actualObject = TopUp.serializeTopUpItem(testObject);
        expect(expectedObject).toEqual(actualObject);
    });
});

chutzpah.json:

module TopUp {

    export function serializeTopUpItem(candidateData: any[]): { Address: any, TopUpItems: any[] } {

        if (!candidateData.length) {
            return null;
        }

        // Initialize local variables
        var data = {
            Address: {},
            TopUpItems: []
        };
        var topupItems = [];
        var topupItem = {};
        var address = {};
        var counter = 0;
        var name = '';

        // Loop over the entries of candidateData
        candidateData.forEach((element, index) => {

            if (element.name.indexOf("Address") > -1) {
                name = element.name.substring(element.name.lastIndexOf('.') + 1);
                address[name] = element.value;
            } else if (element.name.indexOf("DOB") > -1) {
                // -> Do nothing
            } else if (element.name.indexOf("TopUp") > -1) {
                if (element.name.indexOf(counter) === -1) {
                    // The object does not contain counter, meaning it must be a new item.
                    topupItems.push(topupItem);
                    counter++;
                    topupItem = {};
                } else {
                    name = element.name.substring(element.name.lastIndexOf('.') + 1);
                    topupItem[name] = element.value;
                }
            }
        });

        if (!topupItems.length) {
            topupItems.push(topupItem);
        }

        data.Address = address;
        data.TopUpItems = topupItems;
        return data;
        };
}

0 个答案:

没有答案