Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add auto-mock support for generators
  • Loading branch information
steven10172 committed Apr 13, 2018
commit 5929e9e8f67f122949aebfae3de1b86c27edb566
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

* `[jest-mock]` Add support for auto-mocking generator functions
* `[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