UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

Tags: ,



I am getting this huge error running a Mocha test with node version 10 and a MongoDB database:

(node:27884) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert( user.blogPosts[0].comments[0].content === ‘As programs get bigger, they also become more complex’ )

at User.findOne.populate.then.user (/Users/danale/Projects/users/test/association_test.js:50:9)
at process._tickCallback (internal/process/next_tick.js:68:7) (node:27884) 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(). (rejection id: 1) (node:27884) [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.

For a Mocha test that was working earlier:

it('saves a full relation graph', done => {
    User.findOne({ name: 'Joe' })
      .populate({
        path: 'blogPosts',
        populate: {
          path: 'comments',
          model: 'comment',
          populate: {
            path: 'user',
            model: 'user'
          }
        }
      })
      .then(user => {
        assert(user.name === 'Joe');
        assert(user.blogPosts[0].title === 'Eloquent JavaScript');
        assert(
          user.blogPosts[0].comments[0].content ===
            'As programs get bigger, they also become more complex'
        );
        assert(user.blogPosts[0].comments[0].user.name === 'Joe');
        done();
      });
  });

So I know the issue is here:

assert(
   user.blogPosts[0].comments[0].content ===
     'As programs get bigger, they also become more complex'
);

but what exactly is wrong with this?

Answer

The error message was appropriate for the content I was testing because I was testing the wrong content in the it() block. I was testing the content inside my beforeEach():

beforeEach(done => {
    joe = new User({ name: 'Joe' });
    blogPost = new BlogPost({
      title: 'Eloquent JavaScript',
      content: 'As programs get bigger, they also become more complex'
    });

When I was really trying to test the content in the comment object here: comment = new Comment({ content: 'Love this post' });

Once I corrected this to the appropriate content above, all tests are passing.



Source: stackoverflow