javascript - Unit testing Typescript decorators -
i have application built on typescript decorators convenience property assignments , wondering how can go writing unit tests them.
export function apiurl() { return function (target: any, key: string) { let _value = target[key]; function getter() { return _value; } function setter(newvalue) { _value = getapiurl(); } if (delete target[key]) { object.defineproperty(target, key, { get: getter, set: setter }); } }; }
in spec class have,
it("should return url string", ()=> { @apiurl(); let baseurl:string; expect(baseurl typeof string).tobe(true) })
since decorators functions suggest test them other function. , if need to, add 1 tests shows how use decorator class/member/...
here example such test like:
import test 'ava'; import { apiurl } './path'; const decorate = new apiurl(); test.before(t => { let obj = { someprop: 'foo' }; decorate(obj, 'someprop'); t.context.foo = obj; }); test('should return original value', t => { t.is(t.context.foo.someprop, 'foo'); });
Comments
Post a Comment