iamolegga / react-native-launch-arguments

Get launch arguments for testing with Detox and Appium
MIT License
54 stars 20 forks source link

TypeError: Cannot read properties of undefined (reading 'value') #43

Open jgeorge-benchling opened 1 year ago

jgeorge-benchling commented 1 year ago

Hi there, My tests in CI are failing with the error:

_Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'value')
at Object.<anonymous> (node_modules/react-native-launch-arguments/dist/index.js:2:43)_

Please, could you add optional chaining to the second line of the code below:

import { NativeModules } from "react-native";
const raw = NativeModules.LaunchArguments.value;
const parsed = {};
for (const k in raw) {
    const rawValue = raw[k];
    try {
        parsed[k] = JSON.parse(rawValue);
    }
    catch {
        parsed[k] = rawValue;
    }
}
export const LaunchArguments = {
    value() {
        return parsed;
    },
};

const raw = NativeModules.LaunchArguments.value; will be const raw = NativeModules.LaunchArguments?.value; This will remove the TypeError that I get in CI.

aleksanb commented 1 year ago

If the native module isn't set up correctly then you could experience this error. LaunchArguments should always be present in your build, so using ? will surpress any potential configuration errors.

christophby commented 4 months ago

Had the same error in my jest tests. I added this to my jest.setup.ts to solve that issue

jest.mock('react-native-launch-arguments', () => {
  return {
    LaunchArguments: {
      value: jest.fn().mockReturnValue({}),
    },
  };
});