Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-mock support for generators #5983

Merged
merged 2 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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