I have been tasked with implementing a class decorator that adds an "identify" class method, which returns a class name with the information passed in the decorator.
For example :
@identifier('example')
class Test {}
const test = new Test();
console.log(test['identify']()); // Test-example
When I console log, I get the expected result showing the class name and the string in brackets. However, the issue arises when running unit tests:
describe('identifier', () => {
it('should return Test-example from identify', () => {
@identifier('example')
class Test {}
const test = new Test();
assert.strictEqual(test['identify'](), 'Test-example');
});
it('should return ClassA-prototype from identify', () => {
@identifier('prototype')
class ClassA {}
const test = new ClassA();
assert.strictEqual(test['identify'](), 'ClassA-prototype');
})
});
Decorator implementation :
Method 1 :
function identifier(...args: any): ClassDecorator {
return function <TFunction extends Function>(
target: TFunction
): TFunction | any {
return target.name + "-" + args;
};
}
Method 2:
function identifier(passedInformation: string): any {
return function (target) {
return target.name + "-" + passedInformation;
};
}
Both functions seem correct, but they are both causing errors in junit.xml:
<testcase name="identifier should return Test-example from identify" time="0.0000" classname="should return Test-example from identify">
<failure message="" type="TypeError"><![CDATA[TypeError:
at DecorateConstructor (node_modules\reflect-metadata\Reflect.js:544:31)
at Object.decorate (node_modules\reflect-metadata\Reflect.js:130:24)
at __decorate (test\index.ts:4:92)
at C:\Users\artio\Desktop\6 Decorators\decorators\test\index.ts:81:20
at Context.<anonymous> (test\index.ts:85:10)
at processImmediate (internal/timers.js:439:21)]]></failure>
</testcase>
<testcase name="identifier should return ClassA-prototype from identify" time="0.0000" classname="should return ClassA-prototype from identify">
<failure message="" type="TypeError"><![CDATA[TypeError:
at DecorateConstructor (node_modules\reflect-metadata\Reflect.js:544:31)
at Object.decorate (node_modules\reflect-metadata\Reflect.js:130:24)
at __decorate (test\index.ts:4:92)
at C:\Users\artio\Desktop\6 Decorators\decorators\test\index.ts:93:22
at Context.<anonymous> (test\index.ts:97:10)
at processImmediate (internal/timers.js:439:21)]]></failure>
</testcase>
Can anyone identify the problem and suggest a solution? I am unable to complete the task due to these errors... It's worth mentioning that I cannot modify the unit tests, only the functions.