-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat: add auto-mock support for async generators #11080
Conversation
eb078bf
to
665659b
Compare
Cool! If that is invoked, is it a generator? I believe we have some inference for that |
I'm not sure I understand the question fully but here's an example of the behavior I would expect with comments about the current behavior. I would be more than happy to add these tests to the suite as well but wasn't sure where the appropriate place was so I just tried to match what was already included in #5983
export function* generator(): IterableIterator<number> {
yield 1;
yield 2;
yield 3;
}
export async function* asyncGenerator(): AsyncIterableIterator<number> {
yield 1;
yield 2;
yield 3;
}
import { mocked } from "ts-jest/utils";
import * as sut from "../index";
function* arrayToIterableIterator<T>(arr: T[]): IterableIterator<T> {
for (const item of arr) {
yield item;
}
}
async function* arrayToAsyncIterableIterator<T>(arr: T[]): AsyncIterableIterator<T> {
for (const item of arr) {
yield item;
}
}
const mockedValue = [4, 5, 6];
test("generator mocks correctly", () => {
mocked(sut.generator).mockReturnValue(arrayToIterableIterator(mockedValue));
const arr = Array.from(sut.generator());
expect(arr).toEqual(mockedValue); // this passes 👍
});
test("async generator mocks correctly", async () => {
// this currently throws: TypeError: Cannot read property 'mockReturnValue' of undefined
mocked(sut.asyncGenerator).mockReturnValue(arrayToAsyncIterableIterator(mockedValue));
const arr: number[] = [];
// Array.from does not work for async generators so we iterate and push the values instead
for await (const value of sut.asyncGenerator()) {
arr.push(value);
}
expect(arr).toEqual(mockedValue);
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Adds async generators to the types of functions that will be auto-mocked
Test plan
yarn jest "generator"
passes