JavaScript doesn't want to support mutual recursion within our testing environment. So, the below function fails while running our test suite.
const selectPersistedContext = (state: RootState) => {
const advisors = selectAdvisors(state); // selectAdvisors undefined
const context = selectPersistedContext(state); // selectPersistedContext undefined
const { fmid } = context;
if (fmid) {
const advisor = advisors.find(advisor => advisor.fmid === fmid);
return advisor || Advisor.create({ fmid });
}
return undefined;
};It fails because selectAdvisors and selectPersistedContext come from different files, which JavaScript believes to imply mutual recursion (which it thinks it doesn't support) and so it fails.
The problem can be eleviated by delaying supplying the selectAdvisors and selectPersistedContext. Like so:
const gradualSelectPersistedAdvisor = (
selectAdvisors: Function,
selectPersistedContext: Function,
) => (state: RootState) => {
const advisors = selectAdvisors(state);
const context = selectPersistedContext(state);
const { fmid } = context;
if (fmid) {
const advisor = advisors.find(advisor => advisor.fmid === fmid);
return advisor || Advisor.create({ fmid });
}
return undefined;
};where then the actual selectAdvisors and selectPersistedContext functions can be pulled in from the test file using require.requireActual or mocking them out.
The old selector function can then be written easily like so:
const selectPersistedAdvisor = gradualSelectPersistedAdvisor(
selectAdvisors,
selectPersistedContext,
);