0

In stuff.js:

function init() {
    return "works";
}

(function(ParentNamespace) {
    ParentNamespace.MySubNamespace = {};
})(window.MyNamespace || (window.MyNamespace = {}));

In my test JS file:

/// <reference path="../../../project1/Shared/sub1/Javascript/stuff.js" />
test("foo test", function () {
    deepEqual(init(), "works", "couldn't access source JS file");
    ok(window, "no window context");
    var ns = window.MyNamespace;
    ok(ns in window, "namespace is bad");
    var ns2 = window.MyNamespace.MySubNamespace;
    ok(ns2 in window, "subnamespace is bad");
});

I get 'undefined' is not an object (evaluating 'window.MyNamespace.MySubNamespace') when running the above test using Chutzpah Test Adapter. That is to say, an exception is thrown on the var ns2 line, and I never get to the last ok() assertion. What am I doing wrong? Shouldn't qUnit/Chutzpah have run the code in stuff.js before trying to run the test?

Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129
  • If I rewrite the function to be callable directly as foo, and then I call it with `foo(window.MyNamespace || (window.MyNamespace = {}));` before doing my assertions, I get "namespace is bad" and "subnamespace is bad". Doesn't seem right. – Patrick Szalapski Jan 29 '13 at 18:50

1 Answers1

1

I have changed the test. Following test works...

/// <reference path="../../../project1/Shared/sub1/Javascript/stuff.js" />
test("foo test", function () {
deepEqual(init(), "works", "couldn't access source JS file");
ok(window, "no window context");    
ok('MyNamespace' in window, "namespace is bad");    
ok('MySubNamespace' in window.MyNamespace, "subnamespace is bad");
});
  • OK, but why doesn't it work without the single quotes? That is, why doesn't `ok(MyNamespace in window, "namespace is bad");` pass? – Patrick Szalapski Feb 01 '13 at 16:24
  • This is just a JavaScript syntax. Your question can be best replied by the authors of JavaScript :) – Utkarsh Patkar Feb 07 '13 at 13:44
  • I would think the above is valid syntax. `MyNamespace` is an object that should exist; `window` is an object that exists, and the `in` operator tests to see if the right side object is a member of the left side object. What am I missing? – Patrick Szalapski Feb 07 '13 at 19:05
  • 1
    As mentioned earlier this is the syntax. The syntax is "propNameOrNumber in objectName" where propNameOrNumber is a string or numeric expression representing a property name or array index, and objectName is the name of an object. Please refer to https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Expressions_and_Operators – Utkarsh Patkar Feb 08 '13 at 03:19
  • Got it--that's what I was missing. Too bad "in" isn't a little more flexible. – Patrick Szalapski Feb 08 '13 at 14:07