When it comes to Firestore cloud function TypeScript unit tests, my focus is on mocking doc().id
, while leaving doc('path')
untouched. Can anyone suggest how I can achieve this?
admin.firestore().collection('posts').doc().id // Mocking only this line
admin.firestore().collection('posts').doc('1')
I attempted the following approach using sinon, but encountered an infinite loop issue at sinon/proxy-invoke.js:50:47
:
const collection = admin.firestore().collection('posts');
sinon.stub(collection, 'doc').callsFake(path =>
path === undefined ? mock : collection.doc(path)
);
sinon.stub(admin.firestore(), 'collection')
.callThrough()
.withArgs('posts')
.returns(collection)
Another attempt involved the following code, however, the doc(documentPath: string)
method ended up being stubbed out as well:
sinon.stub(collection, 'doc')
//@ts-ignore
.withArgs()
.returns(mock)
If there's a workaround available in other mock libraries, I'm open to exploring those options.