diff --git a/lib/index.js b/lib/index.js index 0f80b8aa..353abe3b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,30 +25,40 @@ function nodeRequire(module) { function getContext() { + // If process is an object, we're probably running in Node or Electron + // From: http://stackoverflow.com/a/24279593/1417293 if (typeof process === 'object' && process + '' === '[object process]') { return process.type === 'renderer' ? 'electron' : 'nodejs'; - } + } - if (typeof navigator !== 'undefined') { - if (navigator.product === 'ReactNative') { // eslint-disable-line no-undef + // When running via Jest, the jest object is defined. + if (typeof jest === 'object') { + return 'nodejs'; + } + + // If we've already injected the Realm class, we are probably + // running in a jscore environment, either directly or via React Native + if (typeof Realm !== 'undefined') { + if (typeof navigator !== 'undefined' && + navigator.product === 'ReactNative') { // eslint-disable-line no-undef return 'reactnative'; } - if (/Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef - return 'chromedebugger'; - } - } - - if (typeof global !== 'undefined') { - if (global.__debug__) { - return 'vscodedebugger'; - } - } - - if (typeof Realm !== 'undefined') { return 'jscore'; } + // Visual Studio Code defines the global.__debug__ object. + if (typeof global !== 'undefined' && global.__debug__) { + return 'vscodedebugger'; + } + + // Finally, if the navigator.userAgent contains the string "Chrome", we're likely + // running via the chrome debugger. + if (typeof navigator !== 'undefined' && + /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef + return 'chromedebugger'; + } + throw Error("Unknown execution context"); } diff --git a/tests/react-test-app/package.json b/tests/react-test-app/package.json index 445c5b79..31355884 100644 --- a/tests/react-test-app/package.json +++ b/tests/react-test-app/package.json @@ -6,11 +6,14 @@ "start": "react-native start" }, "dependencies": { - "react": "15.3.2", - "react-native": "^0.37.0", + "react": "15.4.0", + "react-native": "^0.39.0", "react-native-fs": "^1.1.0", "realm": "file:../..", "realm-tests": "file:../js", "xmlbuilder": "^4.2.1" + }, + "devDependencies": { + "invariant": "^2.2.2" } }