Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6测试redux saga私有函数_Javascript_React Native_Ecmascript 6_Mocha.js_Redux Saga - Fatal编程技术网

Javascript ES6测试redux saga私有函数

Javascript ES6测试redux saga私有函数,javascript,react-native,ecmascript-6,mocha.js,redux-saga,Javascript,React Native,Ecmascript 6,Mocha.js,Redux Saga,我有一个react本地应用程序,希望在不导出私有函数的情况下测试我的redux传奇,下面是我的代码 // my saga file (login.js) function* login(action) { try { } catch (error) { } } export default function* loginWatcher() { yield takeLatest('SOME_ACTION_TYPE', login); } // my test file (l

我有一个react本地应用程序,希望在不导出私有函数的情况下测试我的redux传奇,下面是我的代码

// my saga file (login.js)
function* login(action) {
  try {

  } catch (error) {

  }
}

export default function* loginWatcher() {
  yield takeLatest('SOME_ACTION_TYPE', login);
}

// my test file (login.test.js)
describe('Login Saga', () => {
    describe('watch', () => {
        it('waits for the action', () => {
            const iterator = loginSaga();

            const expectedYield = takeLatest('SOME_ACTION_TYPE', login)
            const actualYield = iterator.next().value;

            expect(expectedYield).to.deep.equal(actualYield);
        });
    });
});
我不想导出saga文件中的登录函数。 我读过关于rewire的文章,但它似乎还不支持ES6

有没有什么我可以做的

更新

我将此代码添加到我的saga文件中,以便在测试中使用私有函数

let tests = {
  _login: null
}

if(process.env.NODE_ENV === "test") {
  tests._login = login;
};

export { tests };
我不知道这是否是最好的方法,因为我讨厌更改文件以便通过测试,但它是有效的


希望有另一种方法

您不想导出saga文件中的登录函数有什么特殊原因吗?没有理由导出它,它应该只在watcher函数执行操作时触发。导出它的一个原因是您可以测试它。您不应该只为测试而导出私有函数,这不是一个好的做法。不过,我更新的代码运行良好。不知道还有没有别的办法。