diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a20eb60e8f..041610bfe087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/integration-tests/__tests__/generator_mock.test.js b/integration-tests/__tests__/generator_mock.test.js new file mode 100644 index 000000000000..349e7d45148f --- /dev/null +++ b/integration-tests/__tests__/generator_mock.test.js @@ -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); +}); diff --git a/integration-tests/generator-mock/__tests__/generator_mock.test.js b/integration-tests/generator-mock/__tests__/generator_mock.test.js new file mode 100644 index 000000000000..b1294aa76f51 --- /dev/null +++ b/integration-tests/generator-mock/__tests__/generator_mock.test.js @@ -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(); +}); diff --git a/integration-tests/generator-mock/index.js b/integration-tests/generator-mock/index.js new file mode 100644 index 000000000000..2e762526fb99 --- /dev/null +++ b/integration-tests/generator-mock/index.js @@ -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; diff --git a/integration-tests/generator-mock/package.json b/integration-tests/generator-mock/package.json new file mode 100644 index 000000000000..a39800c4abd6 --- /dev/null +++ b/integration-tests/generator-mock/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} \ No newline at end of file diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index ff02d3a5ff38..0f5dc8c91e2c 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -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'; @@ -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' ||