exponea / exponea-react-native-sdk

MIT License
8 stars 13 forks source link

calling the exponea configure function causes all tests to fail #112

Closed Adnan-Bacic closed 10 months ago

Adnan-Bacic commented 10 months ago

i have a standard exponea configure setup:

import Exponea from 'react-native-exponea-sdk';
import Configuration from 'react-native-exponea-sdk/lib/Configuration';

const exponeaConfiguration: Configuration = {
  projectToken: token,
  authorizationToken: token,
  baseUrl: 'https://api.eu1.exponea.com',
};

export const configureExponea = async (): Promise<void> => {
  try {
    if (!await Exponea.isConfigured()) {
      Exponea.configure(exponeaConfiguration);
    }
  } catch {
    Promise.resolve();
  }
};

when i run my test script, ALL tests fail due to calling the exponea function. these are just some snapshot tests i have added before integrating exponea.

i get this error message in all my snapshot test files:

FAIL  __tests__/App.test.tsx
  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'configure')

    > 1 | import Exponea from 'react-native-exponea-sdk';
        | ^
      2 | import Configuration from 'react-native-exponea-sdk/lib/Configuration';
      3 |

      at Object.configure (node_modules/react-native-exponea-sdk/lib/index.js:46:53)
      at Object.require (src/services/exponea.ts:1:1)
      at Object.require (src/services/index.ts:3:1)
      at Object.require (src/screens/Benefits/screens/BenefitsCategoriesScreen.tsx:4:1)
      at Object.require (src/screens/Benefits/index.ts:1:1)
      at Object.require (src/screens/index.ts:1:1)
      at Object.require (src/navigation/index.tsx:8:1)
      at Object.require (App.tsx:8:1)
      at Object.require (__tests__/App.test.tsx:7:1)

it seems to be referring to these lines:

var Exponea = {
    configure: react_native_1.NativeModules.Exponea.configure,
    isConfigured: react_native_1.NativeModules.Exponea.isConfigured,
    getCustomerCookie: react_native_1.NativeModules.Exponea.getCustomerCookie,
    checkPushSetup: react_native_1.NativeModules.Exponea.checkPushSetup,
    getFlushMode: react_native_1.NativeModules.Exponea.getFlushMode,
    setFlushMode: react_native_1.NativeModules.Exponea.setFlushMode,
    getFlushPeriod: react_native_1.NativeModules.Exponea.getFlushPeriod,
    setFlushPeriod: react_native_1.NativeModules.Exponea.setFlushPeriod,
    getLogLevel: react_native_1.NativeModules.Exponea.getLogLevel,
    setLogLevel: react_native_1.NativeModules.Exponea.setLogLevel,
    ...
}
adam1929 commented 10 months ago

Hi @Adnan-Bacic thank you for reporting. Would be accepted by you to skip SDK init while running tests? Problem here is caused by principle, that Unit tests are NOT running in platform environment but web env. SDK is not supporting web but only Android and iOS. For example Exponea.configure is directly accessing 'configure' method in NativeModules and these are available in iOS or Android environment. Or you may create MOCK but I prefer to skip SDK init if is not required for your testing purposes. See how