Skip to content
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
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
`);