Open lidoravitan opened 4 years ago
Hello,
It looks like you run into the same issue as me. It is related to async dispatch from logic component. If you are interesting, I have a redux-logic fork with some changes that supports dispatching action async and fixes issues related to wrong behavior of hooks. Also there are a lot of unit tests that may be helpful for you. just use this line in your package.json (instead standard redux-logic) for check - does it help: "@rmaenk/redux-logic": "git://github.com/rmaenk/redux-logic.git#v2.1.1-arfph.6",
sample of test: https://github.com/rmaenk/redux-logic/blob/async-ready-for-process-hook/test/createLogicMiddleware-process-async.spec.js. See the next test "describe(`first logic with delayed action and done" for interrupting infinite test the next function is used:
/**
* Interrupts async test hooks
* @param {Function} hDone done callback of mocha test hook
* @param {number} timeout time is ms for waiting before interrupt a mocha test hook
* @param {object} mw redux-logic middleware,
* when specified then it expected that test hook will complete
* using mw.whenComplete before interrupting by timeout.
* @returns {void}
*/
function interrupt(hDone, timeout, mw) {
// used to avoid double call of hDone callback
let interrupted = false;
const timer = setTimeout(() => {
interrupted = true;
hDone();
}, timeout);
if (mw) {
mw.whenComplete(() => {
// avoids double call of hDone callback
clearTimeout(timer);
if (!interrupted) {
hDone();
}
});
}
}
Regards, Roman
I created logic to verify something after 10 seconds, for instance:
as you can see I added cancelType which allows to cancel this logic before it reaches 10sec
I'm using Jest and redux-logic-test to test logics, However, there are two issues I ran into.
First, because
whenComplete
resolved only whendone()
is called. This means that the test should wait until the timers is over and only then calldone()
(at least 10 sec per unit-testing).Ok, I know!...why don't use
jest.useFakeTimers()
? well, it looks like there's some problem withjest.useFakeTimers
and some of the timersRxJs
operators.Anyway,
useFakeTimers
causes the test freeze until you invokejest.runAllTimers()
(it make sense why this happens, becausejest.useFakeTimers()
freeze all timers until you calljest.runAllTimers()
or some another jest timer helpers) but now the test totally freezes because of the real timer(from the logic implementation) which wrap byjest.useFakeTimers()
but created after the firstjest.runAllTimers()
so nothing happen.Second, it related to the same problem with whenComplete which resolved only when
done()
is called. so imagine that I want to test cancelType, what I expect for?a. create
createMockStore
, b. dispatch 'VALIDATE_SOMETHING', c. dispatch CANCEL_VALIDATE_SOMETHINGbut when you dispatch the first action(in this case VALIDATE_SOMETHING),
whenComplete
wait until the done() will call and then resolved. it means you have to wait until the timer finish so you never cancel the logic in test.Test code:
when using
jest.useFakeTimers()
andjest.runAllTimers()
getting this error