I am following the Detox documentation on the advance mocking with detox. I am doing so because I would like to mock up my api.js file which by default fetching data from a backend server. My fake api file which I named it api.e2e.js will just contain functions returning promised with json data. Unfortunately, the fake api does not kicks in. Below is my libraries used
I have tried to trigger the env variable through the metro and build command as well but with no luck.
action/restaurants.js
import Api from '../api';
export const getRestaurants = () => {
return (dispatch) => {
dispatch(gettingRestaurants());
Api.get.restaurants()
.then(resJson => {
console.log('get response', resJson);
dispatch(getRestrauntsSucces(resJson));
}).catch(error => {
console.log('response error', error)
dispatch(getRestaurantsFailure(error));
})
}
}
api.e2e.js
export default {
get: {
restaurants: () => {
console.log('you are in fake api');
return new Promise((resolve, reject) => {
resolve( [
{
id: 1,
name: 'Test Shop',
location: 'Johore',
category: 'Johore',
user_id: 1
}
])
})
},
}
}
rn-cli.config.js
module.exports = {
getSourceExts: () => process.env.RN_SRC_EXT ?
process.env.RN_SRC_EXT.split(',') : []
};
Expected the fake api will be called but still the original api is called.
You need to start your bundler with RN_SRC_EXT=e2e.js
before running your tests.
So the flow should be:
Run RN_SRC_EXT=e2e.js react-native start
, then detox tests
. You can also try building detox with RN_SRC_EXT=e2e.js
in package.json
:
"detox": {
"configurations": {
"ios.sim.debug": {
"binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/Glitz.app",
"build": " RN_SRC_EXT=e2e.js xcodebuild...YOUR BUILD",
"type": "ios.simulator",
"name": "iPhone X"
}
},
"test-runner": "jest"
}