-
-
Notifications
You must be signed in to change notification settings - Fork 517
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
React Native support #203
Comments
I wonder if using |
do you think, for react native, that it could work for grahphql requests (via apollo client) as well? |
@hsavit1, I believe so. It's a matter of understanding a request flow in React Native. I assume React Native app runs on Node, so that's why I've suggested to check if using As of GraphQL, it's not much different than a regular REST request. In fact, it is a regular REST request, just compliant with the GraphQL specification (has a |
I believe React Native has some access to Node.js APIs via the node-js mobile library. See this for more. Read more about the javascript core here: https://reactnative.dev/docs/0.60/javascript-environment |
Stumbled across this issue while trying to set up a react native app to use msw for integration tests. I would love to help get this working for react-native outside of tests if someone could point me in the right direction. |
Hey, @donovanhiland! We'd really appreciate your help, if you have a spare minute. Could you please try using Would love to hear how it goes from you. |
Absolutely! @kettanaito I was able to get In trying to implement
There is no global |
@donovanhiland @kettanaito is there a way to shim the node env using this? |
Thanks for trying that out so quickly. Excited to hear that you got it to the working state!
I think this is the gotcha for any Node tests that rely on
This is an interesting detail. Looks like we need to adjust our @hsavit1, that's a nice find! I'm concerned about the amount of modules this single import pulls in. It may have a high cost, which may affect the tests' performance. Perhaps, if we could also check for React Native environment by some distinct flag, we may omit relying on |
@kettanaito this is a project that seems to be a bit more focused |
@donovanhiland How did you do this? |
I can confirm that I was able to get this working for my jest test suite using ApolloProvider and the |
I set |
does the PR #262 address these issues? |
strangely, when trying to run my tests on master, they now fail with error |
Yes, #262 addresses proper environment check for React Native, so it's treated as any other NodeJS process by MSW. The changes are not published yet, however. Looks like you have |
That's great news! In my package.json, I have:
When I run
|
It appears you are installing the library from its Git branch. While that would link the library to your dependencies, it won't build the library. Since build artifacts, such as $ cd node_modules/msw
$ yarn build The screenshot above illustrates that the |
the
|
I was interested to see if it was possible to start msw's server at runtime in react native ( as is possible with the browser's worker ). I was glad to find that it's possible to make it work. My finding: I had to patch msw like this: diff --git a/node_modules/msw/node/index.js b/node_modules/msw/node/index.js
index 0e16e2e..e06d680 100644
--- a/node_modules/msw/node/index.js
+++ b/node_modules/msw/node/index.js
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
-var timers = _interopDefault(require('timers'));
var nodeRequestInterceptor = require('node-request-interceptor');
/*! *****************************************************************************
@@ -590,10 +589,6 @@ function resetHandlers(initialHandlers, ...nextHandlers) {
*/
const setupServer = (...requestHandlers) => {
const interceptor = new nodeRequestInterceptor.RequestInterceptor();
- // Error when attempting to run this function in a browser environment.
- if (!isNodeProcess()) {
- throw new Error('[MSW] Failed to execute `setupServer` in the environment that is not NodeJS (i.e. a browser). Consider using `setupWorker` instead.');
- }
// Store the list of request handlers for the current server instance,
// so it could be modified at a runtime.
let currentHandlers = [...requestHandlers];
@@ -632,7 +627,7 @@ const setupServer = (...requestHandlers) => {
var _a;
// using the timers module to ensure @sinon/fake-timers or jest fake timers
// don't affect this timeout.
- timers.setTimeout(() => {
+ setTimeout(() => {
resolve({
status: response.status,
statusText: response.statusText, and diff --git a/node_modules/node-request-interceptor/lib/RequestInterceptor.js b/node_modules/node-request-interceptor/lib/RequestInterceptor.js
index e24d5df..abfb4cf 100644
--- a/node_modules/node-request-interceptor/lib/RequestInterceptor.js
+++ b/node_modules/node-request-interceptor/lib/RequestInterceptor.js
@@ -36,7 +36,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
-var override_1 = require("./http/override");
var override_2 = require("./XMLHttpRequest/override");
var debug = require('debug')('interceptor');
var RequestInterceptor = /** @class */ (function () {
@@ -69,7 +68,7 @@ var RequestInterceptor = /** @class */ (function () {
}); };
this.middleware = [];
debug('created new RequestInterceptor');
- this.overrides = [override_1.overrideHttpModule, override_2.overrideXhrModule].map(function (override) {
+ this.overrides = [override_2.overrideXhrModule].map(function (override) {
return override(_this.applyMiddleware);
});
}
diff --git a/node_modules/node-request-interceptor/lib/XMLHttpRequest/XMLHttpRequest/createEvent.js b/node_modules/node-request-interceptor/lib/XMLHttpRequest/XMLHttpRequest/createEvent.js
index 8d9479a..81d2e69 100644
--- a/node_modules/node-request-interceptor/lib/XMLHttpRequest/XMLHttpRequest/createEvent.js
+++ b/node_modules/node-request-interceptor/lib/XMLHttpRequest/XMLHttpRequest/createEvent.js
@@ -1,6 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventOverride_1 = require("./EventOverride");
+var ProgressEvent = function(type, eventInit) {
+ this.type = type
+ this.lengthComputable = eventInit.lengthComputable
+ this.loaded = eventInit.loaded
+ this.total = eventInit.total
+ }
+
+ //ProgressEvent.prototype = Object.create(Event.prototype)
+ ProgressEvent.prototype.constructor = ProgressEvent
+
exports.createEvent = function (options, target, type) {
var progressEvents = [
'error', and include a polyfill, That was enough to start using a local dev server in RN, at runtime. |
Nice work @Federkun - did you do this against master or against |
Against 0.19.5 ( 7a72ff7 ), so it doesn't have those few useful last commits |
Hey, @Federkun. Thank you for those insights! -var timers = _interopDefault(require('timers')); I suppose
-if (!isNodeProcess()) { Could you please share your findings on why this line was causing troubles?
-var override_1 = require("./http/override"); This is interesting. I'd expect |
Keep in mind that RN's js runtime is not the same as node, most of those core modules are missing there. That unfortunately includes About, |
Happy to hear that @hsavit1 , I think that we can consider this issue as resolved. Happy coding to everyone 🎉 |
hi @marcosvega91 @hsavit1 , 2- When msw js enabled with running server.listen(), the service calls having no mocks in any handlers give network error Do you have any idea about these situations? |
This is some great work, sadly like @hsavit1 I couldn't get msw running against my jest tests?
I get it now, |
Please try the server.listen({ onUnhandledRequest: 'error' }) |
@ilkerceng I am also trying to implement REST Api. But i get this following error
MSW Version: |
That's odd. We have Line 113 in 8a5f573
This means the |
@kettanaito My Node version is |
I'm having issues getting this to work too. I've tried on an existing project and a brand new project with the same error:
I'm using node version Edit: After going back through the published versions of msw, the last version I'm able to get working is 0.22.2. I've had a quick look at the differences made between 0.22.2 and 0.22.3 to see what's changed that might be causing it but I can't really see what it might be. However, while with that version running I'm able to intercept a rest API call (which is great), it crashes when I try to return a json response. Using react-query to call the API (with fetch), If I use this as my handler array:
I get this error:
If I remove the ctx.json call and just leave the ctx.status it doesn't crash, and the app does end up failing due to there being no json value (which is to be expected as there is none). Has anyone else run into these problems? |
It seems that the bundled version for React Native ( |
The React Native support should be back in 0.31.0. Thanks to @wwdrew. |
I've opened mswjs/examples#60 to get in a Rest React Native example for reference and smoke testing future MSW versions. |
Love this. Great job guys! |
Thank you, @taylorkline! I'll take a look at the example and help you with what I can. |
Hi. Any progress on this? this library is sort-of a last-mile towards getting integration (visual-testing) done using the storybook+expo setup for a current project. But still unsure - if this would work with it; just before trying it out - has anyone got any feedback for the same? Some relevant info - expo^41.0.0 -> rn-0.63; storybook^5.3; Thanks. |
Hey, @astriskit. There's no time capacity to look into this issue. Please feel free to pursue the fix and share the results of the investigation with us. We'd love to have a stable representative of the React Native community to help us with the respective issues. |
@astriskit I'm not sure what progress is being waited on, React Native support should be working fine now. If you're looking for an example, @taylorkline has referenced one above, and there's mine at https://github.com/wwdrew/examples/tree/react-native |
Hi @kettanaito - the {msw, storybook, expo} setup went successfully. Was able to render a network-ed component from the example provided by @wwdrew. And @wwdrew - thanks for the example link - the react-native example was helpful in making the setup work. And for those - who might be reading to get the same setup working -
|
Should we update this FAQ about React Native? I can create a PR on the mswjs.io repo, but I'm not sure what the official stance is on RN support. |
I agree with @blwinters we should update the website |
MSW should work fine in React Native since that's just a Node.js process. You may require to provide your test environment with all the necessary polyfills to run tests. @blwinters, so yes, by all means, pull requests are welcome. What would you like to add? |
@kettanaito Cool, I'll create a PR with text that briefly explains the nature of RN support and use of polyfills and we can refine the text from there. |
I can't get it to work to perform integration tests with jest, is there anything new regarding that? |
Is your feature request related to a problem? Please describe.
Does React Native have the ability to support this?
Describe the solution you'd like
Some guide to do something similar with react native
Describe alternatives you've considered
Can something custom be set up with https://github.com/Blitz-Mobile-Apps/react-native-bg-thread#readme ?
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: