Npm test -t db-connection fails

halo tried run this “npm test -t db-connection” command but the test keeps failing and returning
the following err msg

 server@1.0.0 test /home/olaneat/Desktop/filez/project/mongo/mflix
> jest --passWithNoTests "db-connection"

Determining test suites to run...Setup Mongo Connection
(node:20772) 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/db-connection.test.js (19.763s)
  Connection
    ✕ Can access MFlix data (1179ms)
    ✕ Can retrieve a movie by id (2687ms)
    ✕ Can retrieve first page of movies (1882ms)

  ● Connection › Can access MFlix data

    expect(received).toContain(expected) // indexOf

    Expected value: "movies"
    Received array: []

      10 |     const collections = await mflix.listCollections().toArray()
      11 |     const collectionNames = collections.map(elem => elem.name)
    > 12 |     expect(collectionNames).toContain("movies")
         |                             ^
      13 |     expect(collectionNames).toContain("comments")
      14 |     expect(collectionNames).toContain("users")
      15 |   })

      at toContain (test/db-connection.test.js:12:29)
      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.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

  ● Connection › Can retrieve a movie by id

    TypeError: Cannot read property 'title' of null

      18 |     const id = "573a13a6f29313caabd17bd5"
      19 |     const movie = await MoviesDAO.getMovieByID(id)
    > 20 |     expect(movie.title).toEqual("Once Upon a Time in Mexico")
         |                  ^
      21 |   })
      22 | 
      23 |   test("Can retrieve first page of movies", async () => {

      at title (test/db-connection.test.js:20: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.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

  ● Connection › Can retrieve first page of movies

    expect(received).toEqual(expected) // deep equality

    Expected: 20
    Received: 0

      26 |       totalNumMovies: numMovies,
      27 |     } = await MoviesDAO.getMovies()
    > 28 |     expect(firstPage.length).toEqual(20)
         |                              ^
      29 |     expect(numMovies).toEqual(23539)
      30 |   })
      31 | })

      at toEqual (test/db-connection.test.js:28:30)
      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.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

Test Suites: 1 failed, 1 total
Tests:       3 failed, 3 total
Snapshots:   0 total
Time:        20.759s
Ran all test suites matching /db-connection/i.
Teardown Mongo Connection
npm ERR! Test failed.  See above for more details.

npm start starts the node server but when i click on status and connection i get this err

 Connection: The return from the api was incorrect 

Hi @Tosin_15696,

Did you import the data in your cluster? Looks like collections are empty.

Kanika

yea, i imported data into the cluster, when i created the cluster i clicked on the load data, but when i try loading the data in the CLI using this command

mongorestore --drop --gzip --uri "mongodb+srv://olaneat:@mflix-ritt8.mongodb.net/test?retryWrites=true&w=majority" data
i get this error
2020-02-07T09:11:32.579+0100	WARNING: ignoring unsupported URI parameter 'retrywrites'
2020-02-07T09:11:35.789+0100	Failed: mongorestore target 'data' invalid: stat data: no such file or directory

If you have imported the data already, then you do not need to import this again. It was optional.

To confirm, look at count of documents in movies database, if it > 0, then please proceed with the course. :slight_smile:

Kanika

running the db.movies.count()
returned 23539 as the number of movies but now the issue is answering the first quizz
anytime i click on connection it returns this msg
Connection: The return from the api was incorrect

Can you share any output you see in console? Also, did the unit test pass?

If not,

  • Rename the file to .env
  • Make sure cluster string is correct and your system is able to connect.

Kanika

i’ve renamed the env_unix to .env, i didn’t pass the unit test, its the output i displayed as my first message, the cluster string is correct and i can connect to ny atlas using the same cluster string via my mongodb-compass and cli

i think this is where i am getting it wrong, when i try to upload to my cluster using terminal
i get this err msg

mongorestore --drop --gzip --uri mongodb+srv://m220student:m220password@mflix-ncxgv.mongodb.net/test?retryWrites=true&w=majority data
[5] 10650
2020-02-09T10:05:58.694+0100	WARNING: ignoring unsupported URI parameter 'retrywrites'

Command 'data' not found, did you mean:

  command 'date' from deb coreutils (8.30-1ubuntu1)
  command 'dat' from deb liballegro4-dev (2:4.4.2-13)

Try: sudo apt install <deb name>

olaneat@olaneat:~/Desktop/filez/project/mongo/file/mflix/data/data/mflix$ 2020-02-09T10:06:04.400+0100	using default 'dump' directory
2020-02-09T10:06:04.400+0100	see mongorestore --help for usage information
2020-02-09T10:06:04.400+0100	Failed: mongorestore target 'dump' invalid: stat dump: no such file or directory


I would try to put the URI inside single quotes. I suspect that & starts the part before it in background and the remining part cause the data command not found.

i’ve tried that and it’s isnt working

mongorestore --drop --gzip --uri 'mongodb+srv://m220student:m220password@mflix-ncxgv.mongodb.net/test?retryWrites=true&w=majority' data

… but as it says in the documentation:

that exactly how i have the URI but it’s not still not working

What is the error message you get with the quotes?

Can you try after removing query parameters? like below:

  mongodb+srv://m220student:m220password@mflix-ncxgv.mongodb.net

If still not resolved, please share the content of your .env file.

Kanika

1 Like

i’ve been able to load the data now but when i run the test still get err same err and when i start the server and try to check the status to answer the first lab, i still get the same err message

Please share.

Kanika

# Ticket: Connection
# Rename this file to .env after filling in your MFLIX_DB_URI and your SECRET_KEY
# Do not surround the URI with quotes
SECRET_KEY=b'Sv\xfan\xa1h\xd3\xbf8\x1erSe\x94'a3'
MFLIX_DB_URI=mongodb+srv://m220student:m220password@mflix-ncxgv.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS=mflix
PORT=5000

Can you try replacing this with below:

MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-ncxgv.mongodb.net
MFLIX_NS= sample_mflix

Also, if you have older files/dataset (movies documents > 23539), please re-download handouts, remove the dataset, import the sample dataset as shown in the README screenshots from atlas.

Kanika

1 Like

Hi,

I had the same issue and after going through the forum searching for help, I found @kanikasingla suggestion helpful.
However, 1 test was still failing but the other 2 passed.

This is what worked for me:

  1. I copied the first part of the SRV string as provided by @kanikasingla which includes the username and password and stopped at the “@” sign:

mongodb+srv://m220student:m220password@

  1. Then I opened my Atlas cluster and clicked on Connect with the mongo shell then copied the middle part of the SRV string provided.
    It looks like this: mflix.23mdo.mongodb.net

Just the part I highlighted in the string below
mongo “mongodb+srv://mflix.23mdo.mongodb.net/” --username m220student

Joining the both strings looks like this:
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix.23mdo.mongodb.net

I’ve been able to pass all the tests and connect successfully.

I hope it helps @Tosin_15696

2 Likes