Ticket Get Comments

This is my implementation. It fails.

** Mod Edit **
Removed implementation

And the error is:

➜  mflix-js npm test -t get-comments

> server@1.0.0 test /Users/UserName/mflix-js
> jest --passWithNoTests "get-comments"

Determining test suites to run...Setup Mongo Connection

 FAIL  test/get-comments.test.js (8.241s)
  Get Comments
    ✓ Can fetch comments for a movie (288ms)
    ✕ comments should be sorted by date (5025ms)

  ● Get Comments › comments should be sorted by date

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

      13 |   })
      14 | 
    > 15 |   test('comments should be sorted by date', async () => {
         |   ^
      16 |     // most recent to least
      17 |     expect.assertions(24)
      18 |     const movieIds = [

      at Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:85:20)
      at test (test/get-comments.test.js:15:3)
      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 next] (node_modules/regenerator-runtime/runtime.js:114:21)

  ● Get Comments › comments should be sorted by date

    expect.assertions(24)

    Expected 24 assertions to be called but received twelve assertion calls.

      15 |   test('comments should be sorted by date', async () => {
      16 |     // most recent to least
    > 17 |     expect.assertions(24)
         |            ^
      18 |     const movieIds = [
      19 |       '573a1391f29313caabcd8414',
      20 |       '573a1391f29313caabcd9058',

      at assertions (test/get-comments.test.js:17:12)
      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 next] (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:35:14
      at new F (node_modules/babel-runtime/node_modules/core-js/library/modules/_export.js:36:28)
      at Object.<anonymous> (node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        8.51s, estimated 9s
Ran all test suites matching /get-comments/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.
npm ERR! Test failed.  See above for more details.

Yet, the /status passed. I tried with removing quotations around some of the operators in the $lookup pipeline (in fact exactly as the solution), but still the same error.

I have 2 questions.

  1. What am I doing wrong? I’ve been on it for more than 6 hours reading all posts relating to Get Comments but there’s none related to my error, and searching the internet for operator syntax. I am frustrated. Please help.
  2. Quotations around operators. Can someone explain when quotation is required and when it is not under the Aggregation Pipeline?

Thank you.

1 Like

First, I removed your implementation to avoid any spoilers for anyone else.

Your pipeline looks entirely correct, but the test failure is pointing out an issue with the promise being resolved, the bit about Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

Could you DM me your full implementation, including the return bit and the catch block?

Hi Nathan, how do I direct message you, assuming DM means that?

In answer to your second question, quotes are necessary when you have nested keys such as “foo.bar”, and on the right hand side of of keys when dealing with strings or aggregation variables such as "$$id".

We provided a .prettierrc and .eslintrc configuration files. If you are using an IDE with plugin architecture I’d recommend installing the Prettier plugin and ESLint. These will enforce quotation where it is necessary. If you don’t want to use these, surrounding all keys with quotes is entirely valid and in fact slightly more correct as everything really is JSON. Javascript gives you a little more flexibility as JSON is native.

Sorry for the confusion here!

You can click on my name, then click on the Message button.

Clicked on your name. Shows this.
Clicked%20on%20Nathan%20Leniz%20name

Then clicked further and shows your page.
Nathan%20Leniz%20page

There’s no Message button.

I thought I somehow messed up the database and so dropped and imported it again into Atlas. But still same error.

Since this is Discourse platform, I found this link saying to click Message button too. But yours must have been disabled somehow. Could you please enable it?
https://discourse.edi-net.eu/t/did-you-know-you-can-use-discourse-to-send-private-messages/240

1 Like

Do you sort the comments by date descending?

A lot of people forgot this part of the requirements.

2 Likes

This was covered on an earlier thread. Really should have been posted as it’s own thread

2 Likes

Indeed, this works as explained. Add a new /jest.setup.js file at the project root folder and add one more line setupTestFrameworkScriptFile: './jest.setup.js' in /jest.config.js.

Thank you @neillunn. And thank you @nathan.leniz for helping out previously.

Hi @nathan.leniz, while implementing Get comments ticket i am getting following errors . Please advise.
image

image

@Rahul_Singh18, as the error message indicates, you have a TypeError. It means that you do not return and array as the comments field of the movie.

@steevej-1495 does it mean i need to modify the return statement , I assumed i have to just implement the pipeline and not change the default code ( here return await movies.aggregate(pipeline).next() )

pleaaaaaaaaaaaaaaaaaase help my head is about to explode :joy:

here is my code

const pipeline = [
   
    
          {$match: {
           '_id': ObjectId(id)
           }},


           {$lookup: {
            from: "comments", 
            localField: "_id",
            foreignField: "movie_id",
            as: "comments"}},

            {$sort:
              {
                date: -1
              } }    
          
  ]

and I did add the jest.setup.js with the line of code
jest.setTimeout(30000)

and updated the jest.config.js to :

module.exports = {
  globalSetup: "./test/config/setup.js",
  globalTeardown: "./test/config/teardown.js",
  testEnvironment: "./test/config/mongoEnvironment",
  setupTestFrameworkScriptFile: './jest.setup.js'        // additional line
}



and still get same error 
Get Comments
    ✓ Can fetch comments for a movie (622ms)
    ✕ comments should be sorted by date (3297ms)

  ● Get Comments › comments should be sorted by date

 
  ● Get Comments › comments should be sorted by date

   
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        5.938s, estimated 7s
Ran all test suites matching /get-comments/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.
npm ERR! Test failed.  See above for more details.

How is the first test case passing , even i am doing the same and my test cases are failing.

The pipeline must sort the comments and put them in a field named comments.

2 Likes

You must use the new $lookup syntax:

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

Hi @nathan.leniz/ @steevej-1495 i got the first test case to pass , for the second one i am getting the error as :

i sorted as per date ( desc order ), not getting what am i missing here :frowning_face: . Please advise.

The error tells you that the comments are not sorted correctly. Your first comment is from 1980 and they were expecting a comment from 1979. So I think your comments are not for the expected movie.

1 Like