Test a call made inside a node API with Jest Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I test a single file using Jest?How to run Jest tests sequentially?How do I deal with localStorage in jest tests?How do I run a single test using Jest?Jest not terminating after tests complete successfullyWhat is the difference between 'it' and 'test' in jest?Capturing/Logging AJAX Calls Made in Jest Tests by a Node ModuleSupertest / Jest / Node - Cannot test a simple routeJest testing user APILdapJS exop Request timeout

Is there a possibility to generate a list dynamically in Latex?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Are there existing rules/lore for MTG planeswalkers?

My admission is revoked after accepting the admission offer

What were wait-states, and why was it only an issue for PCs?

Is it appropriate to mention a relatable company blog post when you're asked about the company?

Israeli soda type drink

RIP Packet Format

"Working on a knee"

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

How to keep bees out of canned beverages?

Why did Europeans not widely domesticate foxes?

Why I cannot instantiate a class whose constructor is private in a friend class?

Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

Why did Israel vote against lifting the American embargo on Cuba?

What is /etc/mtab in Linux?

Suing a Police Officer Instead of the Police Department

Specify the range of GridLines

How to begin with a paragraph in latex

TV series episode where humans nuke aliens before decrypting their message that states they come in peace

Raising a bilingual kid. When should we introduce the majority language?

Marquee sign letters



Test a call made inside a node API with Jest



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I test a single file using Jest?How to run Jest tests sequentially?How do I deal with localStorage in jest tests?How do I run a single test using Jest?Jest not terminating after tests complete successfullyWhat is the difference between 'it' and 'test' in jest?Capturing/Logging AJAX Calls Made in Jest Tests by a Node ModuleSupertest / Jest / Node - Cannot test a simple routeJest testing user APILdapJS exop Request timeout



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice I need to check that another server is alive. The function is working but i'm stuck at testing it with jest.



Here is the code of my node micro-service:



app.get(/ping, (req, res) => 
axios.get('server.com/health')
.then(() =>
res.sendStatus(200);
)
.catch(() =>
res.sendStatus(503);
);
);


And here is the code of my test server :



import nock from 'nock';
import request from 'supertest';

describe('Liveness and Readiness', () => {
beforeEach(() =>
nock('server.com')
.get('/health')
.reply(200);
);

it('Microservice repond statut code 200 when requested on /ping ', () =>
microService.init();

return request(microService.start())
.get('/ping')
.expect(200);
);


I used nock to mock the server i need to check health from.



The error i get is :




expected 200 "OK", got 500 "Internal Server Error"




Without the code of healthcheck (see below) the test are passing.



app.get(/ping, (req, res) => 
res.sendStatus(200);
);


Even without nock (mean it should ping the real server) and i checked it's live it's still not working. It looks like it don't even try to check the health in the test even if it's mocked. I don't know what to check next to make my test work with this particular case.



Also tried moxios on did not succeed with it either.



I'll be grateful for any help from the community on that matter :)










share|improve this question






























    1















    i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice I need to check that another server is alive. The function is working but i'm stuck at testing it with jest.



    Here is the code of my node micro-service:



    app.get(/ping, (req, res) => 
    axios.get('server.com/health')
    .then(() =>
    res.sendStatus(200);
    )
    .catch(() =>
    res.sendStatus(503);
    );
    );


    And here is the code of my test server :



    import nock from 'nock';
    import request from 'supertest';

    describe('Liveness and Readiness', () => {
    beforeEach(() =>
    nock('server.com')
    .get('/health')
    .reply(200);
    );

    it('Microservice repond statut code 200 when requested on /ping ', () =>
    microService.init();

    return request(microService.start())
    .get('/ping')
    .expect(200);
    );


    I used nock to mock the server i need to check health from.



    The error i get is :




    expected 200 "OK", got 500 "Internal Server Error"




    Without the code of healthcheck (see below) the test are passing.



    app.get(/ping, (req, res) => 
    res.sendStatus(200);
    );


    Even without nock (mean it should ping the real server) and i checked it's live it's still not working. It looks like it don't even try to check the health in the test even if it's mocked. I don't know what to check next to make my test work with this particular case.



    Also tried moxios on did not succeed with it either.



    I'll be grateful for any help from the community on that matter :)










    share|improve this question


























      1












      1








      1








      i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice I need to check that another server is alive. The function is working but i'm stuck at testing it with jest.



      Here is the code of my node micro-service:



      app.get(/ping, (req, res) => 
      axios.get('server.com/health')
      .then(() =>
      res.sendStatus(200);
      )
      .catch(() =>
      res.sendStatus(503);
      );
      );


      And here is the code of my test server :



      import nock from 'nock';
      import request from 'supertest';

      describe('Liveness and Readiness', () => {
      beforeEach(() =>
      nock('server.com')
      .get('/health')
      .reply(200);
      );

      it('Microservice repond statut code 200 when requested on /ping ', () =>
      microService.init();

      return request(microService.start())
      .get('/ping')
      .expect(200);
      );


      I used nock to mock the server i need to check health from.



      The error i get is :




      expected 200 "OK", got 500 "Internal Server Error"




      Without the code of healthcheck (see below) the test are passing.



      app.get(/ping, (req, res) => 
      res.sendStatus(200);
      );


      Even without nock (mean it should ping the real server) and i checked it's live it's still not working. It looks like it don't even try to check the health in the test even if it's mocked. I don't know what to check next to make my test work with this particular case.



      Also tried moxios on did not succeed with it either.



      I'll be grateful for any help from the community on that matter :)










      share|improve this question
















      i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice I need to check that another server is alive. The function is working but i'm stuck at testing it with jest.



      Here is the code of my node micro-service:



      app.get(/ping, (req, res) => 
      axios.get('server.com/health')
      .then(() =>
      res.sendStatus(200);
      )
      .catch(() =>
      res.sendStatus(503);
      );
      );


      And here is the code of my test server :



      import nock from 'nock';
      import request from 'supertest';

      describe('Liveness and Readiness', () => {
      beforeEach(() =>
      nock('server.com')
      .get('/health')
      .reply(200);
      );

      it('Microservice repond statut code 200 when requested on /ping ', () =>
      microService.init();

      return request(microService.start())
      .get('/ping')
      .expect(200);
      );


      I used nock to mock the server i need to check health from.



      The error i get is :




      expected 200 "OK", got 500 "Internal Server Error"




      Without the code of healthcheck (see below) the test are passing.



      app.get(/ping, (req, res) => 
      res.sendStatus(200);
      );


      Even without nock (mean it should ping the real server) and i checked it's live it's still not working. It looks like it don't even try to check the health in the test even if it's mocked. I don't know what to check next to make my test work with this particular case.



      Also tried moxios on did not succeed with it either.



      I'll be grateful for any help from the community on that matter :)







      node.js jestjs supertest






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 14:12







      foufrix

















      asked Mar 22 at 14:58









      foufrixfoufrix

      4391922




      4391922






















          0






          active

          oldest

          votes












          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%2f55302401%2ftest-a-call-made-inside-a-node-api-with-jest%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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%2f55302401%2ftest-a-call-made-inside-a-node-api-with-jest%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript