locked
sharing variables across mocha tests RRS feed

  • Question

  • The following code lets me share the variable across tests when mocha is run from the command line prompt, but it does not get shared (reset to 0) if run with mocha in visual studio. I am using a node.js project with the mocha module added.

    describe('some test', function(){
       
    var value = 0;
        it
    ('should pass a value', function(done){
            value
    = 5;
           
    done();
       
    });
        it
    ('and then double it', function(done){
            console
    .log(value * 2); // 10
           
    done();
       
    });
    });

    Friday, October 23, 2015 3:48 AM

All replies

  • Hi eljamaki,

    It seems that the real issue is not the unit test project issue, just to make this issue clearly, which kind of app did you really create? Is it related to the Azure web app or others?

    If it is related to the Azure web app, you know that it has his own supported forum here:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=windowsazurewebsitespreview

    So it possible, you could post this issue to the Azure forum for dedicated support.But if not, please feel free to tell me the real project type, I will help you find a more appropriate forum.

    Thanks,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 26, 2015 2:28 AM
  • The following code lets me share the variable "value" across tests when mocha is run from the command line prompt, but it does not get shared (it is reset to 0) if run with mocha in visual studio. I am using a node.js project with the mocha module added.

    describe('some test', function(){
       
    var value = 0;
        it
    ('should pass a value', function(done){
            value
    = 5;
           
    done();
       
    });
        it
    ('and then double it', function(done){
            console
    .log(value * 2); // 10
           
    done();
       
    });
    });

    Monday, October 26, 2015 6:57 PM
  • You need to set the value in a "before" function. This ensures that it gets set appropriately before the tests.

    Checkout their docs for more info: https://mochajs.org/#hooks


    Monday, October 26, 2015 7:42 PM
  • That will not work. The code is trivial, so was just meant to illustrate the problem. And besides, it does not work that way with Mocha run from the command line!.

    Let me illustrate what I am really doing. I am implementing token based authentication, so imagine trying to validate that a sign-in works and then the token I get back can be used in various tests that follow. The value variable above would only be filled in in a test, not in a before, because I don't always want the token set in a before, because I also need to test things like not having a token.

    Tuesday, October 27, 2015 1:00 AM
  • Hi eljamaki,

    Maybe I misunderstood this issue, just to make this issue clearly, do you mean that it is related to the unit test like this document? It is related to the JavaScript Mocha UnitTest, am I right?

    https://github.com/Microsoft/nodejstools/wiki/Test-Explorer

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites;

    Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 29, 2015 5:54 AM