Followed Detox Mock Implementation but not working

非 Y 不嫁゛ 提交于 2020-01-02 07:06:30

问题


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

  1. react-native 0.57.8
  2. detox 9.1.2

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.


回答1:


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"
    }



回答2:


This is what i do to get the mock files working.

  1. Re-run the bundler by resetting the cache.

If still not ok I will

  1. Delete the build files and rebuild the whole project.


来源:https://stackoverflow.com/questions/54363389/followed-detox-mock-implementation-but-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!