pke/eslint-plugin-redux-saga

Disable triggering in tests

aurbano opened this issue · 6 comments

While testing, I compare the output from each yield of the generator with put, call... functions in the test. But this is obviously triggering the rule:

expect(
  generator.next().value
).toEqual(
  put({ ... })
);

Has anyone else come across this issue? For now I'm disabling the rule on saga test files, but maybe something could be changed?

pke commented

Good question. Its kind of hard to support every test framework out there, so I guess for now the best option you have is to disable the rule in tests.

Having the same problem. In my opinion doesn't make sense to do this validation if the effect is not invoked within a generator function since it that case doesn't make sense to yield anything. A simple check to see if the enclosing function is actually a generator should do the trick.

I will add that this causes false positives for both yield-effects and no-unhandled-errors.

@gcazaciuc's suggestion to check that enclosing function is a generator would prevent 100% of our false positives.

If this solution is acceptable, could open a PR.

Here is another example (not in test code) of a false positive yield-effects that would be prevented by checking that wrapping block is a generator function

/* eslint-disable redux-saga/yield-effects */
const mapContextToLoadEffect = cond([
    [matches({currentUserRole: SELLER_ROLE}), constant(call(getAllSellerInfoSaga))],
    [matches({currentUserRole: BUYER_ROLE}),  constant(call(getAllBuyerInfoSaga))],
    [constant(true),                   constant(call(noop))],
])
/* eslint-enable redux-saga/yield-effects */

const GET_ALL_CURRENT_USER_INFO = 'hz-webapp/current-user/GET_ALL_CURRENT_USER_INFO'
export const getAllCurrentUserInfo = createAction(GET_ALL_CURRENT_USER_INFO)

export function* getAllCurrentUserInfoSaga() {
    const context = {
        currentUserRole: yield select(currentUserRoleSelector),
    }
    yield mapContextToLoadEffect(context)
}
pke commented

Sure, this makes sense and sends easy to fix. I'm in holidays now but PR welcome

pke commented

Please check the new 0.7.0 if it fixes your issue.