Realm 0.15.3 w/ Jest unable to find module realm.node
See original GitHub issueHello, After upgrading to realm 0.15.3, my Jest unit tests stopped working. I have tried both npm and Yarn to install my node modules, but still, it seems that realm.node cannot be found. My app in React Native runs correctly.
It seems similar to this: #713 but I have also tried npm to no avail.
./node_modules/realm/compiled/node-v48_darwin_x64/realm.node' from 'index.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:150:17)
at Object.<anonymous> (node_modules/realm/lib/index.js:77:28)
at Object.<anonymous> (backend/db/util/db.util.js:5:12)
EDIT: It was working just fine with both yarn and npm in Realm 0.15.0 EDIT2:
After some time debugging, I think I have a good idea why this happens.
I believe the problem lies in the script “install.js”
It seems that in order to install realm.node, the script checks if it is in a react-native environment, by trying to require the react-native package.
The thing is, that when running unit-tests, we are not inside a react-native environment, we are in the node environment even if the react native package is present, since the unit tests are ran in the host machine. Therefore, node-pre-gyp install --fallback-to-build
will never run if we are developing in react native.
Bottom line: When trying to have unit tests in react-native, they will always fail, because realm.node will never be installed.
EDIT3: Temporary fix:
I added a post install script in the package.json:
"postinstall" : "./bin/build-realm.sh"
and inside the folder ./bin, I created a build-realm.sh
file with the following contents:
#!/usr/bin/env bash
set -e
npm install -g node-gyp
npm install -g node-pre-gyp
cd ./node_modules/realm/
node-pre-gyp install --fallback-to-build
This will install both node-gyp and node-pre-gyp before executing the build command directly. In my case, I will add the check for pre-existing node-gyp and node-pre-gyp binaries, but you get the idea. It works now.
However, in the case of realm 0.15.3, I believe that no matter what unit testing library one uses, no unit tests will be able to run. I believe that the realm.node file should be built / downloaded immediately after installing, regardless of whether we are developing for react native or not, since unit tests are ran by node, not react native.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (5 by maintainers)
Top GitHub Comments
@kristiandupont It works. thank you.
Yes, this is precisely the issue - Jest runs tests as Node.js code, and Realm is not supported in Node.js on Windows just yet, but it’s something we’re working on at the moment.