@@ -15,7 +15,17 @@ describe('Collection', function () {
1515 } ) ;
1616
1717 context ( '#createIndex' , ( ) => {
18- it ( 'should error when createIndex fails' , function ( done ) {
18+ let client ;
19+
20+ before ( function ( ) {
21+ client = new MongoClient ( `mongodb://${ server . uri ( ) } ` ) ;
22+ } ) ;
23+
24+ after ( async function ( ) {
25+ await client . close ( ) ;
26+ } ) ;
27+
28+ it ( 'should error when createIndex fails' , async function ( ) {
1929 const ERROR_RESPONSE = {
2030 ok : 0 ,
2131 errmsg :
@@ -39,27 +49,11 @@ describe('Collection', function () {
3949 }
4050 } ) ;
4151
42- const client = new MongoClient ( `mongodb://${ server . uri ( ) } ` ) ;
43-
44- const close = e => client . close ( ) . then ( ( ) => done ( e ) ) ;
45-
46- client
47- . connect ( )
48- . then ( ( ) => client . db ( 'foo' ) . collection ( 'bar' ) )
49- . then ( coll => coll . createIndex ( { a : 1 } ) )
50- . then (
51- ( ) => close ( 'Expected createIndex to fail, but it succeeded' ) ,
52- e => {
53- try {
54- expect ( e ) . to . have . property ( 'ok' , ERROR_RESPONSE . ok ) ;
55- expect ( e ) . to . have . property ( 'errmsg' , ERROR_RESPONSE . errmsg ) ;
56- expect ( e ) . to . have . property ( 'code' , ERROR_RESPONSE . code ) ;
57- close ( null ) ;
58- } catch ( err ) {
59- close ( err ) ;
60- }
61- }
62- ) ;
52+ const coll = client . db ( 'foo' ) . collection ( 'bar' ) ;
53+ const e = await coll . createIndex ( { a : 1 } ) . catch ( e => e ) ;
54+ expect ( e ) . to . have . property ( 'ok' , ERROR_RESPONSE . ok ) ;
55+ expect ( e ) . to . have . property ( 'errmsg' , ERROR_RESPONSE . errmsg ) ;
56+ expect ( e ) . to . have . property ( 'code' , ERROR_RESPONSE . code ) ;
6357 } ) ;
6458 } ) ;
6559
0 commit comments