Skip to content

Commit

Permalink
Merge pull request #55 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 Jul 24, 2024
2 parents 5e01d44 + f07ad10 commit b86e329
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/utils/ObjectPropertyCodeRetriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class ObjectPropertyCodeRetriever {
];
} else if (typeof object[prop] === 'function') {
const fnStr = String(object[prop]);
const isMethod = fnStr.startsWith(prop) || fnStr.startsWith(`async ${prop}`);
const gx = new RegExp(`^(async)?\\s{0,}\\*?${prop}`);
const isMethod = gx.test(fnStr);
return `
${isMethod ? fnStr : `${prop}: ${fnStr}`}
`;
Expand Down
31 changes: 31 additions & 0 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ describe("mocking", () => {
expect(mocked).toBeDefined();
});
});

describe("mocking generator class", () => {
it("should mock", () => {
const mocked = mock(getGeneratorClass());
expect(mocked).toBeDefined();
});
});
});

abstract class SampleAbstractClass {
Expand Down Expand Up @@ -332,3 +339,27 @@ export class AsyncClass {
return 0;
}
}

// Generator functions in eval to prevent downcompiling generators to classic functions
// tslint:disable-next-line:no-eval
const getGeneratorClass = () => eval(`class GeneratorClass {
asyncValueFn = async function* hello() {
return 'value';
};
valueFn = function* hello() {
return 'value';
};
async *returnAsyncValue() {
return 0;
}
*returnValue() {
return 0;
}
}
GeneratorClass
`);

0 comments on commit b86e329

Please sign in to comment.