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

Restore function affects the http.request unexpectly #24

Open
xavierchow opened this issue Nov 29, 2015 · 4 comments
Open

Restore function affects the http.request unexpectly #24

xavierchow opened this issue Nov 29, 2015 · 4 comments

Comments

@xavierchow
Copy link
Contributor

Some other libraries such as nock also alter the http.request, as long as you require the mm, even you don't use the mm.http.request, the restore changes it with the origin one(at the context of require moment) by force, which is not expected, see sample below,

var assert = require('assert');
var http = require('http');
var mm = require('mm');

var obj = {
  foo: function () {
    console.log('original foo called');
  }
};
mm(obj, 'foo', function () {
  console.log("mocked foo called");
});
obj.foo();

//Manually override the http.request for certain purpose.
http.request = function (option, callback) {
 throw new Error('Never want to send request out');
} 

mm.restore();
try {
  http.get({ path: '/foo' }, function (res) {});
} catch (e) {
  //assert here failed, because the restore made the overriding above invalid
  assert.equal(e.message, 'Never want to send request out');
}
@alsotang
Copy link
Member

This issue would occurs when mm and nock be used together?

@xavierchow
Copy link
Contributor Author

This issue would occurs when mm and nock be used together?

Yes, also other modules which alter the underlying http.request.

@alsotang
Copy link
Member

it is most likely two hacks can not work correctly together..

I think this issue is inevitable, but yes we can attempt to avoid them

2015-11-30 11:09 GMT+08:00 xavier notifications@github.com:

This issue would occurs when mm and nock be used together?

Yes, also other modules which alter the underlying http.request.


Reply to this email directly or view it on GitHub
#24 (comment).

@xavierchow
Copy link
Contributor Author

It's fine if I use mm.http.request and there's a conflict with other lib which also changes http.request.
But even if I only use the stub feature(underlying muk), the mm.restore still touches the http.request by force. That's the problem I think mm.restore does too much.

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