Forum Discussion

cmpenn's avatar
cmpenn
Contributor
2 years ago
Solved

A const value does not work in other files

Here's an example: File 1:   const MAX_SEARCH_DEPTH = 20;       File 2:   //USEUNIT File 1 function constantsTest() { Log.Message(MAX_SEARCH_DEPTH); //Should return 20, instead returns...
  • tristaanogre's avatar
    tristaanogre
    2 years ago

    I think it has to do with the value being only locally scoped to the unit.  While within the unit where the constant is declared it's treated as a constant, as soon as you pull that unit into a new unit, it becomes a variable.  Example:

    //USEUNIT Constant
    
    function bar(){
        Log.Message(Constant.TEST_CONST)
        Constant.TEST_CONST = 'yada'
        Log.Message(Constant.TEST_CONST)
    }

     

    In the above example, the first message call returns blank, no matter what I have it set to in the unit "Constant".  But then if I assign it the word "yada", then it returns "yada".

     

    What it looks like is that a constant in a JavaScript unit is just a property of that unit that has no "set" method associated to it.  When you export the unit using modules.export (or //USEUNIT) you're not using the unit as is, you're exporting the unit as an in memory  instance of the unit object which is set up to be more generic.

     

    What I've found, generally, is that while JavaScript in TestComplete works pretty closely to using JavaScript in an actual web application, there are some differences that require work arounds. My best bet for you, for simplicity sake, is make sure you're consistent in your naming convention for constants (variables = camelCase, constants = ALL_UPPER) and then just remaining consistent in how you use those items.  In that case, the "exports" and "require" methodology will work best for you.