I have this code to unit test:
collection.find({
result: { $exists: false }
}, {
timeout: false,
maxTimeMS: 1800000,
})
.addCursorFlag('noCursorTimeout', true)
.stream()
.
.
.
How can I stub the
.addCursorFlag('noCursorTimeout', true)
part using sinon? Here is the unit test that worked, before adding in the addCursorFlag
:
collectionStub.find = sinon.stub().returns({
stream: () => new mocks.stream.ReadableMock(false, null, 'mongo error')
});
jobRunner.on('error', (updatedJob) => {
expect(updatedJob).to.deep.equal({
...job,
status: 'error',
error: 'mongo error',
});
done();
});
jobRunner.run();
});
It's kind of tricky but if you observe the method chaining after collection.find
, you have a call to addCursorFlag
which then returns an object from where you call the stream
method. You need to structure your collection stub in a similar way:
collectionStub.find = sinon.stub().returns({
// addCursorFlag is a function that returns an object
addCursorFlag: () => ({
// stream is a method in the returned object
stream: () => new mocks.stream.ReadableMock(false, null, 'mongo error')
)}
});