diff --git a/examples/async/native-async-mock/.babelrc b/examples/async/native-async-mock/.babelrc new file mode 100644 index 000000000000..c13c5f627fd1 --- /dev/null +++ b/examples/async/native-async-mock/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/examples/async/native-async-mock/__tests__/native-async-mock-test.js b/examples/async/native-async-mock/__tests__/native-async-mock-test.js new file mode 100644 index 000000000000..8ba636b0019c --- /dev/null +++ b/examples/async/native-async-mock/__tests__/native-async-mock-test.js @@ -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(); +}); diff --git a/examples/async/native-async-mock/native.js b/examples/async/native-async-mock/native.js new file mode 100644 index 000000000000..a4be2e7481a5 --- /dev/null +++ b/examples/async/native-async-mock/native.js @@ -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; +}; diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 63f51f569696..d7a37352f2cb 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -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'; @@ -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))) || ((prop === 'source' || prop === 'global' || prop === 'ignoreCase' ||