-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Conversation
Generated by 🚫 dangerJS |
const native = require('../native'); | ||
|
||
test('mock works with native async', () => { | ||
expect(native.asyncMethod).toBeDefined(); |
There was a problem hiding this comment.
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? :)
There was a problem hiding this comment.
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?
@@ -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))) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, interesting!
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? |
Sure, I'll send a followup some time later. |
* Support mocking native AsyncFunction * Add native-async-mock example test * Use strict * Return a promise from mock * Only test async * Revert returning promise
* Support mocking native AsyncFunction * Add native-async-mock example test * Use strict * Return a promise from mock * Only test async * Revert returning promise
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. |
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. |
Summary
Fixes #3135
Test plan
New directory in
async
example with no babel async transform. Passes here, fails on current master.