Skip to content

Instantly share code, notes, and snippets.

@ppg
Last active August 15, 2017 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppg/808697fdb740c284aaafc681fbfd373f to your computer and use it in GitHub Desktop.
Save ppg/808697fdb740c284aaafc681fbfd373f to your computer and use it in GitHub Desktop.
Demonstration of deep matching issues with sinon-chain calledWith
'use strict';
const chai = require('chai');
const expect = chai.expect;
const mongoose = require('mongoose');
const ObjectId = mongoose.Schema.Types.ObjectId;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);
const ChildSchema = mongoose.Schema({
});
const Child = mongoose.model('Child', ChildSchema);
const ParentSchema = mongoose.Schema({
children: [{ type: ObjectId, ref: 'Child'}],
parameters: mongoose.Schema.Types.Mixed,
});
const Parent = mongoose.model('Parent', ParentSchema);
it('should deep match mongoose arrays', function() {
expect((new Parent({ children: [] }).children)).to.deep.equal([]);
});
it('should deep match mongoose arrays on stub', function() {
const stub = sinon.stub();
const actual = (new Parent({ children: [] })).children;
stub(actual);
expect(stub.getCall(0).args[0]).to.deep.equal([]);
});
it('should deep match mongoose arrays on stub using called with with sinon matchers', function() {
const stub = sinon.stub();
const actual = (new Parent({ children: [] })).children;
stub(actual);
sinon.assert.calledWith(stub, sinon.match.array.deepEquals([]));
});
it('should deep match mongoose arrays on stub using calledWith with sinon-chai', function() {
const stub = sinon.stub();
const actual = (new Parent({ children: [] })).children;
stub(actual);
expect(stub).to.have.been.deep.calledWith([]);
});
it('should deep match mongoose object on stub using called with with sinon matchers', function() {
const stub = sinon.stub();
const actual = (new Parent({ parameters: { foo: 'bar', alpha: 42 } })).parameters;
stub(actual);
sinon.assert.calledWith(stub, sinon.match({ foo: 'bar', alpha: 42 }));
});
it('should deep match mongoose object on stub using calledWith with sinon-chai', function() {
const stub = sinon.stub();
const actual = (new Parent({ parameters: { foo: 'bar', alpha: 42 } })).parameters;
stub(actual);
expect(stub).to.have.been.calledWith({ foo: 'bar', alpha: 42 });
});
@ppg
Copy link
Author

ppg commented Aug 8, 2017

$ node --version
v6.11.2
$ npm list --depth=0
(redacted)
├── chai@4.1.1
├── mocha@3.5.0
├── mongoose@4.11.6
├── sinon@2.4.1
└── sinon-chai@2.12.0

$ mocha mongoose-arrays.js 


  ✓ should deep match mongoose arrays
  ✓ should deep match mongoose arrays on stub
  1) should deep match mongoose arrays on stub calledWith

  2 passing (23ms)
  1 failing

  1)  should deep match mongoose arrays on stub calledWith:
     AssertionError: expected stub to have been called with arguments []
[]
      at Context.<anonymous> (mongoose-arrays.js:35:34)

@ppg
Copy link
Author

ppg commented Aug 15, 2017

Update to show correct behavior only using sinon and incorret behavior with sinon-chai:

$ mocha mongoose-arrays.js 


  ✓ should deep match mongoose arrays
  ✓ should deep match mongoose arrays on stub
  ✓ should deep match mongoose arrays on stub using called with with sinon matchers
  1) should deep match mongoose arrays on stub using calledWith with sinon-chai
  ✓ should deep match mongoose object on stub using called with with sinon matchers
  ✓ should deep match mongoose object on stub using calledWith with sinon-chai

  5 passing (41ms)
  1 failing

  1)  should deep match mongoose arrays on stub using calledWith with sinon-chai:
     AssertionError: expected stub to have been called with arguments []
[]
      at Context.<anonymous> (mongoose-arrays.js:43:34)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment