1
votes

I've the following scenario:

  • Five class;
  • Each one of five class has the same decorator @test;
  • Decorator @test get all functions of a class and store in static variable tests;

I want to run all methods at the same time that is stored in static variable tests. So, how can I do that?

obs: I'm studying typescript, so, I'm a new one with the language. And I'm trying to create for study purposes a framework that run tests similar to MSTest, nUnit, xUnit (c#).

class exemple:

@test()
class TestDataUnitsTest {

    public startup() {

    }

    public testOne(){

    }

    public testTwo(){

    }
}
1

1 Answers

0
votes

This should work:

const tests: { [name: string]: Function } = {};

function Test() {
     return function (target: Function) {
          const proto = target.prototype;
          const functions = Object.getOwnPropertyNames(proto)
               .filter(prop => prop !== 'constructor')
               .map(prop => proto[prop])
               .filter(prop => typeof prop === 'function');

          functions.forEach((fn: Function) => tests[`${target.name}.${fn.name}`] = fn)
     };
}

See Playground