@@ -22,4 +22,59 @@ describe('lifecycle hooks', () => {
2222 client . release ( )
2323 await pool . end ( )
2424 } )
25+
26+ it ( 'are called on connect with an async hook' , async ( ) => {
27+ const pool = new Pool ( {
28+ hooks : {
29+ connect : async ( client ) => {
30+ const res = await client . query ( 'SELECT 1 AS num' )
31+ client . HOOK_CONNECT_RESULT = res . rows [ 0 ] . num
32+ } ,
33+ } ,
34+ } )
35+ const client = await pool . connect ( )
36+ expect ( client . HOOK_CONNECT_RESULT ) . to . equal ( 1 )
37+ const res = await client . query ( 'SELECT 1 AS num' )
38+ expect ( res . rows [ 0 ] . num ) . to . equal ( 1 )
39+ client . release ( )
40+ const client2 = await pool . connect ( )
41+ expect ( client ) . to . equal ( client2 )
42+ expect ( client2 . HOOK_CONNECT_RESULT ) . to . equal ( 1 )
43+ client . release ( )
44+ await pool . end ( )
45+ } )
46+
47+ it ( 'errors out the connect call if the async connect hook rejects' , async ( ) => {
48+ const pool = new Pool ( {
49+ hooks : {
50+ connect : async ( client ) => {
51+ await client . query ( 'SELECT INVALID HERE' )
52+ } ,
53+ } ,
54+ } )
55+ try {
56+ await pool . connect ( )
57+ throw new Error ( 'Expected connect to throw' )
58+ } catch ( err ) {
59+ expect ( err . message ) . to . contain ( 'invalid' )
60+ }
61+ await pool . end ( )
62+ } )
63+
64+ it ( 'errors out the connect call if the connect hook throws' , async ( ) => {
65+ const pool = new Pool ( {
66+ hooks : {
67+ connect : ( ) => {
68+ throw new Error ( 'connect hook error' )
69+ } ,
70+ } ,
71+ } )
72+ try {
73+ await pool . connect ( )
74+ throw new Error ( 'Expected connect to throw' )
75+ } catch ( err ) {
76+ expect ( err . message ) . to . equal ( 'connect hook error' )
77+ }
78+ await pool . end ( )
79+ } )
2580} )
0 commit comments