Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7abbac4

Browse files
committed
Add support for async toWarnDev scope
1 parent af23b46 commit 7abbac4

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

‎src/__tests__/render.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ test('hydrate can have a wrapper', async () => {
162162
expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(1)
163163
})
164164

165-
test('legacyRoot uses legacy ReactDOM.render', () => {
166-
expect(async () => {
165+
test('legacyRoot uses legacy ReactDOM.render', async() => {
166+
awaitexpect(async () => {
167167
await render(<div />, {legacyRoot: true})
168168
}).toErrorDev(
169169
[
@@ -173,11 +173,11 @@ test('legacyRoot uses legacy ReactDOM.render', () => {
173173
)
174174
})
175175

176-
test('legacyRoot uses legacy ReactDOM.hydrate', () => {
176+
test('legacyRoot uses legacy ReactDOM.hydrate', async() => {
177177
const ui = <div />
178178
const container = document.createElement('div')
179179
container.innerHTML = ReactDOMServer.renderToString(ui)
180-
expect(async () => {
180+
awaitexpect(async () => {
181181
await render(ui, {container, hydrate: true, legacyRoot: true})
182182
}).toErrorDev(
183183
[

‎src/__tests__/renderHook.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ test('allows wrapper components', async () => {
6565
expect(result.current).toEqual('provided')
6666
})
6767

68-
test('legacyRoot uses legacy ReactDOM.render', () => {
68+
test('legacyRoot uses legacy ReactDOM.render', async() => {
6969
const Context = React.createContext('default')
7070
function Wrapper({children}) {
7171
return <Context.Provider value="provided">{children}</Context.Provider>
7272
}
7373
let result
74-
expect(async () => {
74+
awaitexpect(async () => {
7575
result = (
7676
await renderHook(
7777
() => {

‎tests/toWarnDev.js‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ const createMatcherFor = (consoleMethod, matcherName) =>
181181
// Avoid using Jest's built-in spy since it can't be removed.
182182
console[consoleMethod] = consoleSpy
183183

184-
try {
185-
callback()
186-
} catch (error) {
187-
caughtError = error
188-
} finally {
184+
const onFinally = () => {
189185
// Restore the unspied method so that unexpected errors fail tests.
190186
console[consoleMethod] = originalMethod
191187

@@ -289,11 +285,56 @@ const createMatcherFor = (consoleMethod, matcherName) =>
289285

290286
return {pass: true}
291287
}
288+
289+
let returnPromise = null
290+
try {
291+
const result = callback()
292+
293+
if (
294+
typeof result === 'object' &&
295+
result !== null &&
296+
typeof result.then === 'function'
297+
) {
298+
// `act` returns a thenable that can't be chained.
299+
// Once `act(async () => {}).then(() => {}).then(() => {})` works
300+
// we can just return `result.then(onFinally, error => ...)`
301+
returnPromise = new Promise((resolve, reject) => {
302+
result
303+
.then(
304+
() => {
305+
resolve(onFinally())
306+
},
307+
error => {
308+
caughtError = error
309+
return resolve(onFinally())
310+
},
311+
)
312+
// In case onFinally throws we need to reject from this matcher
313+
.catch(error => {
314+
reject(error)
315+
})
316+
})
317+
}
318+
} catch (error) {
319+
caughtError = error
320+
} finally {
321+
return returnPromise === null ? onFinally() : returnPromise
322+
}
292323
} else {
293324
// Any uncaught errors or warnings should fail tests in production mode.
294-
callback()
325+
constresult=callback()
295326

296-
return {pass: true}
327+
if (
328+
typeof result === 'object' &&
329+
result !== null &&
330+
typeof result.then === 'function'
331+
) {
332+
return result.then(() => {
333+
return {pass: true}
334+
})
335+
} else {
336+
return {pass: true}
337+
}
297338
}
298339
}
299340

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /