I'm trying to use sinon.js in testing of a backbone application. But unfortunately I cannot use spy method due to error:
TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
Here is the steps to reproduce the error:
- Create an empty project with backbone yeoman generator
- Install sinon:
cd test && bower install sinon
- Include in test/index.html
<script src="bower_components/sinon/lib/sinon.js"></script>
Create spy in test/spec/test.js:
describe('Give it some context', function () { describe('maybe a bit more context here', function () { it('should run here few assertions', function () { var spy = sinon.spy(); spy.should.be.ok; }); }); });
Run the test with grunt:
grunt test
The test will fail with a described error.
Could anyone help to find out what is wrong?
I'll just leave here the list of files that
sinon
conveniently forgets to load if it is loaded as a<script>
or withrequire.js
(as AMD module) - basically anything else than innode.js
:Feel free to skip any of those but expect sinon to fail in curious ways.
Unlike the other answers, I didn't install simon manually by including each individual source file. Instead I followed the advice of How To Install Sinon.JS In The Browser With Bower.
then
And
Worked for me.
I encountered the same problem with sinon 1.17.2 and Chrome 47.0. After trying the above solutions and variations of those, I ended up using the nuclear option and switching to Jasmine.
For my test suite, it only took about 15 minutes of some global find-and-replace to convert my chai 'expects' into Jasmine ones and some differences around mocha before syntax; Jasmine flagged the unexpected syntax clearly. Jasmine spy objects were a fine substitute for sinon.
It turned out that such functionality as spies, stubs, etc should be added manually by including scripts from
lib/sinon
folder. This fact is mentioned in Installation section. And due to the code of the core sinon.js file only in Node.js environment it is done automatically.