From 729cd01cc196a1f4047ba589c8265d9d31b1c6d9 Mon Sep 17 00:00:00 2001 From: keenondrums Date: Sun, 14 Jul 2019 15:26:33 +0300 Subject: [PATCH] Bug Fixed: Own function properties are not copied --- package.json | 4 ++-- src/class-wrapper.service.spec.ts | 18 ++++++++++++++++++ src/class-wrapper.service.ts | 9 ++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index d55eca2..5fb7bb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "class-logger", - "version": "1.0.3", + "version": "1.1.0", "description": "Boilerplate-free decorator-based class logging", "keywords": [ "decorator", @@ -23,7 +23,7 @@ "type": "git", "url": "git+https://github.com/keenondrums/class-logger.git" }, - "author": "keenondrums (andrey.goncharov+it@protonmail.com)", + "author": "keenondrums (andrey@goncharov.page)", "license": "MIT", "bugs": { "url": "https://github.com/keenondrums/class-logger/issues" diff --git a/src/class-wrapper.service.spec.ts b/src/class-wrapper.service.spec.ts index 9f7cd61..8e2acd1 100644 --- a/src/class-wrapper.service.spec.ts +++ b/src/class-wrapper.service.spec.ts @@ -162,6 +162,24 @@ describe(ClassWrapperService.name, () => { expect(spyLogError).toBeCalledWith(spyFormatterEndMockRes) } + test('copies own properties of a target function', () => { + const testProp = 'test' + const testPropVal = Symbol() + + const fn = jest.fn() as any + fn[testProp] = testPropVal + + const config = ConfigService.config + const className = 'Test' + const propertyName = 'test' + const classInstance = {} + + const classWrapperService: any = new ClassWrapperService() + const fnWrapped = classWrapperService.wrapFunction(config, fn, className, propertyName, classInstance) + + expect(fnWrapped[testProp]).toBe(testPropVal) + }) + describe('synchronous target function', () => { test('logs success', async () => { const fnRes = Symbol() diff --git a/src/class-wrapper.service.ts b/src/class-wrapper.service.ts index 0b78647..9dcd4de 100644 --- a/src/class-wrapper.service.ts +++ b/src/class-wrapper.service.ts @@ -28,7 +28,7 @@ export class ClassWrapperService { const classWrapper = this // Use non-arrow function to allow dynamic context // tslint:disable-next-line only-arrow-functions - return function(this: any, ...args: any[]) { + const res = function(this: any, ...args: any[]) { const messageStart = config.formatter.start({ args, classInstance, @@ -76,6 +76,13 @@ export class ClassWrapperService { throw error } } as T + + // Functions are objects as well. They might have own properties. + for (const prop of Object.keys(fn) as Array) { + res[prop] = fn[prop] + } + + return res } protected isPromise(val: any) {