0

I'm trying to write Unit Testing for my SPA project. Where we have used the Durandal (Framework), Knockout (Binding) with RequireJs.

I have installed the Chutzpah in Visual Studio 2012.

When i run my Test for the View Model, it throws me below error, even though the knockout js and other js are loaded correctly.

Uncaught ReferenceError: ko is not defined

My Json Config Code:

{
    "Framework": "jasmine",
    "TestHarnessReferenceMode": "AMD",
    "TestHarnessLocationMode": "SettingsFileAdjacent",
    "References" : [
        {"Path" : "../Scripts/require.js" },
        {"Path" : "config.js" }
    ],
    "Tests" : [
      {"Path": "tests"}
    ]
}

My Config Js Code:

require.config({
    paths: {
        'text': '../Scripts/text',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins',
        'jquery': '../Scripts/jquery-2.1.4',
        'knockout': '../Scripts/knockout-3.3.0'
    },

    shim: {

    }
});

My FirstTest.Js Code:

define(['project/modules/Settings/Subscriber/Viewmodels/Channels'],
    function (nChannel) {
        describe("Get Channels", function () {
            it("will check the Get Channels call and result", function () {
                var disp = nChannel.getChannels().then(function () {
                    var actualResult = ko.toJS(nChannel.Channels);
                    expect(actualResult.length).toEqual(3);
                });
            });
        });
    });

ViewModel Code:

define(['plugins/dialog'], function (dialog) {
    var subscriberList = ko.observableArray(); //Getting Error here - while loading the Js for Unit Testing

    var JsQ = $; //Getting JQUERY members here. // Works good.

    //Other Logics goes here

    return {
        subscriberList : subscriberList,
        JsQ : JsQ 
    };
});

The Configuration for the Jquery works perfect, since knockout also same as that. But gives error.

Any Idea's / Suggestion why the error?

Do i need to load the ko (knockout) separately?

Edit 1:

I have tried changing the knockout to ko it gives me the error Uncaught Error: Script error for: knockout.

Edit 2:

The problem i'm facing when i apply this solution, those existing code file needs the massive changes and the file counts are in hundreds. From Init.Js we have loaded the Jquery and Knockout. Like below.

requirejs.config({
    paths: {
        'text': '../Scripts/text',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins'
    }
});

define('jquery', [], function () {
    return jQuery;
});

define('knockout', [], function () {
    return ko;
});

So inside any viewmodel we can get the instance of knockout as ko, without declaring the require js stuff in each veiwmodel for the Knockout (As you suggested).

But when i try the same in Chutzpah declaration this is not working. Not sure why.

Hope you understand the problem.

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80

1 Answers1

2

In both modules you show in your question, you use ko but you do not list knockout in your list of dependencies. This is a sure way to get the error you got. Modify your modules to list knockout in the dependencies, and add a corresponding parameter to the callback you give to define. For instance,

define(['knockout', 'plugins/dialog'], function (ko, dialog) {
Louis
  • 146,715
  • 28
  • 274
  • 320