1

I'm trying to build some unit tests for an old JS file/module that is out of my control.

The JS module is built using the following pattern...

var myModule = {
    myMethod:function() {
    }
};

I am then trying to build a DOH test harness to test this. I tried the following...

require([
    "doh/runner",
    "../../myModules/myModule.js"
    ], function(doh) {
        console.log(doh);
        console.log(myModule);
    });

The file seems to be getting picked up fine but I can't reference anything in it. "console.log(myModule);" just returns undefined.

Anyone know how I can correctly include an external non dojo module JS file in a DOH test harness?

Thanks

fatlog
  • 1,182
  • 2
  • 14
  • 28
  • Does `myModule` get returned anywhere in the old JS module? If so, you need to add it to your callback, like @DesertIvy mentioned. If it's not, and it's just assigning values to some global variables, you might need to make sure they're actually global by hanging them off the global object, e.g. `window` and accessing it like `window.myModule`. – Thomas Upton Sep 23 '13 at 21:17
  • myModule is not returned anywhere in the old JS module. How do I hang it off window? – fatlog Sep 24 '13 at 08:42
  • Maybe `window.myModule = myModule` at the bottom of your module file? – Thomas Upton Sep 24 '13 at 15:39
  • As I pointed out in another comment: it should be "myModules/myModule" in the `require` dependency list. Don't include the file extension. – Thomas Upton Sep 25 '13 at 20:13

2 Answers2

1

Other than you shouldn’t be using DOH because it is deprecated (use Intern), there is no reason that you shouldn’t see myModule there. You are using a script address and not a module ID, which isn’t right, and you are using a relative path with a require call, which is also not right, but if either of these things were preventing the loader from finding and loading the script you are trying to load it should be throwing an error that you could see in the console. The only other possibility is you have somehow managed to build a built layer into this myModule script, in which case the entire script ends up wrapped in a closure and so using var foo will no longer define a global variable foo.

C Snover
  • 17,908
  • 5
  • 29
  • 39
  • Would love to use Intern but the choice of technology was made long ago and is outside of my control. – fatlog Sep 24 '13 at 08:41
  • Based on the the information that the OP has provided, it *is*, in fact, the only other possibility. There is no "return" of a module when using a standard JS object literal in a standard browser JS file. I am also not sure how pointing out two potential errors, as well as what the user should expect to see if the loader is failing, is unhelpful and non-constructive, so I would really appreciate it if you would not downvote and then leave nasty comments. Thanks! – C Snover Sep 24 '13 at 23:48
  • I believe this answer is at least on the right track insofar as pointing out the fact that the myModule dependency isn't really being referenced the proper way. As to what exactly should be the expected outcome is currently for anyone to guess, as we have no information as to the OP's filesystem structure or the contents of that module. It's hard to get the OP closer without actually having enough information to get closer. – Ken Franqueiro Sep 24 '13 at 23:53
  • Having accidentally built a layer is not the only other possible cause to this problem. And my comment (which I can't find any more?) was in no way intended to be nasty. Using a relative path in `require` isn't the issue. Telling someone to use a different library instead of answering the question isn't a good answer. I've un-downvoted based on the fact that you pointed out that there's a script address in the `require` statement, which could be part of the problem. – Thomas Upton Sep 25 '13 at 20:11
0

You need to declare myModule in the function callback to your require statement:

require([
    "doh/runner",
    "../../myModules/myModule"
], function(doh, myModule) { // <-- include myModule
    console.log(doh);
    console.log(myModule);
});

Just be sure that myModule.js returns your module.

Bucket
  • 7,415
  • 9
  • 35
  • 45
  • If `myModule` is being returned from the module file, there's no need to create a widget. Plenty of dojo modules aren't widgets, and plenty follow the pattern of @fatlog's module, where a static object is returned. – Thomas Upton Sep 23 '13 at 21:14
  • Can you update your original post to reflect the latest code you've tried? – Ken Franqueiro Sep 24 '13 at 23:56
  • For this to work, `myModule` must return a value. [See the documentation for the deprecated `dojo.provide()` code](http://dojotoolkit.org/reference-guide/1.9/dojo/provide.html) – Bucket Sep 25 '13 at 03:39
  • It should also be "myModules/myModule" in the require statement. You shouldn't put the file extension in the `require` dependency list. – Thomas Upton Sep 25 '13 at 20:12