For the purpose of testing Network Encoding/Decoding Logic, I have implemented a pair of test cases in both Java and JavaScript. These tests utilize Data Providers which essentially consist of various Constants.
In my Java test case, I have a Data Provider that utilizes a static block to generate a random byte array for one of the classes being tested. Here is an example:
public static final byte[] BYTE_ARRAY_RANDOM = new byte[4 * 1024];
static {
new Random().nextBytes(BYTE_ARRAY_RANDOM);
}
public static final FileBody FILE_BODY_RANDOM = new FileBody(BYTE_ARRAY_RANDOM);
Now, when trying to replicate this in TypeScript, I encountered a compilation error due to incorrect implementation of the static block:
public static BYTE_ARRAY_RANDOM: Uint8Array = new Uint8Array(4 * 1024);
static {
BYTE_ARRAY_RANDOM.set(pseudoRandomBytes(bytes.length));
}
public static FILE_BODY_RANDOM: FileBody = new FileBody(TestDataProvider.BYTE_ARRAY_RANDOM);
Despite attempting several other methods, I have not been successful in finding a suitable solution so far.
Therefore, I am seeking guidance on how to achieve similar functionality in JavaScript. My aim is to create a Constant that provides a unique random byte array each time the tests are executed.
In simpler terms, how can I make this constant value static in TypeScript?
let bytes = new Uint8Array(4 * 1024);
bytes.set(pseudoRandomBytes(bytes.length));
let fileBody = new FileBody(bytes);