1919
2020var neo4jv1 = require ( "../../lib/v1" ) ;
2121
22- var _console = console ;
23-
2422/**
2523* The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
2624*
@@ -29,7 +27,12 @@ var _console = console;
2927*/
3028describe ( 'examples' , function ( ) {
3129
32- var driverGlobal , out , console , originalTimeout ;
30+ var driverGlobal ;
31+ var console ;
32+ var originalTimeout ;
33+
34+ var testResultPromise ;
35+ var resolveTestResultPromise ;
3336
3437 beforeAll ( function ( ) {
3538 var neo4j = neo4jv1 ;
@@ -44,9 +47,13 @@ describe('examples', function() {
4447
4548 beforeEach ( function ( done ) {
4649
50+ testResultPromise = new Promise ( function ( resolve , reject ) {
51+ resolveTestResultPromise = resolve ;
52+ } ) ;
53+
4754 // Override console.log, to assert on stdout output
48- out = [ ] ;
49- console = { log : function ( msg ) { out . push ( msg ) ; } } ;
55+ console = { log : resolveTestResultPromise } ;
56+
5057 var session = driverGlobal . session ( ) ;
5158 session . run ( "MATCH (n) DETACH DELETE n" ) . then ( function ( ) {
5259 session . close ( ) ;
@@ -81,12 +88,13 @@ describe('examples', function() {
8188 console . log ( result . records [ 0 ] . get ( "title" ) + " " + result . records [ 0 ] . get ( "name" ) ) ;
8289 session . close ( ) ;
8390 driver . close ( ) ;
84- } )
85- // end::minimal-example[]
86- . then ( function ( ) {
87- expect ( out [ 0 ] ) . toBe ( "King Arthur" ) ;
88- done ( ) ;
8991 } ) ;
92+ // end::minimal-example[]
93+
94+ testResultPromise . then ( function ( loggedMsg ) {
95+ expect ( loggedMsg ) . toBe ( "King Arthur" ) ;
96+ done ( ) ;
97+ } ) ;
9098 } ) ;
9199
92100 it ( 'should be able to configure session pool size' , function ( done ) {
@@ -102,10 +110,11 @@ describe('examples', function() {
102110 console . log ( theOnesCreated ) ;
103111 s . close ( ) ;
104112 driver . close ( ) ;
105- } )
106- . then ( function ( ) {
107- expect ( out [ 0 ] ) . toBe ( 1 ) ;
108- done ( ) ;
113+ } ) ;
114+
115+ testResultPromise . then ( function ( loggedCount ) {
116+ expect ( loggedCount ) . toBe ( 1 ) ;
117+ done ( ) ;
109118 } ) ;
110119 } ) ;
111120
@@ -119,11 +128,12 @@ describe('examples', function() {
119128 var theOnesCreated = result . summary . counters . nodesCreated ( ) ;
120129 console . log ( "There were " + theOnesCreated + " the ones created." ) ;
121130 session . close ( ) ;
122- } )
123- . then ( function ( ) {
124- expect ( out [ 0 ] ) . toBe ( "There were 1 the ones created." ) ;
125- done ( ) ;
126131 } ) ;
132+
133+ testResultPromise . then ( function ( loggedMsg ) {
134+ expect ( loggedMsg ) . toBe ( "There were 1 the ones created." ) ;
135+ done ( ) ;
136+ } ) ;
127137 } ) ;
128138
129139 it ( 'should document a statement without parameters' , function ( done ) {
@@ -139,10 +149,10 @@ describe('examples', function() {
139149 } ) ;
140150
141151 // Then
142- setTimeout ( function ( ) {
143- expect ( out [ 0 ] ) . toBe ( "There were 1 the ones created." ) ;
152+ testResultPromise . then ( function ( loggedMsg ) {
153+ expect ( loggedMsg ) . toBe ( "There were 1 the ones created." ) ;
144154 done ( ) ;
145- } , 1000 )
155+ } ) ;
146156 } ) ;
147157
148158 it ( 'should be able to iterate results' , function ( done ) {
@@ -167,11 +177,12 @@ describe('examples', function() {
167177 } ) ;
168178 // end::result-traversal[]
169179 } ) ;
180+
170181 // Then
171- setTimeout ( function ( ) {
172- expect ( out [ 0 ] ) . toBe ( "Sword in the stone" ) ;
182+ testResultPromise . then ( function ( loggedMsg ) {
183+ expect ( loggedMsg ) . toBe ( "Sword in the stone" ) ;
173184 done ( ) ;
174- } , 1000 ) ;
185+ } ) ;
175186 } ) ;
176187
177188 it ( 'should be able to access records' , function ( done ) {
@@ -205,10 +216,10 @@ describe('examples', function() {
205216 } ) ;
206217
207218 // Then
208- setTimeout ( function ( ) {
209- expect ( out [ 0 ] . length ) . toBe ( 3 ) ;
219+ testResultPromise . then ( function ( loggedCount ) {
220+ expect ( loggedCount . length ) . toBe ( 3 ) ;
210221 done ( ) ;
211- } , 1000 )
222+ } ) ;
212223 } ) ;
213224
214225 it ( 'should be able to retain for later processing' , function ( done ) {
@@ -238,15 +249,14 @@ describe('examples', function() {
238249 // end::retain-result[]
239250 } ) ;
240251
241- //await the result
242- setTimeout ( function ( ) {
243- expect ( out [ 0 ] ) . toBe ( "Lancelot is a knight of Camelot" ) ;
252+ testResultPromise . then ( function ( loggedMsg ) {
253+ expect ( loggedMsg ) . toBe ( "Lancelot is a knight of Camelot" ) ;
244254 done ( ) ;
245- } , 1000 ) ;
255+ } ) ;
246256 } ) ;
247257
248258 it ( 'should be able to do nested queries' , function ( done ) {
249- var session = driverGlobal . session ( ) ; ;
259+ var session = driverGlobal . session ( ) ;
250260 session
251261 . run ( "CREATE (knight:Person:Knight {name: {name1}, castle: {castle}})" +
252262 "CREATE (king:Person {name: {name2}, title: {title}})" ,
@@ -277,25 +287,28 @@ describe('examples', function() {
277287 // end::nested-statements[]
278288 } ) ;
279289
280- //await the result
281- setTimeout ( function ( ) {
282- expect ( out [ 0 ] ) . toBe ( "Count is 1" ) ;
290+ testResultPromise . then ( function ( loggedMsg ) {
291+ expect ( loggedMsg ) . toBe ( "Count is 1" ) ;
283292 done ( ) ;
284- } , 1000 ) ;
293+ } ) ;
285294 } ) ;
286295
287296 it ( 'should be able to handle cypher error' , function ( done ) {
288297 var session = driverGlobal . session ( ) ;
289298
290299 // tag::handle-cypher-error[]
291300 session
292- . run ( "Then will cause a syntax error" )
301+ . run ( "This will cause a syntax error" )
293302 . catch ( function ( err ) {
294- expect ( err . fields [ 0 ] . code ) . toBe ( "Neo.ClientError.Statement.SyntaxError" ) ;
303+ console . log ( err ) ;
295304 session . close ( ) ;
296- done ( ) ;
297305 } ) ;
298306 // end::handle-cypher-error[]
307+
308+ testResultPromise . then ( function ( loggedError ) {
309+ expect ( loggedError . fields [ 0 ] . code ) . toBe ( "Neo.ClientError.Statement.SyntaxError" ) ;
310+ done ( ) ;
311+ } ) ;
299312 } ) ;
300313
301314 it ( 'should be able to profile' , function ( done ) {
@@ -306,18 +319,16 @@ describe('examples', function() {
306319 session
307320 . run ( "PROFILE MATCH (p:Person {name: {name}}) RETURN id(p)" , { name : "Arthur" } )
308321 . then ( function ( result ) {
309- //_console.log(result.summary.profile);
310322 console . log ( result . summary . profile ) ;
311323 session . close ( ) ;
312324 } ) ;
313325 // end::result-summary-query-profile[]
314326 } ) ;
315327
316- //await the result
317- setTimeout ( function ( ) {
318- expect ( out . length ) . toBe ( 1 ) ;
328+ testResultPromise . then ( function ( loggedMsg ) {
329+ expect ( loggedMsg ) . toBeDefined ( ) ;
319330 done ( ) ;
320- } , 2000 ) ;
331+ } ) ;
321332 } ) ;
322333
323334 it ( 'should be able to see notifications' , function ( done ) {
@@ -335,10 +346,10 @@ describe('examples', function() {
335346 } ) ;
336347 // end::result-summary-notifications[]
337348
338- setTimeout ( function ( ) {
339- expect ( out [ 0 ] ) . toBe ( "Neo.ClientNotification.Statement.CartesianProductWarning" ) ;
349+ testResultPromise . then ( function ( loggedMsg ) {
350+ expect ( loggedMsg ) . toBe ( "Neo.ClientNotification.Statement.CartesianProductWarning" ) ;
340351 done ( ) ;
341- } , 1000 ) ;
352+ } ) ;
342353 } ) ;
343354
344355 it ( 'should document committing a transaction' , function ( ) {
@@ -352,7 +363,7 @@ describe('examples', function() {
352363 } ) ;
353364
354365 it ( 'should document rolling back a transaction' , function ( ) {
355- var session = driverGlobal . session ( ) ; ;
366+ var session = driverGlobal . session ( ) ;
356367
357368 // tag::transaction-rollback[]
358369 var tx = session . beginTransaction ( ) ;
0 commit comments