You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 when done() is called. This means that the test should wait until the timers is over and only then call done() (at least 10 sec per unit-testing).
Ok, I know!...why don't use jest.useFakeTimers()? well, it looks like there's some problem with jest.useFakeTimers and some of the timers RxJs operators.
Anyway, useFakeTimers causes the test freeze until you invoke jest.runAllTimers() (it make sense why this happens, because jest.useFakeTimers() freeze all timers until you call jest.runAllTimers() or some another jest timer helpers) but now the test totally freezes because of the real timer(from the logic implementation) which wrap by jest.useFakeTimers() but created after the first jest.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_SOMETHING
but 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:
describe('test: unit tests',()=>{test('should run properly',async()=>{jest.useFakeTimers()conststore=createMockStore({logic: [validateSomething],reducer: rootReducer})store.dispatch({type: 'VALIDATE_SOMETING'})jest.runAllTimers()constexpectedActions=[]awaitstore.whenComplete()expect([]).toEqual(expectedActions)})})
when using jest.useFakeTimers() and jest.runAllTimers() getting this error
javascript console.error node_modules/redux-logic/build-lib/createCancelled$.js:74
warning: logic (validateSegmentsPushDataArrivedBeforeTimeoutLogic) is still running after 60s, forget
to call done()? For non-ending logic, set warnTimeout: 0```
The text was updated successfully, but these errors were encountered:
lidoravitan
changed the title
can't test cancelType and/or Timers
can't test Timers and/or cancelType
Feb 13, 2020
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",
/**
* 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();
}
});
}
}
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_SOMETHING
but 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 errorThe text was updated successfully, but these errors were encountered: