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
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- `[jest-haste-map]` Add support for `dependencyExtractor` written in ESM ([#12008](https://github.com/facebook/jest/pull/12008))
- `[jest-mock]` [**BREAKING**] Rename exported utility types `ConstructorLike`, `MethodLike`, `ConstructorLikeKeys`, `MethodLikeKeys`, `PropertyLikeKeys`; remove exports of utility types `ArgumentsOf`, `ArgsType`, `ConstructorArgumentsOf` - TS builtin utility types `ConstructorParameters` and `Parameters` should be used instead ([#12435](https://github.com/facebook/jest/pull/12435))
- `[jest-mock]` Improve `isMockFunction` to infer types of passed function ([#12442](https://github.com/facebook/jest/pull/12442))
- `[jest-mock]` Add support for auto-mocking async generator functions ([#11080](https://github.com/facebook/jest/pull/11080))
- `[jest-resolve]` [**BREAKING**] Add support for `package.json` `exports` ([#11961](https://github.com/facebook/jest/pull/11961), [#12373](https://github.com/facebook/jest/pull/12373))
- `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392))
- `[jest-resolve, jest-runtime]` Add support for async resolver ([#11540](https://github.com/facebook/jest/pull/11540))
Expand Down
4 changes: 4 additions & 0 deletions e2e/generator-mock/__tests__/generatorMock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ const methods = require('../index');
test('mock works with generator', () => {
expect(methods.generatorMethod).toBeDefined();
});

test('mock works with asyncGenerator', () => {
expect(methods.asyncGeneratorMethod).toBeDefined();
});
5 changes: 5 additions & 0 deletions e2e/generator-mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ function* generatorMethod() {
yield 42;
}

async function* asyncGeneratorMethod() {
yield 42;
}

module.exports.generatorMethod = generatorMethod;
module.exports.asyncGeneratorMethod = asyncGeneratorMethod;
6 changes: 4 additions & 2 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ function getType(ref?: unknown): MockFunctionMetadataType | null {
if (
typeName === 'Function' ||
typeName === 'AsyncFunction' ||
typeName === 'GeneratorFunction'
typeName === 'GeneratorFunction' ||
typeName === 'AsyncGeneratorFunction'
) {
return 'function';
} else if (Array.isArray(ref)) {
Expand Down Expand Up @@ -442,7 +443,8 @@ function isReadonlyProp(object: any, prop: string): boolean {
return (
typeName === 'Function' ||
typeName === 'AsyncFunction' ||
typeName === 'GeneratorFunction'
typeName === 'GeneratorFunction' ||
typeName === 'AsyncGeneratorFunction'
);
}

Expand Down