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

feat: add auto-mock support for async generators #11080

Merged
merged 2 commits into from
Feb 24, 2022

Conversation

shooit
Copy link
Contributor

@shooit shooit commented Feb 12, 2021

Summary

Adds async generators to the types of functions that will be auto-mocked

Test plan

yarn jest "generator" passes

@SimenB
Copy link
Member

SimenB commented Feb 12, 2021

Cool! If that is invoked, is it a generator? I believe we have some inference for that

@shooit
Copy link
Contributor Author

shooit commented Feb 12, 2021

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

index.ts

export function* generator(): IterableIterator<number> {
  yield 1;
  yield 2;
  yield 3;
}

export async function* asyncGenerator(): AsyncIterableIterator<number> {
  yield 1;
  yield 2;
  yield 3;
}

index.test.ts

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);
});

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@SimenB SimenB merged commit 103fb15 into jestjs:main Feb 24, 2022
@SimenB
Copy link
Member

SimenB commented Feb 24, 2022

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants