@@ -60,6 +60,59 @@ describe('Transaction', () => {
6060
6161 } )
6262
63+ describe ( '.close()' , ( ) => {
64+ describe ( 'when transaction is open' , ( ) => {
65+ it ( 'should roll back the transaction' , async ( ) => {
66+ const connection = newFakeConnection ( )
67+ const tx = newTransaction ( { connection } )
68+
69+ await tx . run ( 'RETURN 1' )
70+ await tx . close ( )
71+
72+ expect ( connection . rollbackInvoked ) . toEqual ( 1 )
73+ } )
74+
75+ it ( 'should surface errors during the rollback' , async ( ) => {
76+ const expectedError = new Error ( 'rollback error' )
77+ const connection = newFakeConnection ( ) . withRollbackError ( expectedError )
78+ const tx = newTransaction ( { connection } )
79+
80+ await tx . run ( 'RETURN 1' )
81+
82+ try {
83+ await tx . close ( )
84+ fail ( 'should have thrown' )
85+ } catch ( error ) {
86+ expect ( error ) . toEqual ( expectedError )
87+ }
88+ } )
89+ } )
90+
91+ describe ( 'when transaction is closed' , ( ) => {
92+ const commit = async ( tx : Transaction ) => tx . commit ( )
93+ const rollback = async ( tx : Transaction ) => tx . rollback ( )
94+ const error = async ( tx : Transaction , conn : FakeConnection ) => {
95+ conn . withRollbackError ( new Error ( 'rollback error' ) )
96+ return tx . rollback ( ) . catch ( ( ) => { } )
97+ }
98+
99+ it . each ( [
100+ [ 'commmited' , commit ] ,
101+ [ 'rolled back' , rollback ] ,
102+ [ 'with error' , error ]
103+ ] ) ( 'should not roll back the connection' , async ( _ , operation ) => {
104+ const connection = newFakeConnection ( )
105+ const tx = newTransaction ( { connection } )
106+
107+ await operation ( tx , connection )
108+ const rollbackInvokedAfterOperation = connection . rollbackInvoked
109+
110+ await tx . close ( )
111+
112+ expect ( connection . rollbackInvoked ) . toEqual ( rollbackInvokedAfterOperation )
113+ } )
114+ } )
115+ } )
63116} )
64117
65118function newTransaction ( {
@@ -69,9 +122,9 @@ function newTransaction({
69122 lowRecordWatermark = 300
70123} : {
71124 connection : FakeConnection
72- fetchSize : number
73- highRecordWatermark : number ,
74- lowRecordWatermark : number
125+ fetchSize ? : number
126+ highRecordWatermark ? : number ,
127+ lowRecordWatermark ? : number
75128} ) : Transaction {
76129 const connectionProvider = new ConnectionProvider ( )
77130 connectionProvider . acquireConnection = ( ) => Promise . resolve ( connection )
0 commit comments