From 665659bdce5f81b2f9077da3a201cf6f81e03fb3 Mon Sep 17 00:00:00 2001 From: Sam Hewitt Date: Fri, 12 Feb 2021 09:12:13 -0500 Subject: [PATCH] feat: add auto-mock support for async generators --- CHANGELOG.md | 1 + e2e/generator-mock/__tests__/generatorMock.test.js | 4 ++++ e2e/generator-mock/index.js | 5 +++++ packages/jest-mock/src/index.ts | 6 ++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc8f3169adf..12bea647c0c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-mock]` Add support for auto-mocking async generator functions ([#11080](https://github.com/facebook/jest/pull/11080)) - `[jest-circus]` [**BREAKING**] Fail tests when multiple `done()` calls are made ([#10624](https://github.com/facebook/jest/pull/10624)) - `[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)) - `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874)) diff --git a/e2e/generator-mock/__tests__/generatorMock.test.js b/e2e/generator-mock/__tests__/generatorMock.test.js index f34730b014c3..8da54d3f63f6 100644 --- a/e2e/generator-mock/__tests__/generatorMock.test.js +++ b/e2e/generator-mock/__tests__/generatorMock.test.js @@ -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(); +}); diff --git a/e2e/generator-mock/index.js b/e2e/generator-mock/index.js index 03da7dc17eda..9bc2c6208c40 100644 --- a/e2e/generator-mock/index.js +++ b/e2e/generator-mock/index.js @@ -9,4 +9,9 @@ function* generatorMethod() { yield 42; } +async function* asyncGeneratorMethod() { + yield 42; +} + module.exports.generatorMethod = generatorMethod; +module.exports.asyncGeneratorMethod = asyncGeneratorMethod; diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 2ed41294e06b..382074187b25 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -304,7 +304,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)) { @@ -347,7 +348,8 @@ function isReadonlyProp(object: any, prop: string): boolean { return ( typeName === 'Function' || typeName === 'AsyncFunction' || - typeName === 'GeneratorFunction' + typeName === 'GeneratorFunction' || + typeName === 'AsyncGeneratorFunction' ); }