-
Notifications
You must be signed in to change notification settings - Fork 576
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
Make the react-native entry point Jest compatible #6012
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,19 @@ | |
/* eslint-disable @typescript-eslint/no-var-requires -- We're exporting using CJS assignment */ | ||
/* eslint-env commonjs */ | ||
|
||
module.exports = require("./dist/bundle.react-native").Realm; | ||
// eslint-disable-next-line no-undef -- In React Native, process is not defined, but in Jest it is | ||
const isJest = process?.env?.JEST_WORKER_ID !== undefined; | ||
|
||
let entryPoint; | ||
|
||
if (isJest) { | ||
// Define a require function that will load the node bundle | ||
// otherwise, metro will preemptively load the node bundle | ||
const nodeRequire = require; | ||
// Jest is running, use the node bundle | ||
entryPoint = nodeRequire("./dist/bundle.node"); | ||
} else { | ||
entryPoint = require("./dist/bundle.react-native"); | ||
} | ||
|
||
module.exports = entryPoint.Realm; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest simply inlining this assignment. No need to store it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I was doing a lot of dancing around how metro bundled this, to ensure it didn't pre-load the node entry point. Setting |
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'm a bit sad that we need to do this 😞 It would be better if we could find a way to keep this pure from runtime checks of the environment ... historically we've been bitten by the brittleness of these many times before.
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.
Also - I'm surprised you're not getting an
Uncaught ReferenceError: process is not defined
when running on React Native 🤔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.
We can't get around the
react-native
preset forjest
, which is basically ensuring thereact-native
entry point is being loaded. I did a lot of digging around this and this was the best solution I could find. At least it's very dependent on Jest and has very little impact on the experience.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 need to refine the comment.
process
is actually defined inreact-native
, but for some reason it is not typed.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.
As per that link, it looks to me that we could probably export the Node.js bundle via the
require
condition (since it's giving priority torequire
overreact-native
and the opposite is true for the defaults of the metro bundler) and avoid the runtime check?https://github.com/facebook/react-native/blob/1ba7bc0eb1099b64601e0037f3ef717fcd1302e3/packages/react-native/jest/react-native-env.js#L15C9-L15C9
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.
How do we set the
require
condition for our users?The linked code is ran by adding
preset: "react-native"
to the jest config, which is automatically done when initializing a React Native project. Jest isn't looking for this automatically in imported packages, as far as I can tell.Jest also doesn't allow for multiple presets
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.
Add this line
after
realm-js/packages/realm/package.json
Line 31 in 0b9dd8b
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.
Oh boy...that works!