# Testing and development

> Package commands, test ownership, accessibility checks and visual regression.

Run `vp check packages/dify-ui` from the repository root for formatting, lint, and TypeScript diagnostics. Run the remaining commands from `packages/dify-ui/`:

- `vp test --project unit` runs primitive unit, interaction, and light/dark `axe-core` accessibility tests.
- `vp test run --project unit --changed=origin/main` runs only unit tests affected by this branch and uncommitted changes. Pull request CI runs this selection without coverage; the merge queue runs the full project with coverage.
- `vp run test:visual` runs visual regression tests (`*.visual.spec.{ts,tsx}`).
- `vp run test:visual:update` updates visual regression baseline screenshots.
- `vp test` runs all test projects (`unit` and `visual`).

## Test boundary

This guide owns the Dify UI testing policy and runtime setup. Add tests for observable Dify integration behavior or a reproducible regression, not merely because a component or prop exists. Check existing tests before adding coverage. Browser execution alone does not justify a new case; do not add permanent tests for incidental visual adjustments.

The package has two [Vitest projects]: `unit` and `visual`. Both run in Playwright Chromium [Browser Mode]; the project name identifies the behavior owner, not a different runtime.

Playwright traces are disabled locally to avoid large disk usage. CI retains traces on failure under `.vitest-browser/traces/` and failure screenshots under `.vitest/attachments/`.

Use the `unit` project (`src/**/__tests__/**/*.spec.{ts,tsx}`) for observable Dify integration behavior: visible state changes, user interactions, keyboard paths, overlay flows, form behavior, loading behavior, controlled-state coordination, submitted values, store behavior, regressions reached through a public API, and light/dark `axe-core` accessibility checks. Prop passthrough alone does not justify a test. Assert the resulting behavior instead of CSS class names (`toHaveClass` is forbidden by lint in package tests) or private structure, and do not duplicate behavior already owned by Base UI or the browser. Vitest 5 browser locators match string names exactly by default. Assert a control's accessible name or description when that is the contract. `toHaveTextContent` also checks the full text exactly; use `toMatchTextContent` only when the complete text is intentionally outside the test's scope.

## Accessibility testing

Run light and dark `axe-core` accessibility checks in the `unit` project using `expectA11yThemes` from [`src/internal/a11y-test.tsx`]. Each scenario renders across both `light` and `dark` themes on the Dify panel surface (`bg-components-panel-bg text-text-primary`), optionally executes a `play` setup interaction (for example, opening a popup or focusing an item), and fails on any enabled `axe-core` violation, including `color-contrast`.

Do not add global rule exceptions in [`src/internal/a11y-test.tsx`]. When a known design-token gap cannot be fixed without diverging from the package tokens, keep the exception local to the affected test via the `rules` option and scope the `color-contrast` `selector` to the affected theme (`html[data-theme="light"]` or `html[data-theme="dark"]`) and offending elements (for example, `*:not(html[data-theme="dark"] ...)`), rather than setting `enabled: false`. Do not use an interaction assertion in place of an accessibility fix.

## Type contracts

Keep compile-only public API fixtures in `__tests__/types.tsx`; package type checking includes them without registering runtime tests. Import public subpaths, verify useful inference with `expectTypeOf`, and use `@ts-expect-error` for intentional API boundaries. Cover nullable callbacks, value models and unsupported composition, not every forwarded prop. Runtime assertions cannot prove TypeScript inference, and type fixtures cannot prove focus, DOM semantics or interaction.

## Animation setup

Base UI can wait for `element.getAnimations()` before unmounting transition-driven components. [`vitest.setup.ts`] sets `BASE_UI_ANIMATIONS_DISABLED = true` for primitive tests that assert final DOM state. A unit test that intentionally asserts animation behavior may set the flag to `false` locally, but must restore the previous value during cleanup.

## Visual regression

Use `src/**/__tests__/**/*.visual.spec.{ts,tsx}` (typically `src/<component>/__tests__/index.visual.spec.tsx`) for primitives whose visual appearance, light/dark token parity, focus indicators, or overlay layout warrant pixel-level regression coverage without duplicating unit behavior assertions.

- **Declarative helper**: Author matrices with `describeVisual` or `expectVisualThemes` from [`src/internal/visual-test.tsx`]. Each scenario renders synchronously into both `light` and `dark` themes inside a deterministic `bg-components-panel-bg text-text-primary` surface canvas, waits for `document.fonts.ready` and two animation frames, defaults baseline names to Vitest's fully qualified test name (`expect.getState().currentTestName`) unless `name` is set explicitly, and asserts `toMatchScreenshot` (`pixelmatch`, `threshold: 0.1`, `allowedMismatchedPixelRatio: 0.002`, `allowedMismatchedPixels: 120` — Vitest enforces the stricter limit so small icons and focus rings cannot slip past full-page captures).
- **Deterministic runtime**: [`vitest.visual.setup.ts`] and [`vitest.visual.css`] lock the viewport to `1280x720`, bundle `@fontsource-variable/inter` (`Inter Variable`), disable CSS animations, transitions, and caret blink, and disable Base UI animations.
- **Linux Chromium single source of truth**: Reference PNGs live beside each spec directory under `src/**/__tests__/**/__screenshots__/` (with non-`index` spec files nested under `__screenshots__/<spec-name>/`) without OS or browser suffixes (`-darwin`/`-linux`) because CI (`depot-ubuntu-24.04-4` Linux Chromium) is the sole source of truth for font rasterization and subpixel anti-aliasing. Do not commit macOS-generated baselines.
- **Generating or updating baselines**: Add the `update-visual-baselines` label to the pull request. The [autofix.ci workflow] runs `vp run test:visual:update` in `packages/dify-ui` on `depot-ubuntu-24.04-4`, and autofix.ci commits the updated screenshots to the PR branch, which starts the PR checks again. Only the labeling event runs the update, so later pushes keep comparing against the committed baselines; remove and re-add the label to regenerate them.

[Browser Mode]: https://vitest.dev/guide/browser
[Vitest projects]: https://vitest.dev/guide/projects.html
[`src/internal/a11y-test.tsx`]: https://github.com/langgenius/dify/blob/main/packages/dify-ui/src/internal/a11y-test.tsx
[`src/internal/visual-test.tsx`]: https://github.com/langgenius/dify/blob/main/packages/dify-ui/src/internal/visual-test.tsx
[`vitest.setup.ts`]: https://github.com/langgenius/dify/blob/main/packages/dify-ui/vitest.setup.ts
[`vitest.visual.css`]: https://github.com/langgenius/dify/blob/main/packages/dify-ui/vitest.visual.css
[`vitest.visual.setup.ts`]: https://github.com/langgenius/dify/blob/main/packages/dify-ui/vitest.visual.setup.ts
[autofix.ci workflow]: https://github.com/langgenius/dify/blob/main/.github/workflows/autofix.yml
