Skip to content

Commit

Permalink
feat: add auto-mock support for async generators
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hewitt committed Feb 12, 2021
1 parent 42f78d4 commit 665659b
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 @@ -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))
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 @@ -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)) {
Expand Down Expand Up @@ -347,7 +348,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 665659b

Please sign in to comment.