You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I've started to do some testing with firebase-mock with facebook/jest but I'm starting to face a strange problem. When running my tests, sometimes they'll succeed and sometimes they won't if I decide to run some setup inside beforeEach and afterEach:
Without changes, running the same tests:
I'm not sure if this is some sort of race condition, given that jest parallelizes testing to reduce time. To avoid collisions or unpredictable behavior during my tests (precisely because of that) I generate random IDs before creating new documents for my setup and then use those IDs for testing my REST API. Using --runInBand flag in jest didn't change the outcome though.
I would assume that multiple calls to .set, .delete, .create, and all of those won't affect firebase-mock internals (apart from the data)... but apparently, somehow it does? As you can see in the screenshots, on subsequent tests, for some reason, .set (and further down .delete) does not exist because .doc() is not working as expected for subcollections?
See the code
letEVENT_ID: string;letGIVEAWAY_ID: string;letPARTICIPANT_ID: string;beforeEach(async()=>{EVENT_ID=cuid();GIVEAWAY_ID=cuid();PARTICIPANT_ID=getGiveawayParticipantId(GIVEAWAY_ID,'user1');awaitfirestore.collection('events').doc(EVENT_ID).set({address: 'Address',coordinates: newf.GeoPoint(0,0),title: `${EVENT_ID} Event`,subtitle: `Subtitle`,images: {compact: 'https://via.placeholder.com/350x150',cover: 'https://via.placeholder.com/350x350'},startDate: f.Timestamp.fromDate(moment('20200101').toDate()),});awaitfirestore.collection('events').doc(EVENT_ID).collection('giveawayParticipants').doc(PARTICIPANT_ID)// this is where it breaks for some calls.set({userId: 'user1',giveawayId: GIVEAWAY_ID,entryDate: f.Timestamp.fromDate(moment('20200101T01').toDate())});});afterEach(async()=>{awaitfirestore.collection('events').doc(EVENT_ID).delete();awaitfirestore.collection('events').doc(EVENT_ID).collection('giveawayParticipants').doc(PARTICIPANT_ID)// this is where it breaks for some calls.delete();});
The same strategy works for other tests properly, it's precisely with subcollections where I'm faced with this "sometimes not working" behavior. The issue even occurs within my REST API code (which is mocked with firestore-mock), so it's definitely a thing inside firestore-mock.
See the code
constbatch=this.firebaseService.firestore.batch();batch.create(this.firebaseService.firestore.collection('events').doc(this.eventId).collection('giveawayParticipants').doc(participantId),participantData);// ... some other stuff in the batchtry{awaitbatch.commit();}catch(err){// This generates the screenshot's responsethrownewErrors.UnprocessableEntityError(err.message);}
This is what I was expecting, but this was literally a struck of luck, after running jest around 3 to 5 times:
I'm currently using firebase-mock from master (npm i -s github:dmurvihill/firebase-mock#master).
The text was updated successfully, but these errors were encountered:
lesmo
changed the title
Erratic behavior on interaction subcollections
Erratic behavior on interacting with subcollections
Mar 19, 2020
Hi,
Thanks for keeping up with
firebase-mock
!So I've started to do some testing with
firebase-mock
withfacebook/jest
but I'm starting to face a strange problem. When running my tests, sometimes they'll succeed and sometimes they won't if I decide to run some setup insidebeforeEach
andafterEach
:Without changes, running the same tests:
I'm not sure if this is some sort of race condition, given that
jest
parallelizes testing to reduce time. To avoid collisions or unpredictable behavior during my tests (precisely because of that) I generate random IDs before creating new documents for my setup and then use those IDs for testing my REST API. Using--runInBand
flag injest
didn't change the outcome though.I would assume that multiple calls to
.set
,.delete
,.create
, and all of those won't affectfirebase-mock
internals (apart from the data)... but apparently, somehow it does? As you can see in the screenshots, on subsequent tests, for some reason,.set
(and further down.delete
) does not exist because.doc()
is not working as expected for subcollections?See the code
The same strategy works for other tests properly, it's precisely with subcollections where I'm faced with this "sometimes not working" behavior. The issue even occurs within my REST API code (which is mocked with
firestore-mock
), so it's definitely a thing insidefirestore-mock
.See the code
This is what I was expecting, but this was literally a struck of luck, after running
jest
around 3 to 5 times:I'm currently using
firebase-mock
frommaster
(npm i -s github:dmurvihill/firebase-mock#master
).The text was updated successfully, but these errors were encountered: