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

Doc: Runtime patching on Node.js #14

Closed
d9k opened this issue Jul 28, 2021 · 4 comments
Closed

Doc: Runtime patching on Node.js #14

d9k opened this issue Jul 28, 2021 · 4 comments

Comments

@d9k
Copy link

d9k commented Jul 28, 2021

Does this library supports runtime patching (without the use of webpack at all)?

inside node interpreter:

MR = require('mock-requests');
fetch = require('node-fetch');

MR.setMockUrlResponse('http://t/1', {
  mockExample: 1
});

fetch('http://t/1').then((response) => response.json()).then(data => console.log(data));
// request fails
@d9k
Copy link
Author

d9k commented Jul 28, 2021

fetch = require('node-fetch');
global.fetch = fetch
MR = require('mock-requests');

MR.setMockUrlResponse('http://localhost:1111/t', {
  mockExample: 1
});

fetch('http://localhost:1111/t').then((response) => response.json()).then(data => console.log(data));
// request fails

@d9k
Copy link
Author

d9k commented Jul 28, 2021

OK, found a solution

global.fetch = require('node-fetch');
global.Headers = fetch.Headers;
MR = require('mock-requests');

MR.setMockUrlResponse('http://localhost:1111/t', {
  mockExample: 1
});

fetch('http://localhost:1111/t').then((response) => response.json()).then(data => console.log(data));
// works OK

@d9k d9k changed the title Runtime patching support Runtime patching on Node.js Jul 28, 2021
@d9k d9k changed the title Runtime patching on Node.js Doc: Runtime patching on Node.js Jul 28, 2021
@D-Pow
Copy link
Owner

D-Pow commented Jul 29, 2021

This is a great catch, @d9k!

As you mentioned, node-fetch recommends its users to patch the global.fetch function in order to use it. That's why I've typically favored using isomorphic-fetch in the past for both browser and node polyfills -- because it automatically patches everything for the user.

That being said, this should be made specifically clear in the MockRequests docs so that other users aren't left scratching their heads over why it's not working as expected. I'll leave this issue open until I push the update.

D-Pow added a commit that referenced this issue Nov 10, 2021
# Enhance XMLHttpRequest docs and implementation

This PR fixes issues [#14](#14), [#15](#15), and [#16](#16).

Primary changes:

* Add handlers for both `onload` and `onloadend` events in addition to `readystatechange` in order to support `axios@>=0.22.2`.
* Support both `on[event]` and `addEventListener(event)` methods for `XMLHttpRequest`.
* Add shim for usage in back-end/NodeJS scripts using `axios` with `XMLHttpRequest` polyfills.
* Add extensive documentation in the ReadMe for how to use MockRequests in the back-end/NodeJS scripts.

Secondary changes:

* Update both MockRequests and its demo to use `node@>=14` for better dependency resolution.
    - Requires replacing `node-sass` with `sass` and fixing the incorrect re-defining of `vendor` in both `entry` and `optimization` within webpack.config.js.
* Make homepage redirect to JSDoc website.
* Ensure Jest DOM is defined by changing setup files from `setupFiles` to `setupFilesAfterEnv`.
* Fix ReadMe links to work on npmjs.com (they already worked perfectly on GitHub, but npmjs.com is too primitive to understand renamed hyperlinks via `<a name="X"/>`).
* Simplify wrapper overriding logic.
* Add tests for all sorts of `addEventListener()` usages.
@D-Pow
Copy link
Owner

D-Pow commented Nov 10, 2021

This should be fixed in PR #17. Please let me know if you still encounter any issues!

@D-Pow D-Pow closed this as completed Nov 10, 2021
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