Javascript polluting the global namespace is generally considered bad. This is why Coffeescript wraps all your Javascript in a wrapper (function() {}).call(this); .
However, I started writing QUnit tests for my Coffeescript code, and QUnit complains that it cannot find my functions.
1. Died on test #1: getGoodNamePart is not defined getGoodNamePart is not defined at Object.<anonymous> (file:
I would like to test variables without polluting the global namespace. What a good way to do this?
Here is the generated Javascript that I want to check:
(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:
test('getGoodNamePart()', function() { equals(getGoodNamePart("\"Kev Burke\" < kev@inburke.com >"), "Kev Burke", "\"name\" <email> works"); equals(getGoodNamePart("", "", "empty string works")); equals(getGoodNamePart(" kev@inburke.com ", " kev@inburke.com ", "raw email works")); return equals(getGoodNamePart("< kev@inburke.com >", " kev@inburke.com ", "email inside carets -> carets get stripped")); });
Thanks Kevin
source share