to.have.been.calledWith is not a function error in chai#3.5.0

Viewed 7875

I've updated the version of chai in my project and after updating it to 3.5.0, some tests are failing. I see that I'm not able to test arguments of the function for which I did spy.

I created a fiddle to reproduce my issue with sample method here - JSFiddle

describe('Mocha + Chai JsFiddle', function() {

  it('should test arg', function() {
    var spy = sinon.spy(test, 'testFun');

    test.testFun(5);

    expect(spy).to.have.been.called.with(5);
  });
});

Can anyone suggest how can we test argument in newer version chai.js?

3 Answers

If you are using TypeScript with Sinon and Chai and want to use nice calledWith assertions in Chai, use sinon-chai imports like this:

import chai, {expect} from 'chai'
import sinon from 'ts-sinon'
import sinonChai from 'sinon-chai' // <--- NOTE

then configure chai with:

chai.use(sinonChai)

and then use assertions like:

let send = sinon.spy()
...
expect(send).to.have.been.calledWithExactly('Greetings!')

Runnable example here (disclaimer: my own repo)

Related