React-native-config: How can I access Config in a Detox test that uses Jest as test runner?

Created on 20 May 2019  路  1Comment  路  Source: luggit/react-native-config

I have a react-native app and I can smoothly run Jest unit/component tests and also Detox tests with Jest as the test-runner. In the former tests, I followed the docs and placed my environment variables in __mocks__/react-native-config. However, these variables don't seem to be available when I run a detox end-to-end test.

Here is my jest.config.js:

const { defaults: tsjPreset } = require('ts-jest/presets');

module.exports = {
  ...tsjPreset,
  preset: 'react-native',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  transform: {
    ...tsjPreset.transform,
    '^.+\\.tsx?$': 'ts-jest',
  },
  globals: {
    'ts-jest': {
      babelConfig: true,
      __DEV__: true,
    },
  },
  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!(react-native|@react-native-community|@felfel|react-native-config|react-native-vector-icons|react-native-elements))',
  ],
  cacheDirectory: '.jest/cache',
  setupFiles: ['./src/config/asyncStorageMock.ts'],
  setupFilesAfterEnv: ['./e2e/init.js'],
  testEnvironment: 'node',
  reporters: ['detox/runners/jest/streamlineReporter'],
  verbose: true,
};

Here is the detox key in my package.json:

  "detox": {
    "configurations": {
      "android.emu.debug": {
        "binaryPath": "android/app/build/outputs/apk/debug/app-debug.apk",
        "build": "cd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && cd ..",
        "type": "android.emulator",
        "name": "Nexus_9_API_27"
      },
      "android.emu.release": {
        "binaryPath": "android/app/build/outputs/apk/release/app-release.apk",
        "build": "cd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release && cd ..",
        "type": "android.emulator",
        "name": "Nexus_9_API_27"
      }
    },
    "test-runner": "jest",
    "runner-config": "./jest.config.js"
  }

and my e2e/init.js:

const detox = require('detox');
const config = require('../package.json').detox;
const adapter = require('detox/runners/jest/adapter');
const specReporter = require('detox/runners/jest/specReporter');

// Set the default timeout
jest.setTimeout(120000);
jasmine.getEnv().addReporter(adapter);

// This takes care of generating status logs on a per-spec basis. By default, jest only reports at file-level.
// This is strictly optional.
jasmine.getEnv().addReporter(specReporter);

beforeAll(async () => {
  await detox.init(config);
});

beforeEach(async () => {
  await adapter.beforeEach();
});

afterAll(async () => {
  await adapter.afterAll();
  await detox.cleanup();
});

My workaround for detecting test is to set __DEV__ to true as a global in Jest config. But this isn't ideal.

How to get this to work?

Most helpful comment

So, actually I can access the environment variables established using react-native-config. What I had forgotten is that detox tests are actually running an actual .apk in the tests, and this apk is built with a particular environment file (this is in the react-native-config docs). I was changing the environment but wasn't rebuilding the apk for the tests.

>All comments

So, actually I can access the environment variables established using react-native-config. What I had forgotten is that detox tests are actually running an actual .apk in the tests, and this apk is built with a particular environment file (this is in the react-native-config docs). I was changing the environment but wasn't rebuilding the apk for the tests.

Was this page helpful?
0 / 5 - 0 ratings