最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unit testing Typescript decorators - Stack Overflow

programmeradmin3浏览0评论

I have an application built on typescript with decorators for some convenience property assignments and wondering how I can go about writing unit tests for 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 a spec class I have,

 it("should return url string", ()=> {
   @APIUrl();
   let baseURL:string;

   expect(baseURL typeOf string).toBe(true)
 })

I have an application built on typescript with decorators for some convenience property assignments and wondering how I can go about writing unit tests for 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 a spec class I have,

 it("should return url string", ()=> {
   @APIUrl();
   let baseURL:string;

   expect(baseURL typeOf string).toBe(true)
 })
Share Improve this question asked Aug 24, 2016 at 1:08 RjkRjk 1,4743 gold badges17 silver badges25 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

Since decorators are just functions I would suggest to just test them like any other function. And only if you really need to, add one tests that shows how to use the decorator with a class/member/...

Here is an example such a test could look like:

import test from 'ava';
import { APIUrl } from './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');
});

Another approach could be to setup some properties and/or methods that use your decorators and test their usage directly.

Note: decorators can only be used on class methods and members so you'd need to create a dummy class in your test.

Here's an example:

//Test Setup
class Test {

    @APIUrl()
    url: string;

    @AnotherDecorator()
    anotherFunction() {}

}


//Unit tests
describe('Decorator Tests', () => {
    it('should work', () => {
       const t = new Test();
       expect(t.url).toEqual("something");
       expect(t.anotherFunction()).toReturn("something else");
    });
}
发布评论

评论列表(0)

  1. 暂无评论