Skip to content

Commit f162adb

Browse files
committed
fix: this in afterMethod & beforeMethod
1 parent f43b77d commit f162adb

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/patch-method.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function beforeMethod<
8686
methodName,
8787
function(superMethod, ...args) {
8888
fn.call(this, ...args)
89-
return superMethod(...args)
89+
return superMethod.apply(this, args)
9090
},
9191
fallback
9292
)
@@ -132,7 +132,7 @@ export function afterMethod<
132132
klass,
133133
methodName,
134134
function(superMethod, ...args) {
135-
const returnValue = superMethod(...args)
135+
const returnValue = superMethod.apply(this, args)
136136
fn.call(this, returnValue, ...args)
137137
return returnValue
138138
},

test/patch-method.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('patchMethod', () => {
88
it('works', () => {
99
class Foo {
1010
bar(value: string) {
11+
expect(this).toBeInstanceOf(Foo)
1112
return value
1213
}
1314
}
@@ -26,6 +27,7 @@ describe('patchMethod', () => {
2627
it('works with derived classes', () => {
2728
class Base {
2829
bar(value: string) {
30+
expect(this).toBeInstanceOf(Base)
2931
return value
3032
}
3133
}
@@ -46,6 +48,7 @@ describe('patchMethod', () => {
4648
it('works on base classes of derived classes', () => {
4749
class Base {
4850
bar(value: string) {
51+
expect(this).toBeInstanceOf(Base)
4952
return value
5053
}
5154
}
@@ -66,12 +69,14 @@ describe('patchMethod', () => {
6669
it('works with derived classes that override', () => {
6770
class Base {
6871
bar(value: string) {
72+
expect(this).toBeInstanceOf(Base)
6973
return `base -> ${value}`
7074
}
7175
}
7276

7377
class Derived extends Base {
7478
bar(value: string) {
79+
expect(this).toBeInstanceOf(Derived)
7580
return `derived -> ${super.bar(value)}`
7681
}
7782
}
@@ -125,6 +130,7 @@ describe('beforeMethod', () => {
125130
it('works', () => {
126131
class Foo {
127132
bar(value: string) {
133+
expect(this).toBeInstanceOf(Foo)
128134
return value
129135
}
130136
}
@@ -144,6 +150,7 @@ describe('afterMethod', () => {
144150
it('works', () => {
145151
class Foo {
146152
bar(value: string) {
153+
expect(this).toBeInstanceOf(Foo)
147154
return value
148155
}
149156
}

0 commit comments

Comments
 (0)