@@ -1267,42 +1267,27 @@ describe('createQuery', () => {
12671267 } ) ,
12681268 )
12691269
1270- it . todo (
1270+ it (
12711271 'should be able to set different stale times for a query' ,
12721272 async ( ) => {
1273- /**
1274- * TODO: There's a super weird bug with this test, and I think it's caused by a race condition in query-core.
1275- *
1276- * If you add this to the top `updateResult` in `packages/query-core/src/queryObserver.ts:647`:
1277- * ```
1278- * for (let i = 0; i < 10_000_000; i++) {
1279- * continue
1280- * }
1281- * ```
1282- *
1283- * This test will miraculously start to pass. I'm suspicious that there's some race condition between props
1284- * being tracked and `updateResult` being called, but that _should_ be fixed by `notifyOnChangeProps: 'all'`,
1285- * and that's not doing anything.
1286- *
1287- * This test will also start to magically pass if you put `$inspect(firstQuery)` before `vi.waitFor` near
1288- * the end of the test.
1289- */
1290-
12911273 const key = [ 'test-key' ]
12921274 const states1 : Array < CreateQueryResult < string > > = [ ]
12931275 const states2 : Array < CreateQueryResult < string > > = [ ]
12941276
12951277 // Prefetch the query
1296- await queryClient . prefetchQuery ( {
1278+ const prefetchPromise = queryClient . prefetchQuery ( {
12971279 queryKey : key ,
12981280 queryFn : async ( ) => {
12991281 await sleep ( 10 )
13001282 return 'prefetch'
13011283 } ,
13021284 } )
1303-
13041285 await vi . advanceTimersByTimeAsync ( 10 )
1286+ await prefetchPromise
1287+
13051288 expect ( queryClient . getQueryState ( key ) ?. data ) . toBe ( 'prefetch' )
1289+ // Advance time so secondQuery (staleTime: 10) sees prefetched data as stale
1290+ await vi . advanceTimersByTimeAsync ( 11 )
13061291
13071292 await withEffectRoot ( async ( ) => {
13081293 const firstQuery = createQuery < string > (
@@ -1331,6 +1316,7 @@ describe('createQuery', () => {
13311316 states2 . push ( { ...secondQuery } )
13321317 } )
13331318
1319+ // Wait for both staleTime to expire (100ms for firstQuery)
13341320 await vi . advanceTimersByTimeAsync ( 101 )
13351321 expect ( firstQuery ) . toMatchObject ( { data : 'two' , isStale : true } )
13361322 expect ( secondQuery ) . toMatchObject ( { data : 'two' , isStale : true } )
0 commit comments