In Javascript polluting the global namespace is generally regarded as a bad thing. This is why Coffeescript wraps all of your Javascript in a (function() {}).call(this);
wrapper.
However, I've begun writing QUnit tests for my Coffeescript code, and QUnit complains that it can't find my functions.
1. Died on test #1: getGoodNamePart is not defined
getGoodNamePart is not defined at Object.<anonymous> (file:///Users/kevin/Documents/docs/code/chrome/tests.js:2:10) at Object.run
I'd like to test the variables without polluting the global namespace. What's a good way to do this?
Here's the generated Javascript I want to test:
(function() {
getGoodNamePart = function(str) {
if (str.charAt(0) === '"') {
str.replace(/" <[^>]+>$"/g, "");
str.replace(/"/g, "");
return str;
} else if (str.charAt(0) === '<') {
str.replace(/<|>/g, "");
return str;
} else {
return str;
}
};
}).call(this);
and my test.js file is:
test('getGoodNamePart()', function() {
equals(getGoodNamePart("\"Kev Burke\" <[email protected]>"), "Kev Burke", "\"name\" <email> works");
equals(getGoodNamePart("", "", "empty string works"));
equals(getGoodNamePart("[email protected]", "[email protected]", "raw email works"));
return equals(getGoodNamePart("<[email protected]>", "[email protected]", "email inside carets -> carets get stripped"));
});
Thanks, Kevin