Javascript Classes vs. //USEUNIT
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Solved! Go to Solution.
- Labels:
-
Script Tests
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This may be what you are looking for. See the Referencing Other Script Units section. It discusses module.exports and USEUNIT.
https://support.smartbear.com/testcomplete/docs/scripting/specifics/javascript.html
Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
