Sinon stub doesn't seem to work when object destruction is used2019 Community Moderator ElectionSinon stub function used with destructuringnode.js stubbing AWS S3 method in request spec with sinonHow to stub a nodejs “required” constructor using sinon?Stub a closure function using sinon for redux actionsSinon Stub standalone utility function in a moduleSinon Stub/Spy on local functions in unit testingSinon stub not called after promise returnedsinon: stub a function that is not attached to an objectsinon and mocha - stubbing private dependenciesStubbing express middleware functions with sinon isHow to stub mongoose methods with multiple arguments in Sinon?

Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?

Why do newer 737s use two different styles of split winglets?

How do you talk to someone whose loved one is dying?

PTIJ: Who should I vote for? (21st Knesset Edition)

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Are Roman Catholic priests ever addressed as pastor

Is it good practice to use Linear Least-Squares with SMA?

Welcoming 2019 Pi day: How to draw the letter π?

What is a ^ b and (a & b) << 1?

Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?

Why do tuner card drivers fail to build after kernel update to 4.4.0-143-generic?

Why is a white electrical wire connected to 2 black wires?

Why do passenger jet manufacturers design their planes with stall prevention systems?

Happy pi day, everyone!

Why does overlay work only on the first tcolorbox?

This word with a lot of past tenses

"of which" is correct here?

Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)

How to pronounce "I ♥ Huckabees"?

What is "focus distance lower/upper" and how is it different from depth of field?

Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?

Aluminum electrolytic or ceramic capacitors for linear regulator input and output?

World War I as a war of liberals against authoritarians?

Why Choose Less Effective Armour Types?



Sinon stub doesn't seem to work when object destruction is used



2019 Community Moderator ElectionSinon stub function used with destructuringnode.js stubbing AWS S3 method in request spec with sinonHow to stub a nodejs “required” constructor using sinon?Stub a closure function using sinon for redux actionsSinon Stub standalone utility function in a moduleSinon Stub/Spy on local functions in unit testingSinon stub not called after promise returnedsinon: stub a function that is not attached to an objectsinon and mocha - stubbing private dependenciesStubbing express middleware functions with sinon isHow to stub mongoose methods with multiple arguments in Sinon?










0















Let's say you have a method called myMethod in the module myModule which is looking like this:



function myMethod() 
return 5;

module.exports.myMethod = myMethod;


Now if I want to stub this method to return 2 instead of 5 with Sinon I would write



const myModule = require('path/myModule');
sinon.stub(myModule, 'myMethod').returns(2);


Now in the place where you actually call the method you happen to import the method like this with object destruction



const myMethod = require('path/myModule');
console.log(myMethod()); // Will print 5


If you do that, myMethod is actually not stubbed and won't return 2 but 5 instead.



If you instead require again the module and use the function from the required module it will work



const myModule= require('path/myModule');
console.log(myModule.myMethod()); // Will print 2


Is there anyone who has a solution to this other than just changing the way I import my functions?










share|improve this question


























    0















    Let's say you have a method called myMethod in the module myModule which is looking like this:



    function myMethod() 
    return 5;

    module.exports.myMethod = myMethod;


    Now if I want to stub this method to return 2 instead of 5 with Sinon I would write



    const myModule = require('path/myModule');
    sinon.stub(myModule, 'myMethod').returns(2);


    Now in the place where you actually call the method you happen to import the method like this with object destruction



    const myMethod = require('path/myModule');
    console.log(myMethod()); // Will print 5


    If you do that, myMethod is actually not stubbed and won't return 2 but 5 instead.



    If you instead require again the module and use the function from the required module it will work



    const myModule= require('path/myModule');
    console.log(myModule.myMethod()); // Will print 2


    Is there anyone who has a solution to this other than just changing the way I import my functions?










    share|improve this question
























      0












      0








      0








      Let's say you have a method called myMethod in the module myModule which is looking like this:



      function myMethod() 
      return 5;

      module.exports.myMethod = myMethod;


      Now if I want to stub this method to return 2 instead of 5 with Sinon I would write



      const myModule = require('path/myModule');
      sinon.stub(myModule, 'myMethod').returns(2);


      Now in the place where you actually call the method you happen to import the method like this with object destruction



      const myMethod = require('path/myModule');
      console.log(myMethod()); // Will print 5


      If you do that, myMethod is actually not stubbed and won't return 2 but 5 instead.



      If you instead require again the module and use the function from the required module it will work



      const myModule= require('path/myModule');
      console.log(myModule.myMethod()); // Will print 2


      Is there anyone who has a solution to this other than just changing the way I import my functions?










      share|improve this question














      Let's say you have a method called myMethod in the module myModule which is looking like this:



      function myMethod() 
      return 5;

      module.exports.myMethod = myMethod;


      Now if I want to stub this method to return 2 instead of 5 with Sinon I would write



      const myModule = require('path/myModule');
      sinon.stub(myModule, 'myMethod').returns(2);


      Now in the place where you actually call the method you happen to import the method like this with object destruction



      const myMethod = require('path/myModule');
      console.log(myMethod()); // Will print 5


      If you do that, myMethod is actually not stubbed and won't return 2 but 5 instead.



      If you instead require again the module and use the function from the required module it will work



      const myModule= require('path/myModule');
      console.log(myModule.myMethod()); // Will print 2


      Is there anyone who has a solution to this other than just changing the way I import my functions?







      node.js sinon






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 6 at 20:52









      Alex HallerAlex Haller

      437




      437






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The stubbed myMethod will have a different reference than the original method. In the destructuring example, you are setting the reference before it can be stubbed.



          // cannot be stubbed
          const myMethod = require('path/myModule');

          // can be stubbed
          const myModule = require('path/myModule');
          myModule.myMethod();


          Check out this similar question for more details






          share|improve this answer























          • So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

            – Alex Haller
            Mar 6 at 20:59











          • That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

            – jakemingolla
            Mar 6 at 21:11











          • makes sense, Thanks a lot!

            – Alex Haller
            Mar 6 at 21:36


















          0
















          This is possible.



          Just note that as soon as this runs:



          const myMethod = require('./lib');


          ...it will remember whatever myMethod was at that moment.



          So, you just have to make sure you set up the stub before that code runs.




          So for this lib.js:



          function myMethod() 
          return 5;

          module.exports.myMethod = myMethod;


          and this code.js:



          const myMethod = require('./lib');
          console.log(myMethod());


          you would just need to do this:



          const sinon = require('sinon');

          const myModule = require('./lib');
          sinon.stub(myModule, 'myMethod').returns(2); // set up the stub FIRST...
          require('./code'); // ...THEN require the code (prints "2")





          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%2f55031937%2fsinon-stub-doesnt-seem-to-work-when-object-destruction-is-used%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









            1














            The stubbed myMethod will have a different reference than the original method. In the destructuring example, you are setting the reference before it can be stubbed.



            // cannot be stubbed
            const myMethod = require('path/myModule');

            // can be stubbed
            const myModule = require('path/myModule');
            myModule.myMethod();


            Check out this similar question for more details






            share|improve this answer























            • So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

              – Alex Haller
              Mar 6 at 20:59











            • That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

              – jakemingolla
              Mar 6 at 21:11











            • makes sense, Thanks a lot!

              – Alex Haller
              Mar 6 at 21:36















            1














            The stubbed myMethod will have a different reference than the original method. In the destructuring example, you are setting the reference before it can be stubbed.



            // cannot be stubbed
            const myMethod = require('path/myModule');

            // can be stubbed
            const myModule = require('path/myModule');
            myModule.myMethod();


            Check out this similar question for more details






            share|improve this answer























            • So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

              – Alex Haller
              Mar 6 at 20:59











            • That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

              – jakemingolla
              Mar 6 at 21:11











            • makes sense, Thanks a lot!

              – Alex Haller
              Mar 6 at 21:36













            1












            1








            1







            The stubbed myMethod will have a different reference than the original method. In the destructuring example, you are setting the reference before it can be stubbed.



            // cannot be stubbed
            const myMethod = require('path/myModule');

            // can be stubbed
            const myModule = require('path/myModule');
            myModule.myMethod();


            Check out this similar question for more details






            share|improve this answer













            The stubbed myMethod will have a different reference than the original method. In the destructuring example, you are setting the reference before it can be stubbed.



            // cannot be stubbed
            const myMethod = require('path/myModule');

            // can be stubbed
            const myModule = require('path/myModule');
            myModule.myMethod();


            Check out this similar question for more details







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 6 at 20:56









            jakemingollajakemingolla

            48239




            48239












            • So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

              – Alex Haller
              Mar 6 at 20:59











            • That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

              – jakemingolla
              Mar 6 at 21:11











            • makes sense, Thanks a lot!

              – Alex Haller
              Mar 6 at 21:36

















            • So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

              – Alex Haller
              Mar 6 at 20:59











            • That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

              – jakemingolla
              Mar 6 at 21:11











            • makes sense, Thanks a lot!

              – Alex Haller
              Mar 6 at 21:36
















            So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

            – Alex Haller
            Mar 6 at 20:59





            So, in this case, it seems like there is no other way but refactoring my imports because of the different references?

            – Alex Haller
            Mar 6 at 20:59













            That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

            – jakemingolla
            Mar 6 at 21:11





            That's how i'm interpreting it, yeah - you would need to place the destructuring closer to the invocation of the function rather than at module initialization in order for the reference to the stub to appear after initialization

            – jakemingolla
            Mar 6 at 21:11













            makes sense, Thanks a lot!

            – Alex Haller
            Mar 6 at 21:36





            makes sense, Thanks a lot!

            – Alex Haller
            Mar 6 at 21:36













            0
















            This is possible.



            Just note that as soon as this runs:



            const myMethod = require('./lib');


            ...it will remember whatever myMethod was at that moment.



            So, you just have to make sure you set up the stub before that code runs.




            So for this lib.js:



            function myMethod() 
            return 5;

            module.exports.myMethod = myMethod;


            and this code.js:



            const myMethod = require('./lib');
            console.log(myMethod());


            you would just need to do this:



            const sinon = require('sinon');

            const myModule = require('./lib');
            sinon.stub(myModule, 'myMethod').returns(2); // set up the stub FIRST...
            require('./code'); // ...THEN require the code (prints "2")





            share|improve this answer



























              0
















              This is possible.



              Just note that as soon as this runs:



              const myMethod = require('./lib');


              ...it will remember whatever myMethod was at that moment.



              So, you just have to make sure you set up the stub before that code runs.




              So for this lib.js:



              function myMethod() 
              return 5;

              module.exports.myMethod = myMethod;


              and this code.js:



              const myMethod = require('./lib');
              console.log(myMethod());


              you would just need to do this:



              const sinon = require('sinon');

              const myModule = require('./lib');
              sinon.stub(myModule, 'myMethod').returns(2); // set up the stub FIRST...
              require('./code'); // ...THEN require the code (prints "2")





              share|improve this answer

























                0












                0








                0









                This is possible.



                Just note that as soon as this runs:



                const myMethod = require('./lib');


                ...it will remember whatever myMethod was at that moment.



                So, you just have to make sure you set up the stub before that code runs.




                So for this lib.js:



                function myMethod() 
                return 5;

                module.exports.myMethod = myMethod;


                and this code.js:



                const myMethod = require('./lib');
                console.log(myMethod());


                you would just need to do this:



                const sinon = require('sinon');

                const myModule = require('./lib');
                sinon.stub(myModule, 'myMethod').returns(2); // set up the stub FIRST...
                require('./code'); // ...THEN require the code (prints "2")





                share|improve this answer















                This is possible.



                Just note that as soon as this runs:



                const myMethod = require('./lib');


                ...it will remember whatever myMethod was at that moment.



                So, you just have to make sure you set up the stub before that code runs.




                So for this lib.js:



                function myMethod() 
                return 5;

                module.exports.myMethod = myMethod;


                and this code.js:



                const myMethod = require('./lib');
                console.log(myMethod());


                you would just need to do this:



                const sinon = require('sinon');

                const myModule = require('./lib');
                sinon.stub(myModule, 'myMethod').returns(2); // set up the stub FIRST...
                require('./code'); // ...THEN require the code (prints "2")






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 7 at 4:43









                brian-lives-outdoorsbrian-lives-outdoors

                8,3271725




                8,3271725



























                    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%2f55031937%2fsinon-stub-doesnt-seem-to-work-when-object-destruction-is-used%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

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко