diff --git a/src/utils/ObjectPropertyCodeRetriever.ts b/src/utils/ObjectPropertyCodeRetriever.ts index ad35c10..74fd6f4 100644 --- a/src/utils/ObjectPropertyCodeRetriever.ts +++ b/src/utils/ObjectPropertyCodeRetriever.ts @@ -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}`} `; diff --git a/test/mocking.types.spec.ts b/test/mocking.types.spec.ts index 7e224bb..5416d8d 100644 --- a/test/mocking.types.spec.ts +++ b/test/mocking.types.spec.ts @@ -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 { @@ -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 +`);