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

Is it possible to separate request from request-promise? #1

Closed
mochja opened this issue Dec 10, 2020 · 3 comments
Closed

Is it possible to separate request from request-promise? #1

mochja opened this issue Dec 10, 2020 · 3 comments

Comments

@mochja
Copy link

mochja commented Dec 10, 2020

Hello,

I have troubles in bigger codebase, where we use both request and rp, but in jest environment regular request module gets extended with request-promise and promise interface, changing all regular requests (made by request) to be promises and causing unhandled rejections.

Simplest testcase I could come up with:

const request = require('request');
const rp = require('request-promise-native');

test('request does not have promise interface', () => {
	expect(Object.is(request.Request.prototype.init, rp.Request.prototype.init)).toBe(false);
})

When I run in node (outside of jest environment), assertion passes, meaning the init method is not replaced for regular request module.

const request = require('request');
const rp = require('request-promise-native');
const assert = require('assert');

assert.ok(Object.is(request.Request.prototype.init, rp.Request.prototype.init) === false);

Is it possible to fully separate request from request-promise in jest environment?

@antonku
Copy link
Owner

antonku commented Dec 17, 2020

Hi @mochja

It seems that the issue happens when request is required before request-promise-native.
If the order of require statements is changed to the opposite the assertion passes.

This module (jest-transform-stealthy-require) relies on jest.isolateModules() to resolve conflicts between the imported modules. if you use request directly unfortunately the order of imports matters due to this issue: jestjs/jest#7863

I would suggest changing the order of require statements in your codebase so that request-promise-native is imported before request if that is possible.

@mochja
Copy link
Author

mochja commented Dec 17, 2020

@antonku thanks this has worked!

I simply required the rp in the jest setup file.

@antonku
Copy link
Owner

antonku commented Dec 18, 2020

Hi @mochja

Thanks for the feedback, good to know that it helped.

@antonku antonku closed this as completed Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants