Skip to content

Commit

Permalink
Add auto-mock support for generators (#5983)
Browse files Browse the repository at this point in the history
  • Loading branch information
steven10172 authored and mjesun committed Apr 14, 2018
1 parent ef4862b commit 7d822d7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

* `[jest-mock]` Add support for auto-mocking generator functions
([#5983](https://github.com/facebook/jest/pull/5983))
* `[expect]` Add support for async matchers
([#5836](https://github.com/facebook/jest/pull/5919))
* `[expect]` Suggest toContainEqual
Expand Down
17 changes: 17 additions & 0 deletions integration-tests/__tests__/generator_mock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

test('mock works with generator', () => {
const {status} = runJest('generator-mock');

expect(status).toBe(0);
});
14 changes: 14 additions & 0 deletions integration-tests/generator-mock/__tests__/generator_mock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

jest.mock('../index');

const methods = require('../index');

test('mock works with generator', () => {
expect(methods.generatorMethod).toBeDefined();
});
12 changes: 12 additions & 0 deletions integration-tests/generator-mock/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

function* generatorMethod() {
yield 42;
}

module.exports.generatorMethod = generatorMethod;
5 changes: 5 additions & 0 deletions integration-tests/generator-mock/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
10 changes: 8 additions & 2 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ function isA(typeName: string, value: any): boolean {
}

function getType(ref?: any): string | null {
if (isA('Function', ref) || isA('AsyncFunction', ref)) {
if (
isA('Function', ref) ||
isA('AsyncFunction', ref) ||
isA('GeneratorFunction', ref)
) {
return 'function';
} else if (Array.isArray(ref)) {
return 'array';
Expand Down Expand Up @@ -194,7 +198,9 @@ function isReadonlyProp(object: any, prop: string): boolean {
prop === 'callee' ||
prop === 'name' ||
prop === 'length') &&
(isA('Function', object) || isA('AsyncFunction', object))) ||
(isA('Function', object) ||
isA('AsyncFunction', object) ||
isA('GeneratorFunction', object))) ||
((prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
Expand Down

0 comments on commit 7d822d7

Please sign in to comment.