From 5929e9e8f67f122949aebfae3de1b86c27edb566 Mon Sep 17 00:00:00 2001 From: Brice Date: Fri, 13 Apr 2018 16:24:51 -0700 Subject: [PATCH 1/2] Add auto-mock support for generators --- CHANGELOG.md | 1 + .../__tests__/generator_mock.test.js | 17 +++++++++++++++++ .../__tests__/generator_mock.test.js | 14 ++++++++++++++ integration-tests/generator-mock/index.js | 12 ++++++++++++ integration-tests/generator-mock/package.json | 5 +++++ packages/jest-mock/src/index.js | 10 ++++++++-- 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 integration-tests/__tests__/generator_mock.test.js create mode 100644 integration-tests/generator-mock/__tests__/generator_mock.test.js create mode 100644 integration-tests/generator-mock/index.js create mode 100644 integration-tests/generator-mock/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a20eb60e8f..b8b8729afd05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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' || From 0f2eb70f7c2816009876e8ef1384cb81eea6817e Mon Sep 17 00:00:00 2001 From: Brice Date: Fri, 13 Apr 2018 16:44:24 -0700 Subject: [PATCH 2/2] Update changelog with PR link --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8b8729afd05..041610bfe087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 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