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

Support mocking native async methods #3209

Merged
merged 6 commits into from
Mar 27, 2017
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
3 changes: 3 additions & 0 deletions examples/async/native-async-mock/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2004-present Facebook. All Rights Reserved.

'use strict';

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

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

test('mock works with native async', () => {
expect(native.asyncMethod).toBeDefined();
});
14 changes: 14 additions & 0 deletions examples/async/native-async-mock/native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2004-present Facebook. All Rights Reserved.

'use strict';

function awaitable() {
return Promise.resolve();
}

module.exports.syncMethod = () => 42;

module.exports.asyncMethod = async () => {
await awaitable();
return 42;
};
4 changes: 2 additions & 2 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function isA(typeName: string, value: any): boolean {
}

function getType(ref?: any): string | null {
if (isA('Function', ref)) {
if (isA('Function', ref) || isA('AsyncFunction', ref)) {
return 'function';
} else if (Array.isArray(ref)) {
return 'array';
Expand Down Expand Up @@ -127,7 +127,7 @@ function isReadonlyProp(object: any, prop: string): boolean {
prop === 'callee' ||
prop === 'name' ||
prop === 'length') &&
isA('Function', object)) ||
(isA('Function', object) || isA('AsyncFunction', object))) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, interesting!

((prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
Expand Down