Get-comments failed with wrong movie name

Hi, I am working on getMovieByID methon in movieDAO.

Updated code -
const pipeline =
{
$match: {
_id: ObjectId(id)
},
lookup: { from: 'comments', let: {'id' : '_id’},
pipeline:[
{
$match: { $expr : {$eq : [’$movie_id’,’$$id’]}}
},
{ $sort : {date : -1}}
],
as: ‘comments’
}
}

  return await movies.aggregate(pipeline).next()

I tried to use as : ‘movie.comments’ also, got same error.

Validation shows below error-
expect(received).toEqual(expected) // deep equality

Expected: "The Express"
Received: "Blacksmith Scene"

   9 |     const id = "573a13b5f29313caabd42c2f"
  10 |     const movie = await MoviesDAO.getMovieByID(id)
> 11 |     expect(movie.title).toEqual("The Express")
     |                         ^
  12 |     expect(movie.comments.length).toBe(147)
  13 |   })

What is the exact error message from your failed

See below full error detail:

FAIL test/get-comments.test.js
Get Comments
× Can fetch comments for a movie (419ms)
× comments should be sorted by date (375ms)

● Get Comments › Can fetch comments for a movie

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

Expected: "The Express"
Received: "Blacksmith Scene"

   9 |     const id = "573a13b5f29313caabd42c2f"
  10 |     const movie = await MoviesDAO.getMovieByID(id)
> 11 |     expect(movie.title).toEqual("The Express")
     |                         ^
  12 |     expect(movie.comments.length).toBe(147)
  13 |   })
  14 |

  at toEqual (test/get-comments.test.js:11:25)
  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)

● Get Comments › comments should be sorted by date

TypeError: Cannot read property 'slice' of undefined

  20 |       const movie = await MoviesDAO.getMovieByID(id)
  21 |       const comments = movie.comments
> 22 |       const sortedComments = comments.slice()
     |                                       ^
  23 |       sortedComments.sort((a, b) => b.date.getTime() - a.date.getTime())
  24 |
  25 |       for (let i = 0; i < Math.min(10, comments.length); i++) {

  at slice (test/get-comments.test.js:22:39)
  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)

● Get Comments › comments should be sorted by date

expect.assertions(10)

Expected ten assertions to be called but received zero assertion calls.

  15 |   test("comments should be sorted by date", async () => {
  16 |     // most recent to least
> 17 |     expect.assertions(10)
     |            ^
  18 |     const movieIds = ["573a13b5f29313caabd42c2f"]
  19 |     const promises = movieIds.map(async id => {
  20 |       const movie = await MoviesDAO.getMovieByID(id)

  at assertions (test/get-comments.test.js:17:12)
  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)
  at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
  at Object.<anonymous> (node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)

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

If getMovieByID fails then all other test will probably failed.

I am very surprise that you return the wrong movie. This indicates that somewhere a movie is created with an _id that is not an object id. So you have one movie that has 573a…2j, a string as _id and another with ObjectId( 573a…2j ) as _id. It is either like that in the supplied data or a movie is created wrongly. It might be the test driver that does it test if your code handle the id correctly.

Resolved by updating pipeline values as below format
[
{ $match:{} },
{ $lookup:{} }
]

Thanks

1 Like