How Do I Stub webpack's require.ensure? How Do I Stub webpack's require.ensure? reactjs reactjs

How Do I Stub webpack's require.ensure?


Each module has its own instance of require so the only way to mock require.ensure is to have some kind of abstraction around require to get this unique require from the required module in test and then add a mock of ensure() to that require instance.

You could use babel-plugin-rewire and use getter to get require, like

const require = myComponent.__get__('require');require.ensure = () => { /* mock here */};

I'm not 100% sure that it will work but definitely I would try to go in this direction. I recommend reading this issue on github which is related to your problem and explains a lot.