Skip to content
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

Add tests for invalid React server APIs #59622

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { findDOMNode } from 'react-dom'

console.log({ findDOMNode })

export default function Page() {
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { flushSync } from 'react-dom'

console.log({ flushSync })

export default function Page() {
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { unstable_batchedUpdates } from 'react-dom'

console.log({ unstable_batchedUpdates })

export default function Page() {
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useFormState } from 'react-dom'

console.log({ useFormState })

export default function Page() {
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useFormStatus } from 'react-dom'

console.log({ useFormStatus })

export default function Page() {
return null
}
24 changes: 24 additions & 0 deletions test/development/acceptance-app/rsc-build-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down