I am working on a large ReactJS based website and need to write some test where I can defer loading of various modules because I need to have Sinon set up first.
The basic setup looks like this
const assert = require('chai').assert;
const sinon = require('sinon');
const reducerUtils = require('../../app/utils/reducerUtils');
const connectToReducerStub = sinon.stub(
reducerUtils,
'connectToReducer',
(stateMap, actionMap) => { console.log(`Connecting to reducer`); }
);
At this point I just need to import a component that tries to use reducerUtils.connectToReducer
that I've stubbed away, but my issue is that if I do like everywhere else in the codebase:
import MyComponent from '../../app/components/myComponent.jsx';
The import is run as the first thing (before everything else), making the import to- and call to reducerUtils.connectToReducer
take place before I get to stub it away.
On the other hand, trying to just defer it by using require
doesn't seem to work:
const MyComponent = require('../../app/components/myComponent.jsx');
What happens here is that MyComponent
is just undefined
.
I'm using ES6 classes to define my components along the lines of:
import react from 'react';
import connectToReducer from '../../utils/reducerUtils';
class MyComponent {
...
}
export connectToReducer(<stateMap>, <actionMap>)(MyComponent);
After having played around with trying to make require
work, I gave up and tried SystemJS as an alternative module loader, but it doesn't resolve paths the same way that require
does, so it seems that most module paths have to be set up by hand.