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

Conversation

thymikee
Copy link
Collaborator

Summary

Fixes #3135

Test plan

New directory in async example with no babel async transform. Passes here, fails on current master.

@thymikee thymikee changed the title Native async Support native async in mocks Mar 25, 2017
@thymikee thymikee changed the title Support native async in mocks Support mocking native async methods Mar 25, 2017
@jest-bot
Copy link
Contributor

jest-bot commented Mar 25, 2017

Fails
🚫 New JS files do not have the Facebook copyright header: examples/async/native-async-mock/__tests__/native-async-mock-test.js and examples/async/native-async-mock/native.js

Generated by 🚫 dangerJS

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

test('mock works with native async', () => {
expect(native.asyncMethod).toBeDefined();
Copy link
Contributor

Choose a reason for hiding this comment

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

can we make it to return a promise though? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So actually, the problem here is not with defining implementation of mock (which works well in both scenarios) but rather with method availability. When a method is an AsyncFunction type, its name isn't copied to the mock, but it does for sync methods, so I figured out we should follow this pattern. Does it make sense?

@aaronabramov aaronabramov merged commit 60b4139 into jestjs:master Mar 27, 2017
@thymikee thymikee deleted the native-async branch March 27, 2017 20:27
@@ -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!

@cpojer
Copy link
Member

cpojer commented Mar 28, 2017

Can we move this from examples into an integration test instead? I don't think this needs to be an example :)

You can even make this a regular test inside of jest-mock and simply skip it if the node version is <7.5 or so. How about that?

@thymikee
Copy link
Collaborator Author

Sure, I'll send a followup some time later.

skovhus pushed a commit to skovhus/jest that referenced this pull request Apr 29, 2017
* Support mocking native AsyncFunction

* Add native-async-mock example test

* Use strict

* Return a promise from mock

* Only test async

* Revert returning promise
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Support mocking native AsyncFunction

* Add native-async-mock example test

* Use strict

* Return a promise from mock

* Only test async

* Revert returning promise
@nareshbhatia
Copy link

This fix does not seem to work for me. Please see my test here where I have tried to mock an async method in a module.

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jest.mock does not mock async methods in module
6 participants