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?
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?
Share Improve this question asked May 16, 2017 at 8:11 Jay ShuklaJay Shukla 5,8268 gold badges34 silver badges65 bronze badges 03 Answers
Reset to default 13As you are using Sinon, you can either use the Sinon spy methods and check the results with Chai:
expect(spy.calledWith(5)).to.equal(true);
Or you can use sinon-chai which will let you do:
expect(spy).to.have.been.calledWith(5);
See a JSFiddle of the first example
Maybe you forgot to include the sinon-chai.js?
Here is the working fiddle https://jsfiddle/zjet0432/1/
I only added the sinon-chai.js and change your last line to expect(spy).to.have.been.calledWith(5);
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)