Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfkaye/9129150 to your computer and use it in GitHub Desktop.
Save dfkaye/9129150 to your computer and use it in GitHub Desktop.
answering the global issue in where.js

why where.js is global

  • globals are not evil. globals are what we have, in JavaScript, and they are not going away. Using good judgement, just avoid them when necessary, but use them where they make sense.

  • where.js is a testing library, not a production application library. The danger of colliding with another global named "where" is vanishingly small, especially in tests.

  • Jasmine, Mocha, QUnit ("... and the rest") all work this way in the browser. In order run jasmine on node.js, jasmine-node does the same thing, making jasmine a global. qunitjs works the same way on node.js. The library by J.P. Castro that in part inspired me to work out where.js also declares a global, using.

  • I can require() my code under test into a test file on Node.js, but must I really require the test file be that kind of module rather than a script?

  • Does it makes sense for where.js tests with mocha, jasmine, QUnit, etc., on both node.js and browsers, to depend on "build" steps for tests written for platform-agnostic code, especially interpreted code that should just run? Avoiding that step reduces dependencies in your tests and test subjects.

-- to be continued in comments and elsewhere, I'm sure --

@iammerrick
Copy link

Doesn't it make sense to just use UMD, and if people want to export it globally they can? (Like how chai assertions work)

@iammerrick
Copy link

Explicitly defining a global for the user seem prescriptive when there is no need to be.

@iammerrick
Copy link

Also, I do believe globals are going away marginally soon. With ES6 module scope + commonjs module scope.

@dfkaye
Copy link
Author

dfkaye commented Feb 21, 2014

Keeping it simple, just require('where') and where is available.

@dfkaye
Copy link
Author

dfkaye commented Feb 21, 2014

IMO, as I'm sure you've guessed, is that the module patterns are code bloat. If you have to enforce any, they should be as small as possible and work on multiple environments without requiring builds.

The my.namespace.pattern works on both node.js and browsers, is well-understood but until recently has been accidentally clobberable, like globals.

The synchronous nature of require() in node.js appears to solve that problem for re-use but actually encourages shell script style code (imperative), not modular code (declarative).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment