1
votes

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

2
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
myModule is not returned anywhere in the old JS module. How do I hang it off window? - fatlog
Maybe window.myModule = myModule at the bottom of your module file? - Thomas Upton
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

2 Answers

0
votes

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.

1
votes

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.