I got
> [email protected] test /Users/keen/dev/work_native/spring2016
> jest
Using Jest CLI v0.10.0, jasmine2, babel-jest
FAIL __tests__/hackathon-test.js
● Runtime Error
TypeError: Cannot read property 'debugHosts' of undefined
at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/node_modules/realm/lib/browser/index.js:30:69)
at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/node_modules/realm/lib/index.js:29:18)
at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/services/hack-service.js:2:12)
at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/__tests__/hackathon-test.js:5:18)
npm ERR! Test failed. See above for more details.
message.
My Package.json is shown below.
{
"name": "spring2016",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react-native": "^0.21.0",
"react-native-action-button": "^1.1.3",
"react-native-facebook-login": "^1.0.3",
"react-native-image-picker": "^0.18.3",
"react-native-list-popover": "^1.0.5",
"react-native-material-design": "^0.3.3",
"react-native-material-kit": "^0.3.0",
"react-native-vector-icons": "^1.3.2",
"realm": "^0.11.0"
},
"rnpm": {
"ios": {
"project": "ios/spring2016.xcodeproj"
}
},
"jest": {
"setupEnvScriptFile": "<rootDir>/node_modules/react-native/jestSupport/env.js",
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
],
"testFileExtensions": [
"js"
],
"unmockedModulePathPatterns": [
"promise",
"source-map",
"<rootDir>/node_modules/react-native",
"<rootDir>/node_modules/realm"
]
},
"devDependencies": {
"babel-jest": "*",
"babel-preset-react-native": "^1.5.6",
"jest-cli": "*",
"react-native": "^0.21.0"
}
}
https://gist.github.com/ehrudxo/684b5aeb7211c18237092aece45f29fb
and .babelrc is like
{
"presets": ["react-native"]
}
It sounds like the Realm module is not getting loaded properly. Did you add Realm to your xcodeproj by running rnpm link realm?
Any luck?
I'm having similar issues. Below is the error I see in the console,
● Runtime Error
SyntaxError: Unexpected reserved word in file 'node_modules/realm/lib/browser/index.js'.
Make sure your preprocessor is set up correctly and ensure your 'preprocessorIgnorePatterns' configuration is correct: http://facebook.github.io/jest/docs/api.html#preprocessorignorepatterns-array-string
If you are currently setting up Jest or modifying your preprocessor, try jest --no-cache.
Preprocessor: node_modules/react-native/jestSupport/preprocessor.js.
Jest tried to the execute the following preprocessed code:
and it prints the whole index.js file in console.
My deps,
"dependencies": {
"react": "^0.14.7",
"react-addons-shallow-compare": "^0.14.7",
"react-native": "^0.24.0-rc4",
"react-redux": "^4.4.2",
"realm": "^0.11.1",
"redux": "^3.4.0",
"redux-logger": "^2.6.1",
"redux-thunk": "^1.0.3",
"regenerator": "^0.8.42"
},
"devDependencies": {
"babel-eslint": "^5.0.0",
"babel-jest": "^11.0.0",
"babel-preset-react-native": "^1.5.6",
"eslint": "^2.7.0",
"eslint-plugin-react": "^4.3.0",
"jest-cli": "^11.0.0",
"react-native": "^0.24.0-rc4"
}
@alazier I did it.but It didn't work
@ehrudxo It looks like the correct solution would be for us to mock NativeModules.Realm. Were you able to work around this issue?
@ramsundark5 The files under browser/ are written in ES6+, so they need Babel to preprocess them. My understanding was that the latest version of Jest automatically handled doing that. If you're on the latest and are still getting the error, would you mind sharing an example that demonstrates this issue so I can get it resolved for you?
@appden I did not work around this issue. I just let my project undone and solve another problem. Does latest Jest solve this problem?
@ehrudxo The workaround is to mock NativeModules.Realm.debugHosts. I will see if I can include that by default in the npm module so you won't have to in the future.
@appden Thanks. I will close. this issue.
@appden Have you managed to get that work around into the node module? I appear to be getting the same error as OP. Also working with Jest.
TypeError: Cannot read property 'debugHosts' of undefined
at Object.<anonymous> (/Users/chris/Desktop/mobile/node_modules/realm/lib/browser/index.js:30:69)
at Object.<anonymous> (/Users/chris/Desktop/mobile/node_modules/realm/lib/index.js:30:18)
at Object.<anonymous> (/Users/chris/Desktop/mobile/src/database/realm.js:9:145)
at Object.<anonymous> (/Users/chris/Desktop/mobile/src/utilities/__tests__/getStockOnDate.spec.js:7:12)
at jasmine2 (/Users/chris/Desktop/mobile/node_modules/jest-jasmine2/src/index.js:193:16)
at handle (/Users/chris/Desktop/mobile/node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (/Users/chris/Desktop/mobile/node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:100:13)
at process.emit (events.js:185:7)
at handleMessage (internal/child_process.js:718:10)
at Pipe.channel.onread (internal/child_process.js:444:11)
I don't suppose it is as simple as jest.mock('NativeModules.Realm.debugHosts');?
I admit I know it's not, as it gives me following error in place of the above error:
Error: Cannot find module 'NativeModules.Realm.debugHosts' from 'getStockOnDate.spec.js'
at Runtime._resolveNodeModule (/Users/chris/Desktop/mobile/node_modules/jest-cli/src/Runtime/Runtime.js:449:11)
at Object.<anonymous> (/Users/chris/Desktop/mobile/src/utilities/__tests__/getStockOnDate.spec.js:4:6)
at jasmine2 (/Users/chris/Desktop/mobile/node_modules/jest-jasmine2/src/index.js:193:16)
So is there any known solution for this yet?
Any updates on Realm working with a testing framework? Currently this is a big roadblock to using Realm in my project. re: https://github.com/realm/realm-js/issues/521
@TGPSKI Seconded, except we've just gone ahead and gone dark on writing tests that relate to the DB.
@Chris-Petty we have made some progress making things work with AVA and mocha - see #521 - still haven't had luck making things work with Jest yet although we are still looking into it.
All, we are not officially supporting realm-js with Node, although we intend to soon. Instead, if you are interested, please email me at [email protected] as I would like to learn more on what you would like to see in an official Node release.
when i first use realm in for app, the Simulator show undefined is not an object (evaluating '_NativeModules$Realm.debugHosts').Can i get any solutions to fix this bug ?
+1
undefined is not an object (evaluating '_NativeModules$Realm.debugHosts')
in Android Reactnative
how to solved this error
I have this very basic realm mock
export default class Realm {
schema: any = [];
data: any = [];
constructor(params: any) {
require('lodash').each(params.schema, (schema) => {
this.data[schema.name] = [];
this.data[schema.name].filtered = () => {
return this.data[schema.name];
};
});
this.schema = params.schema;
}
objects(schemaName) {
return this.data[schemaName];
}
write(fn) {
fn();
}
create(schemaName, data) {
this.data[schemaName].push(data);
return data;
}
};
jest.mock('realm', () => {
return require('../../mocks/realm').default;
});
That .default is bc I'm on typescript heh, you should take it away if you go with es*
Thanks @hybrisCole for a basic mock. I created one for my use based on this.
https://gist.github.com/hyb175/beb9ceed4c34300ba7c77d3d6d44ae52
Most helpful comment
Thanks @hybrisCole for a basic mock. I created one for my use based on this.
https://gist.github.com/hyb175/beb9ceed4c34300ba7c77d3d6d44ae52