Using sinon.js with bower (why is sinon.foo undefined?)

Unfortunately if you're using sinon.js via bower for testing in the browser. If you just bower install sinon you'll end up with all the components from the repo, not a built file which is what the release file is.

If you want say bower.stub() to work you'll need to include the necessary lib/sinon/stub.js too otherwise you'll get an error:

sinon.stub is undefined

There's an easy way to solve this. If you point bower at the release file something like this:

"devDependencies": {
  "sinon": "http://sinonjs.org/releases/sinon-1.9.0.js"
}

Then add to exportsOverride in your bower.json if you're using grunt-bower-task like me e.g:

"exportsOverride": {
  "sinon": {
    "js": "index.js"
  }
}

I'd recommend using something like grunt-bower-task because it allows you to be more selective as to what you serve (and/or commit to the tree). It just moves files out of bower_components to a directory of your choice. This is mainly necessary because a lot of bower packages don't necessarily use the ignore array to remove things that don't need to be in the package.

I also commit the resulting relocated source (not bower_components) as it makes deployments more reliable. Plus you have a history of updates this way too. (Yes committing libs does feel a bit icky at first).

Hattip to @zenocon who pointed out how to reference sinon releases in bower.json here.

Show Comments