Skip to content

Commit 103fb15

Browse files
authored
feat: add auto-mock support for async generators (#11080)
1 parent 15feaca commit 103fb15

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- `[jest-haste-map]` Add support for `dependencyExtractor` written in ESM ([#12008](https://github.com/facebook/jest/pull/12008))
2626
- `[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))
2727
- `[jest-mock]` Improve `isMockFunction` to infer types of passed function ([#12442](https://github.com/facebook/jest/pull/12442))
28+
- `[jest-mock]` Add support for auto-mocking async generator functions ([#11080](https://github.com/facebook/jest/pull/11080))
2829
- `[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))
2930
- `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392))
3031
- `[jest-resolve, jest-runtime]` Add support for async resolver ([#11540](https://github.com/facebook/jest/pull/11540))

e2e/generator-mock/__tests__/generatorMock.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ const methods = require('../index');
1212
test('mock works with generator', () => {
1313
expect(methods.generatorMethod).toBeDefined();
1414
});
15+
16+
test('mock works with asyncGenerator', () => {
17+
expect(methods.asyncGeneratorMethod).toBeDefined();
18+
});

e2e/generator-mock/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ function* generatorMethod() {
99
yield 42;
1010
}
1111

12+
async function* asyncGeneratorMethod() {
13+
yield 42;
14+
}
15+
1216
module.exports.generatorMethod = generatorMethod;
17+
module.exports.asyncGeneratorMethod = asyncGeneratorMethod;

packages/jest-mock/src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ function getType(ref?: unknown): MockFunctionMetadataType | null {
399399
if (
400400
typeName === 'Function' ||
401401
typeName === 'AsyncFunction' ||
402-
typeName === 'GeneratorFunction'
402+
typeName === 'GeneratorFunction' ||
403+
typeName === 'AsyncGeneratorFunction'
403404
) {
404405
return 'function';
405406
} else if (Array.isArray(ref)) {
@@ -442,7 +443,8 @@ function isReadonlyProp(object: any, prop: string): boolean {
442443
return (
443444
typeName === 'Function' ||
444445
typeName === 'AsyncFunction' ||
445-
typeName === 'GeneratorFunction'
446+
typeName === 'GeneratorFunction' ||
447+
typeName === 'AsyncGeneratorFunction'
446448
);
447449
}
448450

0 commit comments

Comments
 (0)