Skip to content

Commit 9f296b1

Browse files
authored
Merge branch 'main' into react-18-upgrade
2 parents 274932b + 4a03704 commit 9f296b1

File tree

9 files changed

+68
-40
lines changed

9 files changed

+68
-40
lines changed

.all-contributorsrc

+9
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,15 @@
584584
"contributions": [
585585
"test"
586586
]
587+
},
588+
{
589+
"login": "masious",
590+
"name": "Masious",
591+
"avatar_url": "https://avatars.githubusercontent.com/u/6429009?v=4",
592+
"profile": "https://www.facebook.com/masoud.bonabi",
593+
"contributions": [
594+
"doc"
595+
]
587596
}
588597
],
589598
"skipCi": true,

.github/workflows/validate.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
run: npm run validate
4848

4949
- name: ⬆️ Upload coverage report
50-
uses: codecov/[email protected].2
50+
uses: codecov/[email protected].3
5151

5252
release:
5353
needs: main

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
248248
<td align="center"><a href="https://github.com/bdwain"><img src="https://avatars.githubusercontent.com/u/3982094?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bryan Wain</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/issues?q=author%3Abdwain" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/react-hooks-testing-library/pulls?q=is%3Apr+reviewed-by%3Abdwain" title="Reviewed Pull Requests">👀</a></td>
249249
<td align="center"><a href="https://github.com/snowystinger"><img src="https://avatars.githubusercontent.com/u/698229?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Robert Snow</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=snowystinger" title="Tests">⚠️</a></td>
250250
<td align="center"><a href="https://github.com/chris110408"><img src="https://avatars.githubusercontent.com/u/10645051?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Chris Chen</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=chris110408" title="Tests">⚠️</a></td>
251+
<td align="center"><a href="https://www.facebook.com/masoud.bonabi"><img src="https://avatars.githubusercontent.com/u/6429009?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Masious</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=masious" title="Documentation">📖</a></td>
251252
</tr>
252253
</table>
253254

docs/api-reference.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The `renderHook` function returns an object that has the following properties:
6868
}
6969
```
7070

71-
The `current` value or the `result` will reflect the latest of whatever is returned from the
71+
The `current` value of the `result` will reflect the latest of whatever is returned from the
7272
`callback` passed to `renderHook`. Any thrown values from the latest call will be reflected in the
7373
`error` value of the `result`. The `all` value is an array containing all the returns (including the
7474
most recent) from the callback. These could be `result` or an `error` depending on what the callback

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
"react-error-boundary": "^3.1.0"
5757
},
5858
"devDependencies": {
59-
"@typescript-eslint/eslint-plugin": "4.28.5",
60-
"@typescript-eslint/parser": "4.28.5",
59+
"@typescript-eslint/eslint-plugin": "4.30.0",
60+
"@typescript-eslint/parser": "4.30.0",
6161
"all-contributors-cli": "6.20.0",
6262
"codecov": "3.8.3",
6363
"cross-env": "^7.0.3",
@@ -71,8 +71,8 @@
7171
"react": "17.0.2",
7272
"react-dom": "17.0.2",
7373
"react-test-renderer": "17.0.2",
74-
"ts-node": "10.1.0",
75-
"typescript": "4.3.5"
74+
"ts-node": "10.2.1",
75+
"typescript": "4.4.2"
7676
},
7777
"peerDependencies": {
7878
"react": ">=16.9.0",

src/__tests__/utils/runForRenderers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const rendererResolvers = {
5858
native: () => requireRenderer('native'),
5959
server: () => requireRenderer<ReactHooksServerRenderer>('server'),
6060
'default/pure': () => requireRenderer('default/pure'),
61-
'dom/pure': () => requireRenderer('default/pure'),
62-
'native/pure': () => requireRenderer('default/pure'),
61+
'dom/pure': () => requireRenderer('dom/pure'),
62+
'native/pure': () => requireRenderer('native/pure'),
6363
'server/pure': () => requireRenderer<ReactHooksServerRenderer>('server/pure'),
6464
'server/hydrated': () => hydratedServerRenderer('server'),
6565
'server/hydrated/pure': () => hydratedServerRenderer('server/pure')

src/core/asyncUtils.ts

+11-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
AsyncUtils
88
} from '../types'
99

10-
import { resolveAfter, callAfter } from '../helpers/promises'
10+
import { createTimeoutController } from '../helpers/createTimeoutController'
1111
import { TimeoutError } from '../helpers/error'
1212

1313
const DEFAULT_INTERVAL = 50
@@ -20,37 +20,26 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
2020
return callbackResult ?? callbackResult === undefined
2121
}
2222

23+
const timeoutSignal = createTimeoutController(timeout)
24+
2325
const waitForResult = async () => {
2426
while (true) {
25-
await Promise.race(
26-
[
27-
new Promise<void>((resolve) => addResolver(resolve)),
28-
interval && resolveAfter(interval)
29-
].filter(Boolean)
30-
)
31-
32-
if (checkResult()) {
27+
const intervalSignal = createTimeoutController(interval)
28+
timeoutSignal.onTimeout(() => intervalSignal.cancel())
29+
30+
await intervalSignal.wrap(new Promise<void>(addResolver))
31+
32+
if (checkResult() || timeoutSignal.timedOut) {
3333
return
3434
}
3535
}
3636
}
3737

38-
let timedOut = false
39-
4038
if (!checkResult()) {
41-
if (timeout) {
42-
const timeoutPromise = () =>
43-
callAfter(() => {
44-
timedOut = true
45-
}, timeout)
46-
47-
await act(() => Promise.race([waitForResult(), timeoutPromise()]))
48-
} else {
49-
await act(waitForResult)
50-
}
39+
await act(() => timeoutSignal.wrap(waitForResult()))
5140
}
5241

53-
return !timedOut
42+
return !timeoutSignal.timedOut
5443
}
5544

5645
const waitFor = async (
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { WaitOptions } from '../types'
2+
3+
function createTimeoutController(timeout: WaitOptions['timeout']) {
4+
let timeoutId: NodeJS.Timeout
5+
const timeoutCallbacks: Array<() => void> = []
6+
7+
const timeoutController = {
8+
onTimeout(callback: () => void) {
9+
timeoutCallbacks.push(callback)
10+
},
11+
wrap(promise: Promise<void>) {
12+
return new Promise<void>((resolve, reject) => {
13+
timeoutController.timedOut = false
14+
timeoutController.onTimeout(resolve)
15+
16+
if (timeout) {
17+
timeoutId = setTimeout(() => {
18+
timeoutController.timedOut = true
19+
timeoutCallbacks.forEach((callback) => callback())
20+
resolve()
21+
}, timeout)
22+
}
23+
24+
promise
25+
.then(resolve)
26+
.catch(reject)
27+
.finally(() => timeoutController.cancel())
28+
})
29+
},
30+
cancel() {
31+
clearTimeout(timeoutId)
32+
},
33+
timedOut: false
34+
}
35+
36+
return timeoutController
37+
}
38+
39+
export { createTimeoutController }

src/helpers/promises.ts

-10
This file was deleted.

0 commit comments

Comments
 (0)