-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Documentation update for testing React Native using jsdom #1873
Documentation update for testing React Native using jsdom #1873
Conversation
docs/guides/react-native.md
Outdated
|
||
## Loading an emulated DOM with JSDOM | ||
|
||
To use enzyme's `deep` until a React Native adapter exists, an emulated DOM must be loaded. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is "deep"? Do you mean mount
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes mount, good catch, I'll update that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been updated to mount
docs/guides/react-native.md
Outdated
|
||
function copyProps(src, target) { | ||
const props = Object.getOwnPropertyNames(src) | ||
.filter(prop => typeof target[prop] === "undefined") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use single quotes here, so the JS would pass eslint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated all the double quotes in the page to single quotes
docs/guides/react-native.md
Outdated
const { window } = jsdom; | ||
|
||
function copyProps(src, target) { | ||
const props = Object.getOwnPropertyNames(src) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is Object.getOwnPropertyDescriptors
not an option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular function comes from the JSDOM setup documentation: https://airbnb.io/enzyme/docs/guides/jsdom.html
I kept it consistent with that page, copying the setup.js part there but adding some other settings below for the adapter and special handling some console warnings.
I can look into getOwnPropertyDescriptors
but probably would want to update it on both pages.
What do you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's update to use it; it's cleaner anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to support es5?
I updated for now to:
function copyProps(src, target) {
Object.defineProperties(target, {
...Object.getOwnPropertyDescriptors(src),
...Object.getOwnPropertyDescriptors(target)
});
}
I think that does what we want cleanly, but VSCode is complaining:
[ts] Property 'getOwnPropertyDescriptors' does not exist on type 'ObjectConstructor'. Did you mean 'getOwnPropertyDescriptor'?
lib.es5.d.ts(172, 5): 'getOwnPropertyDescriptor' is declared here.
Because Object.getOwnPropertyDescriptors isn't in the es5 spec and unsupported also for IE:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know whether we should keep as is to support es5 / IE, or whether the updated code I have above with getOwnPropertyDescriptors
is preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gOPDs is polyfillable, so it should work just fine. https://www.npmjs.com/package/object.getownpropertydescriptors
The spread can be transpiled to Object.assign which is also polyfillable https://www.npmjs.com/package/object.assign
so i think that update works great
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I updated copyProps here and in two other places in the codebase:
docs/guides/jsdom.md
packages/enzyme-example-mocha/test/.setup.js
fe5b22d
to
ba7675e
Compare
Hey @shawnaxsom I'm trying out what you have in the docs, but I'm getting |
@jpaas Is the code public so I can see? Are you importing StyleSheet anywhere? StyleSheet should come from "react-native" instead of "react-native-implementation" generally. Take a look at the boilerplate code we have here to compare: |
@shawnaxsom no sorry its a private repo. I did have a look at your repo. My
|
@jpaas Have you compared versions in package.json? What version of "react-native" are you on? |
@jpaas Also make sure you have this in package.json: https://stackoverflow.com/questions/47269957/cannot-find-module-from-react-native-implementation-js
|
@shawnaxsom I'm using react-native 0.57.4 & react 16.6.0-alpha.8af6728 vs your 0.57.2 and react 16.5.0. I tried rolling back to your version, but no difference. I tried that jest preset in the When I try to use your |
You might need to install metro-react-native-babel-preset and use our
babel.config.js if you want to try our jest.config.js.
…On Fri, Oct 26, 2018 at 10:36 AM Julian Paas ***@***.***> wrote:
@shawnaxsom <https://github.com/shawnaxsom> I'm using react-native 0.57.4
& react 16.6.0-alpha.8af6728 vs your 0.57.2 and react 16.5.0. I tried
rolling back to your version, but no difference.
I tried that jest preset in the package.json but it didn't help, I
suspect because my jest.config.js overrides it with the ts-jest preset.
When I try to use your jest.config.js, I get the error Couldn't find
preset "module:metro-react-native-babel-preset" relative to my root dir`
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1873 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AjcHuroBk9_ioRJgaPf05RC1D4JWXW8lks5uox3ngaJpZM4XyN94>
.
|
@shawnaxsom I am using |
If you have time to set up a public project that replicates the issue I
would be happy to take a look, or I might be able to screen share at some
point. It's hard to diagnose unfortunately without being able to look at
the project.
…On Fri, Oct 26, 2018 at 12:33 PM Julian Paas ***@***.***> wrote:
@shawnaxsom <https://github.com/shawnaxsom> I am using
metro-react-native-babel-preset as my preset in my .babelrc, so I imagine
there's something about your jest.config.js that is preventing it from
being resolved.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1873 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAlGwkh5zQgkoPhjyl2CndvRrnfev9DTks5uozk8gaJpZM4XyN94>
.
|
55ea48f
to
69869e0
Compare
As discussed in #1375, I was also unable to get React Native testing working with Jest/Enzyme with the current documentation instructions.
I see that a React Native adapter is still being discussed in #1436, but I found that some users have had success with jsdom. I was able to get both react-native-mock-renderer and jsdom working, but react-native-mock-renderer didn't result in very clear snapshots when using Jest snapshot testing.
I have therefore provided some instructions here for how I was able to get React Native testing working with jsdom, and I added some examples from the React Native boilerplate project where it is currently working. I decided to leave in a few helpful tips of other issues I encountered along the way as well.