These are chat archives for Automattic/mongoose

13th
Sep 2017
Yusuf Akano Badmus
@TunsBad
Sep 13 2017 11:54
write code in different files and export with module.exports. To make route available in your server.js file, use require('filename').
greetings of the day everyone, please I am having issues understanding how req.user property works .....
does req.user depends on sessions storage ??
TheCARGOS
@TheCARGOS
Sep 13 2017 17:21
:D
Scott Gilroy
@sgilroy
Sep 13 2017 19:30
I just upgraded from mongoose@4.3.7 to mongoose@4.11.11 and now some of our tests are failing that used to pass. How can I resolve the test failures? The following passes with 4.3.7 but fails with 4.11.11. I'm running with node v6.9.2, sinon@1.9.1, mocha@2.5.3
const ObjectId = require('mongoose').Types.ObjectId;

it('calling a method with an equivalent ObjectId value should pass', function() {
    const stub = this.sinon.stub();
    const id1 = new ObjectId('000000000000000000000000');
    const id2 = new ObjectId('000000000000000000000000');
    stub(id1);
    console.log('called with', stub.firstCall.args[0]);
    expect(stub).to.have.been.calledWith(id2);
});
Output with mongoose@4.11.11:
called with 000000000000000000000000

AssertionError: expected stub to have been called with arguments 000000000000000000000000%D
Scott Gilroy
@sgilroy
Sep 13 2017 20:26
This is a workaround using a custom sinon matcher that seems to work. I'd still like to understand what about mongoose/mongo/bson changed that broke this, and ideally have a solution that doesn't require changing all of my failing tests, so further ideas are welcome.
const sinon = require('sinon');
const ObjectId = require('mongoose').Types.ObjectId;

function equivalentId(id) {
    return sinon.match(value => id.equals(value), `ObjectId("${id.toString()}")`);
}

it('calling a method with an equivalent ObjectId value should pass', function() {
    const stub = this.sinon.stub();
    const id1 = new ObjectId('000000000000000000000000');
    const id2 = new ObjectId('000000000000000000000000');
    stub(id1);
    console.log('called with', stub.firstCall.args[0]);
    expect(stub).to.have.been.calledWith(equivalentId(id2));
});