Chai + Sinon error: method is already wrapped

486 views Asked by At

I am getting error:

  • TypeError: Attempted to wrap ajax which is already wrapped
  • Error: Stack Trace for original chai.should();

    describe("Test API", function() {
    
      it('executes a GET request for ISPs', function() {
        var mySpy = sinon.spy(jQuery, "ajax");
        sinon.stub(jQuery, 'ajax');
        mySpy.should.have.been.calledWithMatch({
        url: 'http://data-api.measurementlab.net/locations/nausdcwashington/clients'
        });
      });
    
      after(function() {
        jQuery.ajax.restore();
     });
    });
    

I came across similar posts and added the after block. Still getting the same error: Sinon.js- Trying to spy on console.log but it is already wrapped

2

There are 2 answers

0
beardo34 On

is that the only place you're stubbing ajax? If not, you need to make sure you restore it after each test before trying to stub it again

0
justadudewhohacks On

Why are you stubbing and spying jQuery.ajax? Remove this line: sinon.stub(jQuery, 'ajax');