Skip to content

Commit 665659b

Browse files
author
Sam Hewitt
committed
feat: add auto-mock support for async generators
1 parent 42f78d4 commit 665659b

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
@@ -2,6 +2,7 @@
22

33
### Features
44

5+
- `[jest-mock]` Add support for auto-mocking async generator functions ([#11080](https://github.com/facebook/jest/pull/11080))
56
- `[jest-circus]` [**BREAKING**] Fail tests when multiple `done()` calls are made ([#10624](https://github.com/facebook/jest/pull/10624))
67
- `[jest-circus, jest-jasmine2]` [**BREAKING**] Fail the test instead of just warning when describe returns a value ([#10947](https://github.com/facebook/jest/pull/10947))
78
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))

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
@@ -304,7 +304,8 @@ function getType(ref?: unknown): MockFunctionMetadataType | null {
304304
if (
305305
typeName === 'Function' ||
306306
typeName === 'AsyncFunction' ||
307-
typeName === 'GeneratorFunction'
307+
typeName === 'GeneratorFunction' ||
308+
typeName === 'AsyncGeneratorFunction'
308309
) {
309310
return 'function';
310311
} else if (Array.isArray(ref)) {
@@ -347,7 +348,8 @@ function isReadonlyProp(object: any, prop: string): boolean {
347348
return (
348349
typeName === 'Function' ||
349350
typeName === 'AsyncFunction' ||
350-
typeName === 'GeneratorFunction'
351+
typeName === 'GeneratorFunction' ||
352+
typeName === 'AsyncGeneratorFunction'
351353
);
352354
}
353355

0 commit comments

Comments
 (0)