-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(act): Support async act 🎉 #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {asyncAct} from '../act-compat' | ||
|
||
jest.mock('react-dom/test-utils', () => ({ | ||
act: cb => { | ||
const promise = cb() | ||
return { | ||
then() { | ||
console.error('blah, do not do this') | ||
return promise | ||
}, | ||
} | ||
}, | ||
})) | ||
|
||
test('async act works even when the act is an old one', async () => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
const callback = jest.fn() | ||
await asyncAct(async () => { | ||
await Promise.resolve() | ||
await callback() | ||
}) | ||
expect(console.error.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"It looks like you're using a version of react-dom that supports the \\"act\\" function, but not an awaitable version of \\"act\\" which you will need. Please upgrade to at least [email protected] to remove this warning.", | ||
], | ||
] | ||
`) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
// and it doesn't warn you twice | ||
callback.mockClear() | ||
console.error.mockClear() | ||
|
||
await asyncAct(async () => { | ||
await Promise.resolve() | ||
await callback() | ||
}) | ||
expect(console.error).toHaveBeenCalledTimes(0) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
console.error.mockRestore() | ||
}) | ||
|
||
/* eslint no-console:0 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,23 @@ import React from 'react' | |
import ReactDOM from 'react-dom' | ||
|
||
let reactAct | ||
let actSupported = false | ||
let asyncActSupported = false | ||
try { | ||
reactAct = require('react-dom/test-utils').act | ||
actSupported = reactAct !== undefined | ||
|
||
const originalError = console.error | ||
let errorCalled = false | ||
console.error = () => { | ||
errorCalled = true | ||
} | ||
console.error.calls = [] | ||
reactAct(() => ({then: () => {}})).then(/* istanbul ignore next */ () => {}) | ||
if (!errorCalled) { | ||
asyncActSupported = true | ||
} | ||
console.error = originalError | ||
} catch (error) { | ||
// ignore, this is to support old versions of react | ||
} | ||
|
@@ -19,8 +34,28 @@ function actPolyfill(cb) { | |
|
||
const act = reactAct || actPolyfill | ||
|
||
function rtlAct(...args) { | ||
return act(...args) | ||
let youHaveBeenWarned = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😂 |
||
// this will not avoid warnings that react-dom 16.8.0 logs for triggering | ||
// state updates asynchronously, but at least we can tell people they need | ||
// to upgrade to avoid the warnings. | ||
async function asyncActPolyfill(cb) { | ||
if (!youHaveBeenWarned && actSupported) { | ||
// if act is supported and async act isn't and they're trying to use async | ||
// act, then they need to upgrade from 16.8 to 16.9. | ||
// This is a seemless upgrade, so we'll add a warning | ||
console.error( | ||
alexkrolick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least [email protected] to remove this warning.`, | ||
) | ||
youHaveBeenWarned = true | ||
} | ||
await cb() | ||
// make all effects resolve after | ||
act(() => {}) | ||
} | ||
|
||
export default rtlAct | ||
const asyncAct = asyncActSupported ? reactAct : asyncActPolyfill | ||
|
||
export default act | ||
export {asyncAct} | ||
|
||
/* eslint no-console:0 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,13 @@ import { | |
getQueriesForElement, | ||
prettyDOM, | ||
fireEvent as dtlFireEvent, | ||
configure as configureDTL, | ||
} from 'dom-testing-library' | ||
import act from './act-compat' | ||
import act, {asyncAct} from './act-compat' | ||
|
||
configureDTL({ | ||
asyncWrapper: asyncAct, | ||
}) | ||
|
||
const mountedContainers = new Set() | ||
|
||
|
@@ -133,4 +138,8 @@ fireEvent.select = (node, init) => { | |
export * from 'dom-testing-library' | ||
export {render, cleanup, fireEvent, act} | ||
|
||
// NOTE: we're not going to export asyncAct because that's our own compatibility | ||
// thing for people using [email protected]. Anyone else doesn't need it and | ||
// people should just upgrade anyway. | ||
|
||
/* eslint func-name-matching:0 */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😘 @threepointone