I have a javascript autocomplete plugin that uses the following classes (written in coffeescript): Query, Suggestion, SuggestionCollection, and Autocomplete. Each of these classes has an associated spec written in Jasmine.
The plugin is defined within a module, e.g.:
(function(){
// plugin...
}).call(this);
This prevents the classes from polluting the global namespace, but also hides them from any tests (specs with jasmine, or unit-tests with something like q-unit).
What is the best way to expose javascript classes or objects for testing without polluting the global namespace?
I'll answer with the solution I came up with, but I'm hoping that there is something more standard.
Update: My Attempted Solution
Because I'm a newb with < 100 xp, I can't answer my own question for 8 hours. Instead of waiting I'll just add what I did here.
In order to spec these classes, I invented a global object called _test
that I exposed all the classes within for testing. For example, in coffeescript:
class Query
// ...
class Suggestion
// ...
// Use the classes
// Expose the classes for testing
window._test = {
Query: Query
Suggestion: Suggestion
}
Inside my specs, then, I can reveal the class I'm testing:
Query = window._test.Query
describe 'Query', ->
// ...
This has the advantage that only the _test
object is polluted, and it is unlikely it will collide with another definition of this object. It is still not as clean as I would like it, though. I'm hoping someone will provide a better solution.