TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')

Sergey picture Sergey · Dec 13, 2013 · Viewed 10.6k times · Source

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:

  1. Create an empty project with backbone yeoman generator
  2. Install sinon: cd test && bower install sinon
  3. Include in test/index.html <script src="bower_components/sinon/lib/sinon.js"></script>
  4. 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;
            });
        });
    });
    
  5. Run the test with grunt: grunt test

  6. The test will fail with a described error.

Could anyone help to find out what is wrong?

Answer

Kamil Szot picture Kamil Szot · May 8, 2014

I'll just leave here the list of files that sinon conveniently forgets to load if it is loaded as a <script> or with require.js (as AMD module) - basically anything else than in node.js:

"sinon/lib/sinon.js",
"sinon/lib/sinon/spy.js",
"sinon/lib/sinon/call.js",
"sinon/lib/sinon/behavior.js",
"sinon/lib/sinon/stub.js",
"sinon/lib/sinon/mock.js",
"sinon/lib/sinon/collection.js",
"sinon/lib/sinon/assert.js",
"sinon/lib/sinon/sandbox.js",
"sinon/lib/sinon/test.js",
"sinon/lib/sinon/test_case.js",
"sinon/lib/sinon/match.js"

Feel free to skip any of those but expect sinon to fail in curious ways.