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
Hi community
I am encountering an issue during testing. I have an async function that is triggered by a timer. While I can count the calls to this async function itself, the counts for the functions within this async function are not updating, even though they are being called (as confirmed by the debugger)
Here some code example:
const CodeExample = () =>{
const {
closeDialog,
setContextData,
} = useDataContext()
const {
asyncPoller: { isAsyncPollerActive },
} = useTypedSelector((state) => ({ ...state }))
useEffectOnce(()=>{execute()})
const { execute, status } =
useGetData( <- counted well
async (result) => {
const mapResult = mapDataResult(result) <-cannot get count
if (isAsyncPollerActive) {
if (!isEqual(assets, mappedAssetsData)) { <- cannot get count
stopPoller() <-cannot get count
setContextData(mapResult) <- cannot get count
closeDialog && closeDialog()<- cannot get count
}
}
},
async (error) => {
if (isAsyncPollerActive) {
stopAsyncPoller() <- cannot get count
showErrorToast <-cannot get count
}
}
)
}
I tried next unit tests:
it('should call polling function and stop polling in case of backend was updated', async () => {
vi.spyOn(networkClient, 'get').mockResolvedValue(mockResponse)
mockDataContext.setContextData = setContextDataSpy
mockDataContext.closeDialog = closeDialogSpy,
vi.useFakeTimers()
renderWithProviders(
<Example />,
{
preloadedState: {
asyncPoller: {
isAsyncPollerActive: true,
pollerInterval: 1000,
pollerTimeout: 4000,
},
},
},
DataContextProvider,
true,
[{ pathname: ROUTES.REPORT_STATUS }]
)
vi.advanceTimersByTime(1000)
expect(getAnalysisBacklogAssetsSpy).toBeCalledTimes(1)
// vi.runAllTimers() <- tried, but does not helps
//vi.runAllTimersAsync() <- tried, but does not helps
//await new Promise(process.nextTick) <- tried but provides wrong counts
//expect(stopAsyncPollerSpy).toBeCalledTimes(1) <- wrong count or not called at all, depends on which above was used
// expect(setAssetsDataSpy).toBeCalledWith(mockAssets)
// expect(setAssetsDataSpy).toBeCalledTimes(1)) <- wrong count or not called at all, depends on which above was used
// expect(closeDialogSpy).toBeCalledTimes(1) <- wrong count or not called at all, depends on which above was used
//expect(closeDialogSpy).toBeCalledWith(undefined, mockAssets)
})```
I tried different solution as for the vitest and also from jest examples, but no luck
I am using next versions:
vitest 1.6.0
Will appreciate for some help if someone faced with same issue and can provide some feedback to fix it
Thanks
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi community
I am encountering an issue during testing. I have an async function that is triggered by a timer. While I can count the calls to this async function itself, the counts for the functions within this async function are not updating, even though they are being called (as confirmed by the debugger)
Here some code example:
I tried next unit tests:
Beta Was this translation helpful? Give feedback.
All reactions