Skip to content

Commit

Permalink
feat: add auto-mock support for async generators (#11080)
Browse files Browse the repository at this point in the history
  • Loading branch information
shooit committed Feb 24, 2022
1 parent 15feaca commit 103fb15
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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
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

0 comments on commit 103fb15

Please sign in to comment.