Npm test -t projection

Good night! Mine is Wilson Palmeira, I am enrolled in the Course M220JS.
When I run npm test -t projection, it fails, below is the error message. How to fix it?

C:\mflix-js>npm test -t projection

server@1.0.0 test C:\mflix-js
jest --passWithNoTests “projection”

Determining test suites to run…Setup Mongo Connection
(node:5588) 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.
(node:5588) UnhandledPromiseRejectionWarning: Error: Caught error after test environment was torn down

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

Expected: 1468
Received: 0
(node:5588) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5588) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
FAIL test/projection.test.js (46.03s)
Projection
× Can perform a country search for one country (447ms)
× Can perform a country search for three countries (5008ms)

● Projection › Can perform a country search for one country

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

Expected: 2
Received: 0

   9 |     const kosovoList = ["Kosovo"]
  10 |     const movies = await MoviesDAO.getMoviesByCountry(kosovoList)
> 11 |     expect(movies.length).toEqual(2)
     |                           ^
  12 |   })
  13 |
  14 |   test("Can perform a country search for three countries", async () => {

  at toEqual (test/projection.test.js:11:27)
  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)

● Projection › Can perform a country search for three countries

: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error:

  12 |   })
  13 |
> 14 |   test("Can perform a country search for three countries", async () => {
     |   ^
  15 |     const countriesList = ["Russia", "Japan", "Mexico"]
  16 |     const movies = await MoviesDAO.getMoviesByCountry(countriesList)
  17 |     expect(movies.length).toEqual(1468)

  at new Spec (node_modules/@jest/core/node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
  at Suite.test (test/projection.test.js:14:3)

Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: 71.281s
Ran all test suites matching /projection/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.
events.js:292
throw er; // Unhandled ‘error’ event
^

Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted ‘error’ event on Connection instance at:
at TLSSocket. (C:\mflix-js\node_modules\mongodb\lib\core\connection\connection.js:321:10)
at Object.onceWrapper (events.js:422:26)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
name: ‘MongoNetworkError’,
errorLabels: [ ‘TransientTransactionError’ ],
[Symbol(mongoErrorContextSymbol)]: {}
}
npm ERR! Test failed. See above for more details.

C:\mflix-js>
C:\mflix-js>npm test -t db-connection

server@1.0.0 test C:\mflix-js
jest --passWithNoTests “db-connection”

Determining test suites to run…Setup Mongo Connection
(node:6288) 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
● Test suite failed to run

TypeError: Cannot read property 'close' of undefined

  17 |
  18 |   async teardown() {
> 19 |     await this.global.mflixClient.close()
     |                                   ^
  20 |     await super.teardown()
  21 |   }
  22 |

  at MongoEnvironment.teardown (test/config/mongoEnvironment.js:19:35)

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

Do you have a file name “.env” ? in mflix-js directory

Kanika

Yes, I have an env file. Below is:

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
MFLIX_DB_URI=mongodb+srv://m220student:m220password@m220-cyshi.mongodb.net/test?retryWrites=true
MFLIX_NS=sample_mflix
PORT=5000

Dear
Yes, I have the .env file. Below is the content.

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
MFLIX_DB_URI=mongodb+srv://m220student:m220password@m220-cyshi.mongodb.net/test?retryWrites=true
MFLIX_NS=sample_mflix
PORT=5000

Dear Kanika,
The script below is a .env file. I could make sure it’s MFLIX_DB_URI and the SECRET_KEY.
The error remains the same.

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=m220password
MFLIX_DB_URI=mongodb+srv://m220student:m220password@m220-cyshi.mongodb.net/test?retryWrites=true
MFLIX_NS=sample_mflix
PORT=5000


C:\mflix-js>npm test -t projection

server@1.0.0 test C:\mflix-js
jest --passWithNoTests “projection”

Determining test suites to run…Setup Mongo Connection
(node:5036) 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 the MongoClient constructor.
FAIL test/projection.test.js (15.152s)
Projection
× Can perform a country search for one country (412ms)
× Can perform a country search for three countries (395ms)

● Projection › Can perform a country search for one country

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

Expected: 2
Received: 0

   9 |     const kosovoList = ["Kosovo"]
  10 |     const movies = await MoviesDAO.getMoviesByCountry(kosovoList)
> 11 |     expect(movies.length).toEqual(2)
     |                           ^
  12 |   })
  13 |
  14 |   test("Can perform a country search for three countries", async () => {

  at toEqual (test/projection.test.js:11:27)
  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)

● Projection › Can perform a country search for three countries

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

Expected: 1468
Received: 0

  15 |     const countriesList = ["Russia", "Japan", "Mexico"]
  16 |     const movies = await MoviesDAO.getMoviesByCountry(countriesList)
> 17 |     expect(movies.length).toEqual(1468)
     |                           ^
  18 |     movies.map(movie => {
  19 |       const movieKeys = Object.keys(movie).sort()
  20 |       const expectedKeys = ["_id", "title"]

  at toEqual (test/projection.test.js:17:27)
  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: 2 failed, 2 total
Snapshots: 0 total
Time: 16.327s, estimated 17s
Ran all test suites matching /projection/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.

C:\mflix-js>

I suspect that you have not imported the data set into the sample_mflix database.

1 Like

Dear
You are referring to the sampledata.archive.gz file
I can’t import

C:\Program Files\MongoDB\Server\4.4\bin>mongorestore --gzip --archive=sampledata.archive.gz
2020-09-30T08:42:17.434-0300 Failed: CreateFile sampledata.archive.gz: O sistema não pode encontrar o arquivo especificado.
2020-09-30T08:42:17.570-0300 0 document(s) restored successfully. 0 document(s) failed to restore.

C:\Program Files\MongoDB\Server\4.4\bin>

Unless the course has changed drastically since I took it, your mongorestore command is far from the one indicated in the course instructions. I recommend you check over the steps from Lecture: README.

1 Like

What you can do is…

  1. Login to atlas
  2. In your atlas cluster, if you have not imported data yet, click on “…” button and “Load the Sample Dataset”.

It will load the sample dataset for you.

Kanika

Activities in Atlas or MongooDB Compass work perfectly; all are successfully performed.
The activities in the DOS prompt, some are not performed.
Please, why can’t I import:
setting Up Atlas
mongorestore --gzip --archive=sampledata.archive.gz

Because you do not specify the URI of the Atlas cluster.

Activities in Atlas or MongooDB Compass work perfectly; all are successfully performed.
The activities in the DOS prompt, some are not performed.
Please, why can’t I import:
setting Up Atlas
mongorestore --gzip --archive=sampledata.archive.gz
C:\Program Files\MongoDB\Server\4.4\bin>mongorestore --gzip --archive=sampledata.archive.gz
2020-09-30T13:49:40.030-0300 Failed: CreateFile sampledata.archive.gz: The system cannot find the specified file.
2020-09-30T13:49:40.070-0300 0 document(s) successfully restored. 0 document(s) failed to restore.

C:\Program Files\MongoDB\Server\4.4\bin>

A few things.

First, if you want to mongorestore to an Atlas cluster you must specify the address of an Atlas cluster on the command line. That is what I mean by

If you go back to the driver setup lesson you will see how to specify an Atlas Cluster from the command line with mongorestore.

Second, you are getting

This means the file does not exists the way you specified it. If the file is not present in the current directory, you must specify the full path name.

Last, if you successfully run the mongorestore command, you will mongorestore on your local instance of mongod, not on the Atlas cluster.

My problem continues i picked up the uri (uri - Atlas), changed the .env file and executed the command below.

C:\mflix-js>npm test -t projection

server@1.0.0 test C:\mflix-js
jest --passWithNoTests “projection”

Determining test suites to run…Setup Mongo Connection
(node:14152) 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/projection.test.js (23.945s)
Projection
× Can perform a country search for one country (452ms)
× Can perform a country search for three countries (401ms)

● Projection › Can perform a country search for one country

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

Expected: 2
Received: 0

   9 |     const kosovoList = ["Kosovo"]
  10 |     const movies = await MoviesDAO.getMoviesByCountry(kosovoList)
> 11 |     expect(movies.length).toEqual(2)
     |                           ^
  12 |   })
  13 |
  14 |   test("Can perform a country search for three countries", async () => {

  at toEqual (test/projection.test.js:11:27)
  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)

● Projection › Can perform a country search for three countries

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

Expected: 1468
Received: 0

  15 |     const countriesList = ["Russia", "Japan", "Mexico"]
  16 |     const movies = await MoviesDAO.getMoviesByCountry(countriesList)
> 17 |     expect(movies.length).toEqual(1468)
     |                           ^
  18 |     movies.map(movie => {
  19 |       const movieKeys = Object.keys(movie).sort()
  20 |       const expectedKeys = ["_id", "title"]

  at toEqual (test/projection.test.js:17:27)
  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: 2 failed, 2 total
Snapshots: 0 total
Time: 25.224s
Ran all test suites matching /projection/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.

Post screenshot of your .env file.

Post screenshot of your mongorestore command.

I’m sorry about the job, but I really want to get it right.

Thank you very much!

The database sample_mflix is not loaded on your cluster.

Your command mongorestore is failing because the file sampledata.archive.gz does not exist in the directory where you run it. You must specify the full path name to the file.

1 Like

Dear, good morning!
I wonder if you have come to a conclusion about this problem.
I’m sure I’m not the only one, I ask you not to forget me; I know you can help me.
Thank you very much for your attention.

mongorestore --gzip --archive=sampledata.archive.gz
npm test -t db-connection
Start npm
npm test -t projection

Same conclusion as before.

And the following does not restore the data on your cluster.

Correct, but only on the command line with MongoDB Compass Do all the work. I can also use the MongoDB (CRUD,…) features in CMD DOS. You could send the database so I can proceed on the course.