Add tests for invalid React server APIs (#59622)

Follow-up to #59621.

Adds tests for:

- findDOMNode
- flushSync
- unstable_batchedUpdates
- useFormStatus
- useFormState

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-1877
This commit is contained in:
Tim Neutkens 2023-12-15 10:12:46 +01:00 committed by GitHub
parent 05eb8114f9
commit 90bfbe72bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 1 deletions

View file

@ -0,0 +1,7 @@
import { findDOMNode } from 'react-dom'
console.log({ findDOMNode })
export default function Page() {
return null
}

View file

@ -0,0 +1,7 @@
import { flushSync } from 'react-dom'
console.log({ flushSync })
export default function Page() {
return null
}

View file

@ -0,0 +1,7 @@
import { unstable_batchedUpdates } from 'react-dom'
console.log({ unstable_batchedUpdates })
export default function Page() {
return null
}

View file

@ -0,0 +1,7 @@
import { useFormState } from 'react-dom'
console.log({ useFormState })
export default function Page() {
return null
}

View file

@ -0,0 +1,7 @@
import { useFormStatus } from 'react-dom'
console.log({ useFormStatus })
export default function Page() {
return null
}

View file

@ -263,6 +263,30 @@ describe('Error overlay - RSC build errors', () => {
})
}
const invalidReactDomServerApis = [
'findDOMNode',
'flushSync',
'unstable_batchedUpdates',
'useFormStatus',
'useFormState',
]
for (const api of invalidReactDomServerApis) {
it(`should error when ${api} from react-dom is used in server component`, async () => {
const { session, cleanup } = await sandbox(
next,
undefined,
`/server-with-errors/react-dom-apis/${api.toLowerCase()}`
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toInclude(
`You're importing a component that needs ${api}. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
)
await cleanup()
})
}
it('should allow to use and handle rsc poisoning server-only', async () => {
const { session, cleanup } = await sandbox(
next,

View file

@ -1137,7 +1137,12 @@
"Error overlay - RSC build errors should error when useState from react is used in server component",
"Error overlay - RSC build errors should error when useSyncExternalStore from react is used in server component",
"Error overlay - RSC build errors should error when useTransition from react is used in server component",
"Error overlay - RSC build errors should error when useOptimistic from react is used in server component"
"Error overlay - RSC build errors should error when useOptimistic from react is used in server component",
"Error overlay - RSC build errors should error when findDOMNode from react-dom is used in server component",
"Error overlay - RSC build errors should error when flushSync from react-dom is used in server component",
"Error overlay - RSC build errors should error when unstable_batchedUpdates from react-dom is used in server component",
"Error overlay - RSC build errors should error when useFormStatus from react-dom is used in server component",
"Error overlay - RSC build errors should error when useFormState from react-dom is used in server component"
],
"pending": [
"Error overlay - RSC build errors should throw an error when getStaticProps is used"