Const testClient: not defined #M220JS @Ad_58764

Chapter 1; example m220js mongoclient:

In the example code you do not use the expression let testClient,
but try {
const testClient = await MongoClient.connect(

This I tried, but it gives an error:
$ npm test -t mc

server@1.0.0 test D:\Projects\M220\mflix-js
jest --passWithNoTests “mc”

Determining test suites to run…Setup Mongo Connection
FAIL ./mc.spec.js
MongoClient
× Client initialized with URI (811ms)

● MongoClient › Client initialized with URI

ReferenceError: testClient is not defined

  21 |       expect(e).toBeNull()
  22 |     } finally {
> 23 |       testClient.close()
     |       ^
  24 |     }
  25 |   })
  26 | 

So, I am unsure if your example is correct?

Thanks for your support,
Regards, Ad

The code is fine in …/test/lessons/mongoclient.spec.js so I assume the content creators are aware of this.

Oké; I found out how to do it: the testClient.close() has to be placed inside the try-block.
Thank you

Not sure that moving the close() into the try block is the best approach. I think it’s in the finally block so that the connection is always closed even if some other exception occurs, but this doesn’t work if the exception is thrown whilst opening the connection, because if we fail to open a connection then testClient won’t have been set to anything.

Try increasing the value of the connectTimeoutMS parameter passed to MongoClient.connect, 1000 worked for me.