Unable to pass test `npm test -t db-connection`

I set up my cluster and I am able to connect to it via the mongo shell - have checked and double checked my user name, password and uri. I was able to see that the movie data is loaded via the mongo shell.

MongoDB Enterprise mflix-shard-0:PRIMARY> db.movies.count()
23539

I updated the .env file (and changed the file name to be .env. I went into index.js and console.log(process.env) to make sure I can access those variables and their values. And I am able to see those values.

When I run npm test -t db-connection all the tests fail and I cannot figure out what I need to do to get the tests to pass.

mflix-js 💠  $ npm test -t db-connection

> server@1.0.0 test /Users/karolinrafalski/dev/mflix-js
> jest --passWithNoTests "db-connection"

Determining test suites to run...Setup Mongo Connection
mongodb+srv://m220student:m220password@mflix-pptth.mongodb.net/test?retryWrites=true&w=majority
 FAIL  test/db-connection.test.js
  Connection
    ✕ Can access MFlix data (43ms)
    ✕ Can retrieve a movie by id (25ms)
    ✕ Can retrieve first page of movies (71ms)

  ● Connection › Can access MFlix data

    expect(array).toContain(value)

    Expected array:
      []
    To contain value:
      "movies"

      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/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/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:28:13

  ● 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/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/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:28:13

  ● Connection › Can retrieve first page of movies

    expect(received).toEqual(expected)

    Expected value to equal:
      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/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/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:28:13

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

Hi @Karolin_38713,

Please share the contents of your .env file. :slight_smile: Also, check if there is any space/indentation issue.

Kanika

Thank you for taking the time to look at this. I really appreciate it.

# 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=super_secret_key_you_should_change_sure
MFLIX_DB_URI=mongodb+srv://m220student:m220password@mflix-pptth.mongodb.net/test?retryWrites=true&w=majority
PORT=5000

I had also changed it to this (I pressed undo to get it back) - same errors.

# 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=this_key_is_changed_to_something_else
MFLIX_DB_URI=mongodb+srv://m220student:m220password@mflix-pptth.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS=mflix
PORT=5000

SOLVED

I used nvm to change my node to 11.5.0 (chose this version somewhat randomly it was published in December 2018 that seemed ‘old enough’) from 11.15.0. I restarted my computer. Deleted the mflix project and downloaded it again. Ran npm install again. I followed the instructors in the lesson and got it working!

2 Likes