Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken class parser #60

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading