Our company has developed its own test framework for our software because we found it difficult to use an open-source framework within the context of our specific development needs.
Recently, I took on the task of creating Typescript Definition Files to enhance code completion in VS Code. However, I have encountered a challenge with the current setup:
In our testing environment, we utilize a class called UnitTest to define unit tests. This class includes functions for assertions and utilizes a util-namespace. A key feature is the ability to pass a function as the unit test itself, with the this-object pointing to the Unit Test class instance. The passed function can then access and use the functions provided by the unit test. Internally, the passed function is invoked using "unitTest.call(this)" to set the correct this object.
class UnitTest {
constructor(unitTest: (this: UnitTest) => void);
.
.
.
util = Util; // Refers to the Util-Class
The Util-class contains general functions essential for writing tests:
class Util {
static createFile(filePath: string): File;
.
.
.
}
While declaring a unit test following the standard format works seamlessly:
var unitTest = new UnitTest(function() {
this.util.createFile("..."); // Code completion available
});
However, the issue arises when extending the util-class for specific project needs. By introducing custom utility scripts following a naming convention, the test framework dynamically loads these scripts and provides additional functions in the util namespace, such as "test.util.myProject.js". Although utilizing these custom functions in unit tests is straightforward, enhancing the typescript definition file to cover these custom utilities presents a challenge.
module.exports = {
myCustomUtilFunction = function () { ... }
};
Trying to extend the Util class in the TypeScript definition file does not reflect in code completion within VS Code:
class Util {
static myCustomUtilFunction(): void;
}
I am seeking advice or solutions on how to address this issue effectively.
To provide a clearer overview of the setup:
testframework.d.tsclass UnitTest {
constructor(unitTest: (this: UnitTest) =>
util = Util;
}
class Util {
static createFile(filePath: string): File;
}
myExtendedUtilNamespace.d.ts
class Util {
static myCustomUtilFunction(): void;
}
unitTests.js
var unitTest = new UnitTest(function() {
this.util.createFile("...");
this.util.myCustomUtilFunction();
});