Mongoclient test fail

Cannot read property ‘close’ of undefined
expect(e).toBeNull()
68 | } finally {
> 69 | testClient.close()
| ^
70 | }
71 | })
72 |
I am able to run application with npm start but issue with test

Are you connecting to the same server?

It looks like the test uri is wrong.

i thing i am pickin uri from .env for all connections. i am able to pass -db_connection test
but not mongoclient test “npm test -t mongoclient”

Do you have any other errors? Having the stack trace produced would be nice.

this is the stack trace produced:
I can pass connection test via status of mflix but command “npm test -t mongoclient fails” as follow:

Determining test suites to run…Setup Mongo Connection
FAIL test/lessons/mongoclient.spec.js (8.136s)
MongoClient
√ Client initialized with URI (1570ms)
× Client initialized with URI and options (933ms)
√ Database handle created from MongoClient (1752ms)
× Collection handle created from database handle (698ms)

● MongoClient › Client initialized with URI and options

TypeError: Cannot read property 'close' of undefined

  67 |       expect(e).toBeNull()
  68 |     } finally {
> 69 |       testClient.close()
     |                  ^
  70 |     }
  71 |   })
  72 | 

  at close (test/lessons/mongoclient.spec.js:69:18)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as throw] (node_modules/regenerator-runtime/runtime.js:114:21)
  at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
  at node_modules/babel-runtime/helpers/asyncToGenerator.js:30:13

● MongoClient › Collection handle created from database handle

TypeError: Cannot read property 'close' of undefined

  137 |       expect(e).toBeNull()
  138 |     } finally {
> 139 |       testClient.close()
      |                  ^
  140 |     }
  141 |   })
  142 | })

  at close (test/lessons/mongoclient.spec.js:139:18)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as throw] (node_modules/regenerator-runtime/runtime.js:114:21)
  at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
  at node_modules/babel-runtime/helpers/asyncToGenerator.js:30:13

Hi @satyam_04512,

Please share the uri which you have set in .env file.

Kanika

thanks for reply,
following is the uri
mongodb+srv://m220student:m220password@m220-wnxab.mongodb.net/test?retryWrites=true&w=majority

Hi @satyam_04512,

This seems to be a networking issue, make sure you are not connected to VPN or any other blocking issue.
Meanwhile I am searching more on this.

Kanika

thanks i will check by switching n/w

Hi,

Did you solve this issue? I am in the same situation. Could you please give me some advice?

Thank you!

Hi @FENG_HSIANG_23841,

Please check the following things:

  • Your connection string is updated in .ini file.
  • Firewall/Anti-virus is disabled.

Kanika

Thanks kanika for reply
currently i have unregistered this cource and will start from next cource
I will check these things and reply with updates.

1 Like

Hi, I’m having the same problem, see stack trace below. It is only the two tests where the connectTimeoutMS and retryWrites option are being specified that are failing.
If I remove those options and comment out the assertions that check for those client options then all tests pass, so I am not sure if that could be caused by a connection/network problem.

$ npm test -t mongoclient

server@1.0.0 test /home/me/myapps/learn/mflix-js
jest --passWithNoTests “mongoclient”

Determining test suites to run…Setup Mongo Connection
(node:12555) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
FAIL test/lessons/mongoclient.spec.js (7.062s)
MongoClient
✓ Client initialized with URI (1750ms)
✕ Client initialized with URI and options (437ms)
✓ Database handle created from MongoClient (1849ms)
✕ Collection handle created from database handle (434ms)

● MongoClient › Client initialized with URI and options

TypeError: Cannot read property 'close' of undefined

  67 |       expect(e).toBeNull()
  68 |     } finally {
> 69 |       testClient.close()
     |                  ^
  70 |     }
  71 |   })
  72 | 

  at close (test/lessons/mongoclient.spec.js:69:18)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.(anonymous function) [as throw] (node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _throw (node_modules/@babel/runtime/helpers/asyncToGenerator.js:29:9)

● MongoClient › Collection handle created from database handle

TypeError: Cannot read property 'close' of undefined

  137 |       expect(e).toBeNull()
  138 |     } finally {
> 139 |       testClient.close()
      |                  ^
  140 |     }
  141 |   })
  142 | })

  at close (test/lessons/mongoclient.spec.js:139:18)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.(anonymous function) [as throw] (node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _throw (node_modules/@babel/runtime/helpers/asyncToGenerator.js:29:9)

Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 passed, 4 total
Snapshots: 0 total
Time: 7.365s, estimated 8s
Ran all test suites matching /mongoclient/i.
Teardown Mongo Connection
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren’t stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

Found the problem.
In the test results I noticed that the latency for me to Atlas/AWS is very high:
FAIL test/lessons/mongoclient.spec.js (7.062s)
MongoClient
✓ Client initialized with URI (1750ms)
✕ Client initialized with URI and options (437ms)
✓ Database handle created from MongoClient (1849ms)
✕ Collection handle created from database handle (434ms)
So I increased the connectTimeoutMS for those two tests to 600.

  test("Client initialized with URI and options", async () => {
    /**
     * Here we've initialized a MongoClient with a URI string, as well as a few
     * optional parameters. In this case, the parameters we set are
     * connectTimeoutMS, retryWrites and again useNewUrlParser.
     */

    let testClient
    try {
      testClient = await MongoClient.connect(process.env.MFLIX_DB_URI, {
        connectTimeoutMS: 600,
        retryWrites: true,
        useNewUrlParser: true,
      })

      const clientOptions = testClient.s.options

      // expect clientOptions to have the correct settings
      expect(clientOptions.connectTimeoutMS).toBe(600)
      expect(clientOptions.retryWrites).toBe(true)
      expect(clientOptions.useNewUrlParser).toBe(true)
    } catch (e) {
      expect(e).toBeNull()
    } finally {
      testClient.close()
    }
  })

test("Collection handle created from database handle", async () => {
    /**
     * From the database handle, naturally comes the collection handle. We
     * verified in the previous test that the "mflix" database has a collection
     * called "movies". Now let's see if the "movies" collection has all the
     * data we expect.
     */

    let testClient
    try {
      testClient = await MongoClient.connect(process.env.MFLIX_DB_URI, {
        connectTimeoutMS: 600,
        retryWrites: true,
        useNewUrlParser: true,
      })

      // create a database object for the "mflix" database
      const mflixDB = testClient.db(process.env.MFLIX_NS)

      // create a collection object for the "movies" collection
      const movies = mflixDB.collection("movies")

      // expect the "movies" collection to have the correct number of movies
      const numMoves = await movies.countDocuments({})
      expect(numMoves).toBe(23539)
    } catch (e) {
      expect(e).toBeNull()
    } finally {
      testClient.close()
    }
  })
4 Likes

Thanks for this, I had the same problem.

@Kanika The canonical answer thread should really be updated with this info:

As should the test in the course!!

For reference, here’s my test durations on a powerful macbook pro and a 400Mbps internet connection:

    ✓ Client initialized with URI (1883ms)
    ✓ Client initialized with URI and options (1771ms)
    ✓ Database handle created from MongoClient (2150ms)
    ✓ Collection handle created from database handle (2168ms)

My issue being there are no free tier locations in my country, so the latency is high

I’m certain I’m not the only one with this issue. A quick search shows many thread with this issue

1 Like

Thank you so much for debugging the issue @jrohlandt. I will update the post.

@Frederick_01065, I understand the issue here. But I am not sure if I will be able to help you in this one.

One way is you can raise help ticket for ATLAS and see what they come up with. :slight_smile:

Let me know if you have any questions.

Kanika

I’m fine with having latency during a course, It would be nice if the test options didn’t set a timeout so short that anyone outside a free region can’t possibly pass the test

What timeout value worked for you?

We added timeouts so that if the query is wrong, it should not wait for a long time to throw an error. But I got your point, I will share it with the team to increase the timeout. Thanks :slight_smile:

Kanika

1 Like