Forum Discussion

francisd84's avatar
francisd84
Contributor
3 years ago
Solved

Javascript Classes vs. //USEUNIT

I was a little bit surprised when I tried to create an object from a class located in another file within the same project when this file is imported using //USEUNIT and got a reference error

 

 

//File A
class MyClass
{
   constructor(x)
   {
      this.x = x;
   }
   GetX()
   {
      return this.x
   }
}

//File B
//USEUNIT A

function test()
{
  var myObj = new MyClass(5)
  //Or
  var myObj = new A.MyClass(5)
  //Will throw "Reference Error: MyClass is not defined"
}

 

 

I read couple of posts in this forum and, the fact of using "module.exports" and "require" seems (in my opinion) to defeat the purpose of //USEUNIT that I currently use to import global variables and functions from other units which works very well and makes code clean.

 

But unfortunately, classes from other files aren't imported using //USEUNIT.

 

Is it a known limitation of the implementation of Javascript through TestComplete?  A limitation of the engine? A bug? A misunderstanding of my own?

 

Thanks to help me see a little bit clearer on that.

 

 

 

 

 

 

  • Thanks Marsha_R

     

    I already read this page and didn't found answers that I wanted.

     

    BUT, I think I found a workaround that will limit the use of "require" in one file only.

     

    So if //USEUNIT only imports global variables and functions, so why not put the "require" statement within it's own file and then use //USEUNIT to import it?

     

    //File A
    
    var AClasses = require("A");
    
    class MyClass
    {
       constructor(x)
       {
          this.x = x;
       }
       GetX()
       {
          return this.x
       }
    }
    
    module.exports.MyClass = MyClass;
    
    
    //File B
    //USEUNIT A
    
    function test()
    {
      var myObj = new AClasses.MyClass(5)
      //Works!
    }

     

    IMO, this is a more cleaner way if we want to use the class in multiple units, we just need to use //USEUNIT instead of declaring another variable in each file.

4 Replies

    • francisd84's avatar
      francisd84
      Contributor

      Thanks Marsha_R

       

      I already read this page and didn't found answers that I wanted.

       

      BUT, I think I found a workaround that will limit the use of "require" in one file only.

       

      So if //USEUNIT only imports global variables and functions, so why not put the "require" statement within it's own file and then use //USEUNIT to import it?

       

      //File A
      
      var AClasses = require("A");
      
      class MyClass
      {
         constructor(x)
         {
            this.x = x;
         }
         GetX()
         {
            return this.x
         }
      }
      
      module.exports.MyClass = MyClass;
      
      
      //File B
      //USEUNIT A
      
      function test()
      {
        var myObj = new AClasses.MyClass(5)
        //Works!
      }

       

      IMO, this is a more cleaner way if we want to use the class in multiple units, we just need to use //USEUNIT instead of declaring another variable in each file.

      • TestQA1's avatar
        TestQA1
        Frequent Contributor

        Hi,

         

        Is there a way we can call functions from one project into another. 

        Project1 - > Unit1 -> Function1 returns something

        Can we use Function1 into Project 2. Both projects are in the same suite