Skip to content

Commit 7d7a21c

Browse files
test(react-query): replace 'async/await sleep' with 'sleep().then()' in test 'queryFn' and 'mutationFn' (#10399)
* test(react-query): replace 'async/await sleep' with 'sleep().then()' in test 'queryFn' and 'mutationFn' * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent ef2a4e3 commit 7d7a21c

5 files changed

Lines changed: 73 additions & 269 deletions

File tree

packages/react-query/src/__tests__/fine-grained-persister.test.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ describe('fine grained persister', () => {
7979
it('should restore query state from persister and refetch', async () => {
8080
const key = queryKey()
8181
const hash = hashKey(key)
82-
const spy = vi.fn(async () => {
83-
await sleep(5)
84-
85-
return 'Works from queryFn'
86-
})
82+
const spy = vi.fn(() => sleep(5).then(() => 'Works from queryFn'))
8783

8884
const mapStorage = new Map()
8985
const storage = {

packages/react-query/src/__tests__/useMutation.test.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ describe('useMutation', () => {
157157
return Promise.reject(new Error('Error test Jonas'))
158158
})
159159

160-
mutateFn.mockImplementation(async (value) => {
161-
await sleep(10)
162-
return Promise.resolve(value)
163-
})
160+
mutateFn.mockImplementation((value) =>
161+
sleep(10).then(() => Promise.resolve(value)),
162+
)
164163

165164
function Page() {
166165
const { mutate, failureCount, failureReason, data, status } = useMutation(
@@ -369,10 +368,7 @@ describe('useMutation', () => {
369368
const key = queryKey()
370369

371370
queryClient.setMutationDefaults(key, {
372-
mutationFn: async (text: string) => {
373-
await sleep(10)
374-
return text
375-
},
371+
mutationFn: (text: string) => sleep(10).then(() => text),
376372
})
377373

378374
const states: Array<UseMutationResult<any, any, any, any>> = []
@@ -1033,10 +1029,7 @@ describe('useMutation', () => {
10331029

10341030
function Page() {
10351031
const mutation = useMutation({
1036-
mutationFn: async (_text: string) => {
1037-
await sleep(10)
1038-
return 'result'
1039-
},
1032+
mutationFn: (_text: string) => sleep(10).then(() => 'result'),
10401033
onSuccess: () => Promise.reject(error),
10411034
onError,
10421035
})

packages/react-query/src/__tests__/usePrefetchQuery.test.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ import type { UseSuspenseQueryOptions } from '..'
1717
const generateQueryFn = (data: string) =>
1818
vi
1919
.fn<(...args: Array<any>) => Promise<string>>()
20-
.mockImplementation(async () => {
21-
await sleep(10)
22-
23-
return data
24-
})
20+
.mockImplementation(() => sleep(10).then(() => data))
2521

2622
describe('usePrefetchQuery', () => {
2723
let queryCache: QueryCache

packages/react-query/src/__tests__/useQueries.test.tsx

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,11 @@ describe('useQueries', () => {
5454
queries: [
5555
{
5656
queryKey: key1,
57-
queryFn: async () => {
58-
await sleep(10)
59-
return 1
60-
},
57+
queryFn: () => sleep(10).then(() => 1),
6158
},
6259
{
6360
queryKey: key2,
64-
queryFn: async () => {
65-
await sleep(200)
66-
return 2
67-
},
61+
queryFn: () => sleep(200).then(() => 2),
6862
},
6963
],
7064
})
@@ -1093,17 +1087,11 @@ describe('useQueries', () => {
10931087
queries: [
10941088
{
10951089
queryKey: key1,
1096-
queryFn: async () => {
1097-
await sleep(5)
1098-
return Promise.resolve('query1')
1099-
},
1090+
queryFn: () => sleep(5).then(() => Promise.resolve('query1')),
11001091
},
11011092
{
11021093
queryKey: key2,
1103-
queryFn: async () => {
1104-
await sleep(20)
1105-
return Promise.resolve('query2')
1106-
},
1094+
queryFn: () => sleep(20).then(() => Promise.resolve('query2')),
11071095
},
11081096
],
11091097
combine: ([query1, query2]) => {
@@ -1138,17 +1126,13 @@ describe('useQueries', () => {
11381126
queries: [
11391127
{
11401128
queryKey: key1,
1141-
queryFn: async () => {
1142-
await sleep(5)
1143-
return Promise.resolve('first result ' + count)
1144-
},
1129+
queryFn: () =>
1130+
sleep(5).then(() => Promise.resolve('first result ' + count)),
11451131
},
11461132
{
11471133
queryKey: key2,
1148-
queryFn: async () => {
1149-
await sleep(50)
1150-
return Promise.resolve('second result ' + count)
1151-
},
1134+
queryFn: () =>
1135+
sleep(50).then(() => Promise.resolve('second result ' + count)),
11521136
},
11531137
],
11541138
combine: (queryResults) => {
@@ -1330,17 +1314,11 @@ describe('useQueries', () => {
13301314
queries: [
13311315
{
13321316
queryKey: key1,
1333-
queryFn: async () => {
1334-
await sleep(10)
1335-
return 'first result:' + value
1336-
},
1317+
queryFn: () => sleep(10).then(() => 'first result:' + value),
13371318
},
13381319
{
13391320
queryKey: key2,
1340-
queryFn: async () => {
1341-
await sleep(20)
1342-
return 'second result:' + value
1343-
},
1321+
queryFn: () => sleep(20).then(() => 'second result:' + value),
13441322
},
13451323
],
13461324
combine: React.useCallback((results: Array<QueryObserverResult>) => {
@@ -1414,17 +1392,11 @@ describe('useQueries', () => {
14141392
queries: [
14151393
{
14161394
queryKey: [key1],
1417-
queryFn: async () => {
1418-
await sleep(10)
1419-
return 'first result'
1420-
},
1395+
queryFn: () => sleep(10).then(() => 'first result'),
14211396
},
14221397
{
14231398
queryKey: [key2],
1424-
queryFn: async () => {
1425-
await sleep(20)
1426-
return 'second result'
1427-
},
1399+
queryFn: () => sleep(20).then(() => 'second result'),
14281400
},
14291401
],
14301402
combine: React.useCallback(
@@ -1538,24 +1510,15 @@ describe('useQueries', () => {
15381510
queries: [
15391511
{
15401512
queryKey: [key1],
1541-
queryFn: async () => {
1542-
await sleep(10)
1543-
return 'first result'
1544-
},
1513+
queryFn: () => sleep(10).then(() => 'first result'),
15451514
},
15461515
{
15471516
queryKey: [key2],
1548-
queryFn: async () => {
1549-
await sleep(15)
1550-
return 'second result'
1551-
},
1517+
queryFn: () => sleep(15).then(() => 'second result'),
15521518
},
15531519
{
15541520
queryKey: [key3],
1555-
queryFn: async () => {
1556-
await sleep(20)
1557-
return 'third result'
1558-
},
1521+
queryFn: () => sleep(20).then(() => 'third result'),
15591522
},
15601523
],
15611524
combine: (results) => {
@@ -1600,24 +1563,15 @@ describe('useQueries', () => {
16001563
queries: [
16011564
{
16021565
queryKey: [key1],
1603-
queryFn: async () => {
1604-
await sleep(10)
1605-
return 'first result'
1606-
},
1566+
queryFn: () => sleep(10).then(() => 'first result'),
16071567
},
16081568
{
16091569
queryKey: [key2],
1610-
queryFn: async () => {
1611-
await sleep(15)
1612-
return 'second result'
1613-
},
1570+
queryFn: () => sleep(15).then(() => 'second result'),
16141571
},
16151572
{
16161573
queryKey: [key3],
1617-
queryFn: async () => {
1618-
await sleep(20)
1619-
return 'third result'
1620-
},
1574+
queryFn: () => sleep(20).then(() => 'third result'),
16211575
},
16221576
],
16231577
combine: (results) => {
@@ -1679,17 +1633,11 @@ describe('useQueries', () => {
16791633
queries: [
16801634
{
16811635
queryKey: key1,
1682-
queryFn: async () => {
1683-
await sleep(10)
1684-
return 'first result'
1685-
},
1636+
queryFn: () => sleep(10).then(() => 'first result'),
16861637
},
16871638
{
16881639
queryKey: key2,
1689-
queryFn: async () => {
1690-
await sleep(20)
1691-
return 'second result'
1692-
},
1640+
queryFn: () => sleep(20).then(() => 'second result'),
16931641
},
16941642
],
16951643
combine: React.useCallback((results: Array<QueryObserverResult>) => {

0 commit comments

Comments
 (0)