Skip to content
Open
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
5 changes: 5 additions & 0 deletions contributor-docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Browser locator pilot](#browser-locator-pilot)
- [Snapshots](#snapshots)
- [Running Tests](#running-tests)
- [React Server Component Tests](#react-server-component-tests)
- [Interaction Tests](#interaction-tests)
- [As A Part Of Unit Tests](#as-a-part-of-unit-tests)
- [Visual Regression Tests](#visual-regression-tests)
Expand Down Expand Up @@ -124,6 +125,10 @@ We are slowly moving away from using snapshots as a way to test visual changes o

By default, Vitest fails when tests emit unexpected console output in CI. For local debugging, it is disabled unless you opt in with `VITEST_FAIL_ON_CONSOLE=true`.

### React Server Component Tests

Tests named `*.rsc.test.tsx` render components inside a React Server Component boundary using [`vitest-plugin-rsc`](https://github.com/storybookjs/vitest-plugin-rsc). They run in their own `@primer/react (rsc)` Vitest project (`packages/react/vitest.config.rsc.mts`) because the plugin needs browser mode, and they are excluded from the main browser project. Server Components require React 19, so the project has no tests when React 18 is installed and runs in the React 19 CI job.

## Interaction Tests

### As A Part Of Unit Tests
Expand Down
163 changes: 161 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/react/config/vitest/rsc/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {beforeAll, beforeEach} from 'vitest'
import {cleanup, initialize} from 'vitest-plugin-rsc/testing-library'

beforeAll(() => {
initialize()
})

beforeEach(async () => {
await cleanup()
})
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
"unist-util-find": "3.0.0",
"unist-util-find-before": "4.0.0",
"unist-util-flat-filter": "2.0.0",
"vitest-plugin-rsc": "^0.3.0",
"yaml": "2.7.0"
},
"peerDependencies": {
Expand Down
34 changes: 34 additions & 0 deletions packages/react/src/__tests__/components.rsc.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {describe, expect, test} from 'vitest'
import {page} from 'vitest/browser'
import {renderServer} from 'vitest-plugin-rsc/testing-library'
import {Heading, Text} from '../index'
import {Counter} from './rsc-fixtures/Counter'

// `src/index.ts` is the `'use client'` boundary, so everything imported from it is a client
// reference when it is rendered by a Server Component.
function Greeting({name}: {name: string}) {
return (
<section>
<Heading as="h2">Hello, {name}</Heading>
<Text>Rendered on the server</Text>
</section>
)
}

describe('React Server Components', () => {
test('renders components inside a Server Component', async () => {
await renderServer(<Greeting name="Primer" />)

await expect.element(page.getByRole('heading', {name: 'Hello, Primer'})).toBeVisible()
await expect.element(page.getByText('Rendered on the server')).toBeVisible()
})

test('runs client state and handlers for a client component rendered by a Server Component', async () => {
await renderServer(<Counter initialCount={1} />)

const button = page.getByRole('button', {name: 'Count: 1'})
await expect.element(button).toBeVisible()
await button.click()
await expect.element(page.getByRole('button', {name: 'Count: 2'})).toBeVisible()
})
})
9 changes: 9 additions & 0 deletions packages/react/src/__tests__/rsc-fixtures/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use client'

import {useState} from 'react'
import {Button} from '../../index'

export function Counter({initialCount}: {initialCount: number}) {
const [count, setCount] = useState(initialCount)
return <Button onClick={() => setCount(count + 1)}>Count: {count}</Button>
}
1 change: 1 addition & 0 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"script/**/*.ts",
"rolldown.config.ts",
"vitest.config.mts",
"vitest.config.rsc.mts",
"config/**/*.ts",
".storybook/**/*.ts",
".storybook/**/*.tsx"
Expand Down
1 change: 1 addition & 0 deletions packages/react/vitest.config.browser.mts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default defineConfig({
'**/*.figma.tsx',
'**/*.types.test.ts',
'**/*.types.test.tsx',
'**/*.rsc.test.tsx',
'src/__tests__/exports.test.ts',
'src/__tests__/ssr.test.tsx',
'src/__tests__/storybook.test.tsx',
Expand Down
34 changes: 34 additions & 0 deletions packages/react/vitest.config.rsc.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {createRequire} from 'node:module'
import {defineConfig} from '@primer/vitest-config/config'

import react from '@vitejs/plugin-react'
import {playwright} from '@vitest/browser-playwright'
import {vitestPluginRSC} from 'vitest-plugin-rsc'

const require = createRequire(import.meta.url)
// React Server Components need React 19, so the project has no tests on React 18 (the default).
const supportsRSC = Number.parseInt(require('react/package.json').version, 10) >= 19

// vitest-plugin-rsc runs the server (RSC) and client environments inside the test's browser tab,
// so it needs browser mode. It lives in its own project to keep it out of the main browser suite.
export default defineConfig({
plugins: [react(), vitestPluginRSC()],
resolve: {
dedupe: ['react', 'react-dom'],
},
define: {
__DEV__: true,
},
test: {
name: '@primer/react (rsc)',
include: supportsRSC ? ['src/**/*.rsc.test.?(c|m)[jt]s?(x)'] : [],
setupFiles: ['config/vitest/rsc/setup.ts'],
browser: {
provider: playwright(),
enabled: true,
headless: true,
instances: [{browser: 'chromium'}],
screenshotFailures: false,
},
},
})
2 changes: 2 additions & 0 deletions script/check-classname-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const IGNORED_FILES = [
'packages/react/src/__tests__/Caret.test.tsx',
// Asserts server rendering behavior, not rendered markup
'packages/react/src/__tests__/ssr.test.tsx',
// Asserts rendering inside a Server Component boundary, not a component's className prop
'packages/react/src/__tests__/components.rsc.test.tsx',
'packages/react/src/FilteredActionList/useAnnouncements.test.tsx',
'packages/react/src/TreeView/useRovingTabIndex.test.tsx',
]
Expand Down
1 change: 1 addition & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
'packages/*/vitest.config.mts',
'packages/*/vitest.config.browser.ts',
'packages/*/vitest.config.browser.mts',
'packages/*/vitest.config.rsc.mts',
],
},
})
Loading