Skip to content

Commit

Permalink
Bug Fixed: Own function properties are not copied
Browse files Browse the repository at this point in the history
  • Loading branch information
keenondrums committed Jul 14, 2019
1 parent 83cd8bb commit 729cd01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "class-logger",
"version": "1.0.3",
"version": "1.1.0",
"description": "Boilerplate-free decorator-based class logging",
"keywords": [
"decorator",
Expand All @@ -23,7 +23,7 @@
"type": "git",
"url": "git+https://github.com/keenondrums/class-logger.git"
},
"author": "keenondrums (andrey[email protected])",
"author": "keenondrums (andrey@goncharov.page)",
"license": "MIT",
"bugs": {
"url": "https://github.com/keenondrums/class-logger/issues"
Expand Down
18 changes: 18 additions & 0 deletions src/class-wrapper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion src/class-wrapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<keyof T>) {
res[prop] = fn[prop]
}

return res
}

protected isPromise(val: any) {
Expand Down

0 comments on commit 729cd01

Please sign in to comment.