Here is the code :
(function() {
/*global casper:true */
"use strict";
this.run = function(casper) {
// code test here
};
this.run(casper);
})();
casperjs test myfile.js returns :
TypeError: 'undefined' is not an object (evaluating 'this.run = function(casper) {
}')
# type: uncaughtError
# error: "TypeError: 'undefined' is not an object (evaluating 'this.run = function(casper) {\n \n }')"
If I remove the “use strict”, it simply hangs (expected behavior since this test is incomplete). I guess there’s a rule I’m not understanding around the use strict, but the returned error doesn’t make it obvious.
In strict mode,
thiswill be undefined inside an immediate function like that, and not the global object (which it would be without strict mode). One way to make your code work is to create the global variables explicitly (if that’s what you’re looking for):Or, if you’re not looking for globals, just declare your method locally: