request-promise: Problem with request-promise and request-promise-native in Jest
I have recently found that request-promise and request-promise-native can’t be required at the same time in Jest. The reason is that Jest messes up with the require.cache implementation badly enough that stealthy-require is not able to return a fresh instance of request.
The second require will fail with the error
● Test suite failed to run
Unable to expose method "then"
It is quite a problem because both are just indirect dependencies of many projects, so it creates weird incompatibilities.
Can we get rid of the stealthy-require magic and get a different request instance in a different way, to play nicely with Jest module system? For example, could we expose a clone() method to clone request and its prototype instead?
This affects at least to request-promise, request-promise-native and request-promise-any.
I have prepared a minimal repository to reproduce the problem: https://github.com/davazp/request-promise-issue
If needed, I can work on a pull request for this.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 36
- Comments: 22
Commits related to this issue
- Replace request-promise-native with node-fetch due to https://github.com/request/request-promise/issues/247 that occurs in tests — committed to omrilit/netsuite-suitecloud-sdk by omrilit 4 years ago
- Replace request-promise-native with node-fetch due to https://github.com/request/request-promise/issues/247 that occurs in tests — committed to salto-io/netsuite-suitecloud-sdk by omrilit 4 years ago
- Replace request-promise-native with node-fetch due to https://github.com/request/request-promise/issues/247 that occurs in tests — committed to omrilit/netsuite-suitecloud-sdk by omrilit 4 years ago
- Replace request-promise-native with node-fetch due to https://github.com/request/request-promise/issues/247 that occurs in tests — committed to omrilit/netsuite-suitecloud-sdk by omrilit 4 years ago
- Replace request-promise-native with node-fetch due to https://github.com/request/request-promise/issues/247 that occurs in tests — committed to salto-io/netsuite-suitecloud-sdk by omrilit 4 years ago
- Replace request-promise-native with node-fetch due to https://github.com/request/request-promise/issues/247 that occurs in tests (cherry picked from commit 1a7ca0697918b3397fa493b8202557b991d5fadb) — committed to omrilit/netsuite-suitecloud-sdk by omrilit 4 years ago
I encountered the same problem, and ended with the options of jest
jest.jsonand use a file
jest.setup.jswithWith the new version of Jest, you should use
jest.mock('request-promise-native')instead ofjest.mockModuleI’ve published
jest-transform-stealthy-requiremodule to address this issue, the README contains usage examples: https://github.com/antonku/jest-transform-stealthy-requireHope it helps
My Jest tests are always failing because of it…
Solution @Benno007 does not fit for me because my function does not work at all if I downgrade it.
Thanks for the answer. Finally I resorted to
jest.mockModule('request-promise-native').@hems I am happy to hear that it worked out well. Thank you very much for the feedback!
Adding just
jest.mock('request-promise-native');didn’t fix my issue, but adding this did, so maybe it could help someone else too.
jest.mock('request-promise-native', () => ({ options: { request: {} }, }));I’m also having this problem… while
jest.mock('request-promise');works for one test case, it breaks some other tests that use therequest-promisemodule. Any long term solutions here?Thanks, fixed it like this :
I am having the same issue, got a private package using
requestandrequest-promise-nativeand the project depends onrequest-promise-nativeit throws the errortried using
last one throws
TypeError: Cannot read property 'exposePromiseMethod' of undefinedI am pretty confused and don’t know what to do.Do you have a temporary solution ?