Skip to content

Commit

Permalink
Merge pull request #60 from roypeled/fix-broken-class-parser
Browse files Browse the repository at this point in the history
Fix broken class parser
  • Loading branch information
roypeled authored Aug 20, 2024
2 parents 9ad1e29 + 4370c84 commit 6f88933
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typestrong/ts-mockito",
"version": "2.7.9",
"version": "2.7.11",
"description": "Mocking library for TypeScript",
"main": "lib/ts-mockito.js",
"typings": "lib/ts-mockito",
Expand Down
3 changes: 2 additions & 1 deletion src/utils/ObjectPropertyCodeRetriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export class ObjectPropertyCodeRetriever {
descriptor?.set ? descriptor?.set.toString() : '',
];
} else if (typeof object[prop] === 'function') {
const fnStr = String(object[prop]);
let fnStr = String(object[prop]);
fnStr = fnStr.replace(/\[native code]/, '');
const gx = new RegExp(`^(async)?\\s{0,}\\*?${prop}`);
const isMethod = gx.test(fnStr);
return `
Expand Down
19 changes: 19 additions & 0 deletions test/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,25 @@ cases.forEach(testData => {
}
});
});

describe("decorator + Proxy", () => {
function decorator<T extends object>(target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) {
const originalMethod = descriptor.value;
if (originalMethod !== undefined) {
descriptor.value = new Proxy(originalMethod, {})
}
}

class TestClass {
@decorator
foo() {}
}

it("should mock", () => {
const mocked = mock(TestClass);
expect(mocked).toBeDefined();
});
});
});
});

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"outDir": "./lib",
"module": "commonjs",
"moduleResolution": "node",
Expand Down

0 comments on commit 6f88933

Please sign in to comment.