Home > Mobile >  How to mock Config data of 'react-native-config' library in react native using Jest?
How to mock Config data of 'react-native-config' library in react native using Jest?

Time:11-24

Here I am trying to get the environment details to fetch the base url of the application. Here is the package being used.

// configfile.ts

import Config from 'react-native-config';

interface Setting {
  environment: string;
 
}
const settings: Setting = {
  environment: Config.ENV || '',

};

export default settings;

Now whenever I am trying to access settings.environment from my test file , it is returning empty string('') because the value of Config.ENV is undefined as I am not able to mock the value.

Excepted Result : Whenever I will call settings.environment , I should get 'test' as returned string value.

CodePudding user response:

Jest

For mocking the Config.ENV usage, create a folder __mocks__ at root of your project and create a file inside named react-native-config.js

inside the file paste below code

// react-native-config.js

export default {
  ENV: 'test', // here 'test' is expected value
};
  • Related