Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking admin.firestore().collection().get()

Related to this [question][1], I am trying to mock firestore when doing unit tests.

The code I am trying to mock looks like this:

const firestore = admin.firestore();
const users = await firestore.collection('users').get();

And my attempt to mock it looks like this:

const firestoreStub = sinon.stub();
Object.defineProperty(admin, 'firestore', {
  get: () => {
    return {
      collection: (path) => Promise.resolve({mocka: 'user'})
    }
  }
});

However it does not work.

I have created a repo (a clone of the official functions repo), to give the entire example here if it helps.

like image 200
DauleDK Avatar asked Apr 12 '18 15:04

DauleDK


1 Answers

With the help from Mark, I got this working:

sinon.stub(admin, 'firestore')
        .get(() => {
            return function() {
                return {
                    collection: (path) => {
                        return {
                            get: () => [{user: 'mock-user-1'}, {user: 'mock-user-2'}]
                        }
                    }
                }
            }
        });

It looks crazy - so if anyone knows a better solution, let me know!

like image 121
2 revs, 2 users 97% Avatar answered Oct 19 '22 07:10

2 revs, 2 users 97%