Skip to content

Commit 7d822d7

Browse files
steven10172mjesun
authored andcommitted
Add auto-mock support for generators (#5983)
1 parent ef4862b commit 7d822d7

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
* `[jest-mock]` Add support for auto-mocking generator functions
6+
([#5983](https://github.com/facebook/jest/pull/5983))
57
* `[expect]` Add support for async matchers
68
([#5836](https://github.com/facebook/jest/pull/5919))
79
* `[expect]` Suggest toContainEqual
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
'use strict';
10+
11+
const runJest = require('../runJest');
12+
13+
test('mock works with generator', () => {
14+
const {status} = runJest('generator-mock');
15+
16+
expect(status).toBe(0);
17+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
jest.mock('../index');
9+
10+
const methods = require('../index');
11+
12+
test('mock works with generator', () => {
13+
expect(methods.generatorMethod).toBeDefined();
14+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
function* generatorMethod() {
9+
yield 42;
10+
}
11+
12+
module.exports.generatorMethod = generatorMethod;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

packages/jest-mock/src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ function isA(typeName: string, value: any): boolean {
161161
}
162162

163163
function getType(ref?: any): string | null {
164-
if (isA('Function', ref) || isA('AsyncFunction', ref)) {
164+
if (
165+
isA('Function', ref) ||
166+
isA('AsyncFunction', ref) ||
167+
isA('GeneratorFunction', ref)
168+
) {
165169
return 'function';
166170
} else if (Array.isArray(ref)) {
167171
return 'array';
@@ -194,7 +198,9 @@ function isReadonlyProp(object: any, prop: string): boolean {
194198
prop === 'callee' ||
195199
prop === 'name' ||
196200
prop === 'length') &&
197-
(isA('Function', object) || isA('AsyncFunction', object))) ||
201+
(isA('Function', object) ||
202+
isA('AsyncFunction', object) ||
203+
isA('GeneratorFunction', object))) ||
198204
((prop === 'source' ||
199205
prop === 'global' ||
200206
prop === 'ignoreCase' ||

0 commit comments

Comments
 (0)