@@ -506,10 +506,11 @@ apiDescribe('Database transactions', persistence => {
506
506
} ) ;
507
507
} ) ;
508
508
509
- it ( 'cannot read after writing' , ( ) => {
510
- return withTestDb ( persistence , db => {
511
- return runTransaction ( db , transaction => {
512
- const docRef = doc ( collection ( db , 'anything' ) ) ;
509
+ it ( 'cannot read after writing and does not commit' , ( ) => {
510
+ return withTestDb ( persistence , async db => {
511
+ const docRef = doc ( collection ( db , '00000-anything' ) ) ;
512
+ await setDoc ( docRef , { foo : 'baz' } ) ;
513
+ await runTransaction ( db , async transaction => {
513
514
transaction . set ( docRef , { foo : 'bar' } ) ;
514
515
return transaction . get ( docRef ) ;
515
516
} )
@@ -523,6 +524,41 @@ apiDescribe('Database transactions', persistence => {
523
524
'Firestore transactions require all reads to be executed'
524
525
) ;
525
526
} ) ;
527
+
528
+ const postSnap = await getDoc ( docRef ) ;
529
+ expect ( postSnap . get ( 'foo' ) ) . to . equal ( 'baz' ) ;
530
+ } ) ;
531
+ } ) ;
532
+
533
+ it ( 'cannot read after writing and does not commit, even if the user transaction does not bubble up the error' , ( ) => {
534
+ return withTestDb ( persistence , async db => {
535
+ const docRef = doc ( collection ( db , '00000-anything' ) ) ;
536
+ await setDoc ( docRef , { foo : 'baz' } ) ;
537
+ await runTransaction ( db , async transaction => {
538
+ transaction . set ( docRef , { foo : 'bar' } ) ;
539
+
540
+ // The following statement `transaction.get(...)` is problematic because
541
+ // it occurs after `transaction.set(...)`. In previous versions of the
542
+ // SDK this un-awaited `transaction.get(...)` failed but the transaction
543
+ // still committed successfully. This regression test ensures that the
544
+ // commit will fail even if the code does not await
545
+ // `transaction.get(...)`.
546
+ // eslint-disable-next-line
547
+ transaction . get ( docRef ) ;
548
+ } )
549
+ . then ( ( ) => {
550
+ expect . fail ( 'transaction should fail' ) ;
551
+ } )
552
+ . catch ( ( err : FirestoreError ) => {
553
+ expect ( err ) . to . exist ;
554
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
555
+ expect ( err . message ) . to . contain (
556
+ 'Firestore transactions require all reads to be executed'
557
+ ) ;
558
+ } ) ;
559
+
560
+ const postSnap = await getDoc ( docRef ) ;
561
+ expect ( postSnap . get ( 'foo' ) ) . to . equal ( 'baz' ) ;
526
562
} ) ;
527
563
} ) ;
528
564
0 commit comments