Mocha test fail ensure don() is called












0















Hi I have strange problem while testing code with Mocha:




Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves




Here is code:



describe('POST /notes', () => {
it('should create new note', (done) => {
const title = 'Test title';
const text = 'Test text';
const category = 'Test category';
request(app)
.post('/notes')
.send({title, text, category})
.expect(200)
.expect(res => {
expect(res.body.title).toBe(title);
})
.end((err, res) => {
if (err)
return done(err);

Note.find({text: text}).then(notes => {
expect(notes.length).toBe(1);
expect(notes[0].title).toBe(title);
done();
}).catch(err => done(err));
});
});
it('should not create new note with invalid body data', done => {
request(app)
.post('/notes')
.send({})
.expect(400)
.end((err, res) => {
if (err)
return done(err);

Note.find().then(notes => {
expect(notes.length).toBe(notesDummy.length);
done();
}).catch(err => done(err));
});
})


});



First test fails with error described above. As it comes to the second, it passes. Both tests are similar and I don't know what I am missing... Any ideas ?










share|improve this question



























    0















    Hi I have strange problem while testing code with Mocha:




    Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves




    Here is code:



    describe('POST /notes', () => {
    it('should create new note', (done) => {
    const title = 'Test title';
    const text = 'Test text';
    const category = 'Test category';
    request(app)
    .post('/notes')
    .send({title, text, category})
    .expect(200)
    .expect(res => {
    expect(res.body.title).toBe(title);
    })
    .end((err, res) => {
    if (err)
    return done(err);

    Note.find({text: text}).then(notes => {
    expect(notes.length).toBe(1);
    expect(notes[0].title).toBe(title);
    done();
    }).catch(err => done(err));
    });
    });
    it('should not create new note with invalid body data', done => {
    request(app)
    .post('/notes')
    .send({})
    .expect(400)
    .end((err, res) => {
    if (err)
    return done(err);

    Note.find().then(notes => {
    expect(notes.length).toBe(notesDummy.length);
    done();
    }).catch(err => done(err));
    });
    })


    });



    First test fails with error described above. As it comes to the second, it passes. Both tests are similar and I don't know what I am missing... Any ideas ?










    share|improve this question

























      0












      0








      0








      Hi I have strange problem while testing code with Mocha:




      Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves




      Here is code:



      describe('POST /notes', () => {
      it('should create new note', (done) => {
      const title = 'Test title';
      const text = 'Test text';
      const category = 'Test category';
      request(app)
      .post('/notes')
      .send({title, text, category})
      .expect(200)
      .expect(res => {
      expect(res.body.title).toBe(title);
      })
      .end((err, res) => {
      if (err)
      return done(err);

      Note.find({text: text}).then(notes => {
      expect(notes.length).toBe(1);
      expect(notes[0].title).toBe(title);
      done();
      }).catch(err => done(err));
      });
      });
      it('should not create new note with invalid body data', done => {
      request(app)
      .post('/notes')
      .send({})
      .expect(400)
      .end((err, res) => {
      if (err)
      return done(err);

      Note.find().then(notes => {
      expect(notes.length).toBe(notesDummy.length);
      done();
      }).catch(err => done(err));
      });
      })


      });



      First test fails with error described above. As it comes to the second, it passes. Both tests are similar and I don't know what I am missing... Any ideas ?










      share|improve this question














      Hi I have strange problem while testing code with Mocha:




      Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves




      Here is code:



      describe('POST /notes', () => {
      it('should create new note', (done) => {
      const title = 'Test title';
      const text = 'Test text';
      const category = 'Test category';
      request(app)
      .post('/notes')
      .send({title, text, category})
      .expect(200)
      .expect(res => {
      expect(res.body.title).toBe(title);
      })
      .end((err, res) => {
      if (err)
      return done(err);

      Note.find({text: text}).then(notes => {
      expect(notes.length).toBe(1);
      expect(notes[0].title).toBe(title);
      done();
      }).catch(err => done(err));
      });
      });
      it('should not create new note with invalid body data', done => {
      request(app)
      .post('/notes')
      .send({})
      .expect(400)
      .end((err, res) => {
      if (err)
      return done(err);

      Note.find().then(notes => {
      expect(notes.length).toBe(notesDummy.length);
      done();
      }).catch(err => done(err));
      });
      })


      });



      First test fails with error described above. As it comes to the second, it passes. Both tests are similar and I don't know what I am missing... Any ideas ?







      javascript node.js unit-testing mocha






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 15:18









      tedunioo24cmtedunioo24cm

      163




      163
























          2 Answers
          2






          active

          oldest

          votes


















          0














          If you are interacting with a live database, it's possible that the operation just takes longer than 2 seconds to complete. The test for a successful operation would take longer than the negative test if you have server-side validation happening before interacting with the database.



          You can extend the timeout in mocha using this.timeout(<some number in milliseconds>) :



          it('should create new note', (done) => {
          this.timeout(9000); // set it to something big to see if it fixes your issue
          const title = 'Test title';
          const text = 'Test text';
          const category = 'Test category';
          request(app)
          .post('/notes')
          .send({title, text, category})
          .expect(200)
          .expect(res => {
          expect(res.body.title).toBe(title);
          })
          .end((err, res) => {
          if (err)
          return done(err);

          Note.find({text: text}).then(notes => {
          expect(notes.length).toBe(1);
          expect(notes[0].title).toBe(title);
          done();
          }).catch(err => done(err));
          });
          });


          The only other thing I can think of is that your server side code is hanging somewhere and not sending a response (or that Notes.find() is not resolving or rejecting for some reason). Your test code looks fine to me.






          share|improve this answer
























          • It looks like a bug... Mocha always fais first test for me.

            – tedunioo24cm
            Nov 14 '18 at 15:50





















          0














          I'm not 100% sure that the expects outside of promise-land at the beginning will cause done to be called if they fail:



              .expect(200)
          .expect(res => {
          expect(res.body.title).toBe(title);
          })


          Maybe add some logging to see whether it ever gets to your .end handler?



          Also, what are you importing to get .expect methods on your request? We should look at its docs to see about done hooks.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303440%2fmocha-test-fail-ensure-don-is-called%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            If you are interacting with a live database, it's possible that the operation just takes longer than 2 seconds to complete. The test for a successful operation would take longer than the negative test if you have server-side validation happening before interacting with the database.



            You can extend the timeout in mocha using this.timeout(<some number in milliseconds>) :



            it('should create new note', (done) => {
            this.timeout(9000); // set it to something big to see if it fixes your issue
            const title = 'Test title';
            const text = 'Test text';
            const category = 'Test category';
            request(app)
            .post('/notes')
            .send({title, text, category})
            .expect(200)
            .expect(res => {
            expect(res.body.title).toBe(title);
            })
            .end((err, res) => {
            if (err)
            return done(err);

            Note.find({text: text}).then(notes => {
            expect(notes.length).toBe(1);
            expect(notes[0].title).toBe(title);
            done();
            }).catch(err => done(err));
            });
            });


            The only other thing I can think of is that your server side code is hanging somewhere and not sending a response (or that Notes.find() is not resolving or rejecting for some reason). Your test code looks fine to me.






            share|improve this answer
























            • It looks like a bug... Mocha always fais first test for me.

              – tedunioo24cm
              Nov 14 '18 at 15:50


















            0














            If you are interacting with a live database, it's possible that the operation just takes longer than 2 seconds to complete. The test for a successful operation would take longer than the negative test if you have server-side validation happening before interacting with the database.



            You can extend the timeout in mocha using this.timeout(<some number in milliseconds>) :



            it('should create new note', (done) => {
            this.timeout(9000); // set it to something big to see if it fixes your issue
            const title = 'Test title';
            const text = 'Test text';
            const category = 'Test category';
            request(app)
            .post('/notes')
            .send({title, text, category})
            .expect(200)
            .expect(res => {
            expect(res.body.title).toBe(title);
            })
            .end((err, res) => {
            if (err)
            return done(err);

            Note.find({text: text}).then(notes => {
            expect(notes.length).toBe(1);
            expect(notes[0].title).toBe(title);
            done();
            }).catch(err => done(err));
            });
            });


            The only other thing I can think of is that your server side code is hanging somewhere and not sending a response (or that Notes.find() is not resolving or rejecting for some reason). Your test code looks fine to me.






            share|improve this answer
























            • It looks like a bug... Mocha always fais first test for me.

              – tedunioo24cm
              Nov 14 '18 at 15:50
















            0












            0








            0







            If you are interacting with a live database, it's possible that the operation just takes longer than 2 seconds to complete. The test for a successful operation would take longer than the negative test if you have server-side validation happening before interacting with the database.



            You can extend the timeout in mocha using this.timeout(<some number in milliseconds>) :



            it('should create new note', (done) => {
            this.timeout(9000); // set it to something big to see if it fixes your issue
            const title = 'Test title';
            const text = 'Test text';
            const category = 'Test category';
            request(app)
            .post('/notes')
            .send({title, text, category})
            .expect(200)
            .expect(res => {
            expect(res.body.title).toBe(title);
            })
            .end((err, res) => {
            if (err)
            return done(err);

            Note.find({text: text}).then(notes => {
            expect(notes.length).toBe(1);
            expect(notes[0].title).toBe(title);
            done();
            }).catch(err => done(err));
            });
            });


            The only other thing I can think of is that your server side code is hanging somewhere and not sending a response (or that Notes.find() is not resolving or rejecting for some reason). Your test code looks fine to me.






            share|improve this answer













            If you are interacting with a live database, it's possible that the operation just takes longer than 2 seconds to complete. The test for a successful operation would take longer than the negative test if you have server-side validation happening before interacting with the database.



            You can extend the timeout in mocha using this.timeout(<some number in milliseconds>) :



            it('should create new note', (done) => {
            this.timeout(9000); // set it to something big to see if it fixes your issue
            const title = 'Test title';
            const text = 'Test text';
            const category = 'Test category';
            request(app)
            .post('/notes')
            .send({title, text, category})
            .expect(200)
            .expect(res => {
            expect(res.body.title).toBe(title);
            })
            .end((err, res) => {
            if (err)
            return done(err);

            Note.find({text: text}).then(notes => {
            expect(notes.length).toBe(1);
            expect(notes[0].title).toBe(title);
            done();
            }).catch(err => done(err));
            });
            });


            The only other thing I can think of is that your server side code is hanging somewhere and not sending a response (or that Notes.find() is not resolving or rejecting for some reason). Your test code looks fine to me.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 15:35









            dpopp07dpopp07

            29126




            29126













            • It looks like a bug... Mocha always fais first test for me.

              – tedunioo24cm
              Nov 14 '18 at 15:50





















            • It looks like a bug... Mocha always fais first test for me.

              – tedunioo24cm
              Nov 14 '18 at 15:50



















            It looks like a bug... Mocha always fais first test for me.

            – tedunioo24cm
            Nov 14 '18 at 15:50







            It looks like a bug... Mocha always fais first test for me.

            – tedunioo24cm
            Nov 14 '18 at 15:50















            0














            I'm not 100% sure that the expects outside of promise-land at the beginning will cause done to be called if they fail:



                .expect(200)
            .expect(res => {
            expect(res.body.title).toBe(title);
            })


            Maybe add some logging to see whether it ever gets to your .end handler?



            Also, what are you importing to get .expect methods on your request? We should look at its docs to see about done hooks.






            share|improve this answer




























              0














              I'm not 100% sure that the expects outside of promise-land at the beginning will cause done to be called if they fail:



                  .expect(200)
              .expect(res => {
              expect(res.body.title).toBe(title);
              })


              Maybe add some logging to see whether it ever gets to your .end handler?



              Also, what are you importing to get .expect methods on your request? We should look at its docs to see about done hooks.






              share|improve this answer


























                0












                0








                0







                I'm not 100% sure that the expects outside of promise-land at the beginning will cause done to be called if they fail:



                    .expect(200)
                .expect(res => {
                expect(res.body.title).toBe(title);
                })


                Maybe add some logging to see whether it ever gets to your .end handler?



                Also, what are you importing to get .expect methods on your request? We should look at its docs to see about done hooks.






                share|improve this answer













                I'm not 100% sure that the expects outside of promise-land at the beginning will cause done to be called if they fail:



                    .expect(200)
                .expect(res => {
                expect(res.body.title).toBe(title);
                })


                Maybe add some logging to see whether it ever gets to your .end handler?



                Also, what are you importing to get .expect methods on your request? We should look at its docs to see about done hooks.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 17:15









                Rob StarlingRob Starling

                2,93721737




                2,93721737






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303440%2fmocha-test-fail-ensure-don-is-called%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine