Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by...











up vote
2
down vote

favorite












I'm starting to test my code with Jest, and I can't make a seemingly simple test to pass. I am simply trying to check if what I receive from a Maogoose database request is an object.



The function fetchPosts() is working because I hooked it up with a React frontend and it is displaying the data correctly.



This is my function fetchPosts():



module.exports = {
fetchPosts() {
return new Promise((resolve, reject) => {
Posts.find({}).then(posts => {
if (posts) {
resolve(posts)
} else {
reject()
}
})
})
}
}


And my test:



it('should get a list of posts', function() {
return posts.fetchPosts().then(result => {
expect(typeof result).toBe('object')
})
})


This makes the test fail, and Jest says



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




QUESTION: How can I make this test pass?











share|improve this question




























    up vote
    2
    down vote

    favorite












    I'm starting to test my code with Jest, and I can't make a seemingly simple test to pass. I am simply trying to check if what I receive from a Maogoose database request is an object.



    The function fetchPosts() is working because I hooked it up with a React frontend and it is displaying the data correctly.



    This is my function fetchPosts():



    module.exports = {
    fetchPosts() {
    return new Promise((resolve, reject) => {
    Posts.find({}).then(posts => {
    if (posts) {
    resolve(posts)
    } else {
    reject()
    }
    })
    })
    }
    }


    And my test:



    it('should get a list of posts', function() {
    return posts.fetchPosts().then(result => {
    expect(typeof result).toBe('object')
    })
    })


    This makes the test fail, and Jest says



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




    QUESTION: How can I make this test pass?











    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm starting to test my code with Jest, and I can't make a seemingly simple test to pass. I am simply trying to check if what I receive from a Maogoose database request is an object.



      The function fetchPosts() is working because I hooked it up with a React frontend and it is displaying the data correctly.



      This is my function fetchPosts():



      module.exports = {
      fetchPosts() {
      return new Promise((resolve, reject) => {
      Posts.find({}).then(posts => {
      if (posts) {
      resolve(posts)
      } else {
      reject()
      }
      })
      })
      }
      }


      And my test:



      it('should get a list of posts', function() {
      return posts.fetchPosts().then(result => {
      expect(typeof result).toBe('object')
      })
      })


      This makes the test fail, and Jest says



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




      QUESTION: How can I make this test pass?











      share|improve this question















      I'm starting to test my code with Jest, and I can't make a seemingly simple test to pass. I am simply trying to check if what I receive from a Maogoose database request is an object.



      The function fetchPosts() is working because I hooked it up with a React frontend and it is displaying the data correctly.



      This is my function fetchPosts():



      module.exports = {
      fetchPosts() {
      return new Promise((resolve, reject) => {
      Posts.find({}).then(posts => {
      if (posts) {
      resolve(posts)
      } else {
      reject()
      }
      })
      })
      }
      }


      And my test:



      it('should get a list of posts', function() {
      return posts.fetchPosts().then(result => {
      expect(typeof result).toBe('object')
      })
      })


      This makes the test fail, and Jest says



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




      QUESTION: How can I make this test pass?








      javascript node.js reactjs jestjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 17:15









      Patrick Hund

      6,80252147




      6,80252147










      asked Nov 20 at 16:46









      mokiliii Lo

      4918




      4918
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can expect asynchronous results using resolves, as shown in the Jest documentation.



          In your case:



          it('should get a list of posts', function() {
          const result = posts.fetchPosts();
          expect(result).resolves.toEqual(expect.any(Object));
          })


          …although I have a suspicion your list of posts is actually an array, so you probably want this:



          it('should get a list of posts', function() {
          const result = posts.fetchPosts();
          expect(result).resolves.toEqual(expect.any(Array));
          })


          Another tip: You don't need to wrap the body of your fetchPost in an additional promise, you can simply return the promise you get from Posts.find and add a then to it, like this:



          module.exports = {
          fetchPosts() {
          return Posts.find({}).then(posts => {
          if (posts) {
          return posts;
          }
          throw new Error('no posts'); // this will cause a promise rejection
          })
          }
          }





          share|improve this answer




























            up vote
            0
            down vote













            Also if you are simply looking to increase the timeout, then you can do that by setting






             jest.setTimeout(10000);





            You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.






            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%2f53397695%2fjest-testing-with-node-timeout-async-callback-was-not-invoked-within-the-500%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








              up vote
              1
              down vote



              accepted










              You can expect asynchronous results using resolves, as shown in the Jest documentation.



              In your case:



              it('should get a list of posts', function() {
              const result = posts.fetchPosts();
              expect(result).resolves.toEqual(expect.any(Object));
              })


              …although I have a suspicion your list of posts is actually an array, so you probably want this:



              it('should get a list of posts', function() {
              const result = posts.fetchPosts();
              expect(result).resolves.toEqual(expect.any(Array));
              })


              Another tip: You don't need to wrap the body of your fetchPost in an additional promise, you can simply return the promise you get from Posts.find and add a then to it, like this:



              module.exports = {
              fetchPosts() {
              return Posts.find({}).then(posts => {
              if (posts) {
              return posts;
              }
              throw new Error('no posts'); // this will cause a promise rejection
              })
              }
              }





              share|improve this answer

























                up vote
                1
                down vote



                accepted










                You can expect asynchronous results using resolves, as shown in the Jest documentation.



                In your case:



                it('should get a list of posts', function() {
                const result = posts.fetchPosts();
                expect(result).resolves.toEqual(expect.any(Object));
                })


                …although I have a suspicion your list of posts is actually an array, so you probably want this:



                it('should get a list of posts', function() {
                const result = posts.fetchPosts();
                expect(result).resolves.toEqual(expect.any(Array));
                })


                Another tip: You don't need to wrap the body of your fetchPost in an additional promise, you can simply return the promise you get from Posts.find and add a then to it, like this:



                module.exports = {
                fetchPosts() {
                return Posts.find({}).then(posts => {
                if (posts) {
                return posts;
                }
                throw new Error('no posts'); // this will cause a promise rejection
                })
                }
                }





                share|improve this answer























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  You can expect asynchronous results using resolves, as shown in the Jest documentation.



                  In your case:



                  it('should get a list of posts', function() {
                  const result = posts.fetchPosts();
                  expect(result).resolves.toEqual(expect.any(Object));
                  })


                  …although I have a suspicion your list of posts is actually an array, so you probably want this:



                  it('should get a list of posts', function() {
                  const result = posts.fetchPosts();
                  expect(result).resolves.toEqual(expect.any(Array));
                  })


                  Another tip: You don't need to wrap the body of your fetchPost in an additional promise, you can simply return the promise you get from Posts.find and add a then to it, like this:



                  module.exports = {
                  fetchPosts() {
                  return Posts.find({}).then(posts => {
                  if (posts) {
                  return posts;
                  }
                  throw new Error('no posts'); // this will cause a promise rejection
                  })
                  }
                  }





                  share|improve this answer












                  You can expect asynchronous results using resolves, as shown in the Jest documentation.



                  In your case:



                  it('should get a list of posts', function() {
                  const result = posts.fetchPosts();
                  expect(result).resolves.toEqual(expect.any(Object));
                  })


                  …although I have a suspicion your list of posts is actually an array, so you probably want this:



                  it('should get a list of posts', function() {
                  const result = posts.fetchPosts();
                  expect(result).resolves.toEqual(expect.any(Array));
                  })


                  Another tip: You don't need to wrap the body of your fetchPost in an additional promise, you can simply return the promise you get from Posts.find and add a then to it, like this:



                  module.exports = {
                  fetchPosts() {
                  return Posts.find({}).then(posts => {
                  if (posts) {
                  return posts;
                  }
                  throw new Error('no posts'); // this will cause a promise rejection
                  })
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 17:12









                  Patrick Hund

                  6,80252147




                  6,80252147
























                      up vote
                      0
                      down vote













                      Also if you are simply looking to increase the timeout, then you can do that by setting






                       jest.setTimeout(10000);





                      You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Also if you are simply looking to increase the timeout, then you can do that by setting






                         jest.setTimeout(10000);





                        You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Also if you are simply looking to increase the timeout, then you can do that by setting






                           jest.setTimeout(10000);





                          You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.






                          share|improve this answer












                          Also if you are simply looking to increase the timeout, then you can do that by setting






                           jest.setTimeout(10000);





                          You can use this statement in beforeEach if you want to change the timeout for all your tests in that describe block or in the test/it/spec block if you want it for a single test.






                           jest.setTimeout(10000);





                           jest.setTimeout(10000);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 19:16









                          shmit

                          44239




                          44239






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53397695%2fjest-testing-with-node-timeout-async-callback-was-not-invoked-within-the-500%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

                              Feedback on college project

                              Futebolista

                              Albești (Vaslui)