Test Results
A test run at a glance, with the counts on top and the failures open by default.
NewInstallation
Copy and paste the following code into your project.
1'use client';23import { AnimatePresence, motion, useReducedMotion, type Variants } from 'motion/react';4import * as React from 'react';5import { cn } from '../lib/cn';6import { AUTO_HEIGHT, Roll, STATUS_IN, useAutoHeight, useChanged } from '../lib/arrival';7import { collapse, springs, still } from '../lib/motion';8import { useControllableState } from '../lib/use-controllable-state';910type IconProps = React.SVGProps<SVGSVGElement> & {11 size?: number | string;12};1314function Svg({ size = 24, strokeWidth = 1.5, children, ...props }: IconProps) {15 return (16 <svg17 xmlns="http://www.w3.org/2000/svg"18 width={size}19 height={size}20 viewBox="0 0 24 24"21 fill="none"22 stroke="currentColor"23 strokeWidth={strokeWidth}24 strokeLinecap="round"25 strokeLinejoin="round"26 aria-hidden="true"27 {...props}28 >29 {children}30 </svg>31 );32}3334function ChevronDownIcon(props: IconProps) {35 return (36 <Svg {...props}>37 <path d="m6 9 6 6 6-6" />38 </Svg>39 );40}4142function CheckIcon(props: IconProps) {43 return (44 <Svg {...props}>45 <path d="M20 6 9 17l-5-5" />46 </Svg>47 );48}4950function XIcon(props: IconProps) {51 return (52 <Svg {...props}>53 <path d="M18 6 6 18" />54 <path d="m6 6 12 12" />55 </Svg>56 );57}5859function SkipIcon(props: IconProps) {60 return (61 <Svg {...props}>62 <path d="M6 12h12" />63 </Svg>64 );65}6667function LoaderIcon(props: IconProps) {68 return (69 <Svg {...props}>70 <path d="M12 3a9 9 0 1 0 9 9" />71 </Svg>72 );73}7475const INSTANT_COLLAPSE: Variants = {76 closed: { height: 0, opacity: 0, transition: still },77 open: { height: 'auto', opacity: 1, transition: still },78};7980type TestStatus = 'passed' | 'failed' | 'skipped' | 'running';8182type TestStatusLabels = Record<TestStatus, string>;8384const DEFAULT_STATUS_LABELS: TestStatusLabels = {85 passed: 'Passed',86 failed: 'Failed',87 skipped: 'Skipped',88 running: 'Running',89};9091const STATUS_ICON: Record<TestStatus, React.ComponentType<IconProps>> = {92 passed: CheckIcon,93 failed: XIcon,94 skipped: SkipIcon,95 running: LoaderIcon,96};9798const STATUS_TONE: Record<TestStatus, string> = {99 passed: 'text-success-text',100 failed: 'text-destructive-text',101 skipped: 'text-muted-foreground',102 running: 'text-foreground',103};104105interface TestResultsSummaryValue {106 passed: number;107 failed: number;108 skipped?: number;109}110111interface TestResultsContextValue {112 summary?: TestResultsSummaryValue;113}114115const TestResultsContext = React.createContext<TestResultsContextValue | null>(null);116117const useTestResultsContext = (): TestResultsContextValue => {118 const context = React.use(TestResultsContext);119 if (!context) {120 throw new Error('TestResults components must be used within TestResults');121 }122 return context;123};124125interface TestResultsProps extends React.HTMLAttributes<HTMLDivElement> {126 summary?: TestResultsSummaryValue;127}128129interface TestResultsHeaderProps extends React.HTMLAttributes<HTMLDivElement> {130 duration?: React.ReactNode;131}132133interface TestResultsSummaryProps extends React.HTMLAttributes<HTMLDListElement> {134 passedLabel?: string;135 failedLabel?: string;136 skippedLabel?: string;137}138139interface TestResultsSuiteProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {140 name: React.ReactNode;141 status?: TestStatus;142 defaultOpen?: boolean;143 open?: boolean;144 onOpenChange?: (open: boolean) => void;145 statusLabels?: Partial<TestStatusLabels>;146}147148interface TestResultsTestProps extends Omit<React.LiHTMLAttributes<HTMLLIElement>, 'title'> {149 name: React.ReactNode;150 status?: TestStatus;151 duration?: React.ReactNode;152 statusLabels?: Partial<TestStatusLabels>;153}154155type TestResultsErrorProps = React.HTMLAttributes<HTMLPreElement>;156157const TestResultsRoot = ({158 className,159 summary,160 children,161 ref,162 ...props163}: TestResultsProps & { ref?: React.Ref<HTMLDivElement> }) => {164 const value = React.useMemo<TestResultsContextValue>(() => ({ summary }), [summary]);165 const failed = (summary?.failed ?? 0) > 0;166167 return (168 <TestResultsContext value={value}>169 <div170 ref={ref}171 data-slot="test-results"172 data-failed={failed ? '' : undefined}173 className={cn(174 'bg-card text-card-foreground w-full min-w-0 overflow-hidden rounded-lg shadow-sm',175 className,176 )}177 {...props}178 >179 {children}180 </div>181 </TestResultsContext>182 );183};184185TestResultsRoot.displayName = 'TestResults';186187const TestResultsHeader = ({188 className,189 duration,190 children,191 ref,192 ...props193}: TestResultsHeaderProps & { ref?: React.Ref<HTMLDivElement> }) => {194 return (195 <div196 ref={ref}197 data-slot="test-results-header"198 className={cn(199 'bg-surface-muted border-border text-muted-strong flex min-h-10 items-center gap-2 border-b px-3 font-mono text-xs',200 className,201 )}202 {...props}203 >204 <span className="text-foreground min-w-0 flex-1 truncate">{children}</span>205 {duration ? (206 <span className="text-muted-foreground shrink-0 tabular-nums">{duration}</span>207 ) : null}208 </div>209 );210};211212TestResultsHeader.displayName = 'TestResultsHeader';213214const TestResultsSummary = ({215 className,216 passedLabel = 'Passed',217 failedLabel = 'Failed',218 skippedLabel = 'Skipped',219 ref,220 ...props221}: TestResultsSummaryProps & { ref?: React.Ref<HTMLDListElement> }) => {222 const { summary } = useTestResultsContext();223 if (!summary) return null;224225 const skipped = summary.skipped ?? 0;226 const total = summary.passed + summary.failed + skipped;227 const stats = [228 { key: 'passed', label: passedLabel, count: summary.passed, tone: 'text-success-text' },229 {230 key: 'failed',231 label: failedLabel,232 count: summary.failed,233 tone: summary.failed > 0 ? 'text-destructive-text' : 'text-foreground',234 },235 { key: 'skipped', label: skippedLabel, count: skipped, tone: 'text-foreground' },236 ];237238 return (239 <div data-slot="test-results-summary" className="flex flex-col gap-2 px-3 pt-3">240 <dl ref={ref} className={cn('grid grid-cols-3 gap-2', className)} {...props}>241 {stats.map((stat) => (242 <div243 key={stat.key}244 data-slot={`test-results-${stat.key}`}245 className={cn(246 'bg-secondary flex flex-col-reverse gap-0.5 rounded-md px-3 py-2',247 stat.key === 'failed' && summary.failed > 0 && 'bg-destructive-surface',248 )}249 >250 <dt className="text-muted-foreground text-xs">{stat.label}</dt>251 <dd className={cn('font-heading text-xl font-semibold tabular-nums', stat.tone)}>252 <Roll value={stat.count} />253 </dd>254 </div>255 ))}256 </dl>257 {total > 0 ? (258 <div aria-hidden="true" className="bg-muted flex h-1 overflow-hidden rounded-full">259 <span260 className="bg-success-text"261 style={{ width: `${(summary.passed / total) * 100}%` }}262 />263 <span264 className="bg-destructive-text"265 style={{ width: `${(summary.failed / total) * 100}%` }}266 />267 </div>268 ) : null}269 </div>270 );271};272273TestResultsSummary.displayName = 'TestResultsSummary';274275const TestResultsSuites = ({276 className,277 children,278 ref,279 ...props280}: React.HTMLAttributes<HTMLDivElement> & { ref?: React.Ref<HTMLDivElement> }) => {281 return (282 <div283 ref={ref}284 data-slot="test-results-suites"285 className={cn('flex flex-col gap-1 p-1.5', className)}286 {...props}287 >288 {children}289 </div>290 );291};292293TestResultsSuites.displayName = 'TestResultsSuites';294295const TestResultsSuite = ({296 className,297 name,298 status = 'passed',299 defaultOpen,300 open: controlledOpen,301 onOpenChange,302 statusLabels,303 children,304 ref,305 ...props306}: TestResultsSuiteProps & { ref?: React.Ref<HTMLDivElement> }) => {307 const [open, setOpen] = useControllableState({308 value: controlledOpen,309 defaultValue: defaultOpen ?? status === 'failed',310 onChange: onOpenChange,311 });312 const id = React.useId();313 const shouldReduceMotion = useReducedMotion();314 const StatusIcon = STATUS_ICON[status];315 const statusChanged = useChanged(status);316 const statusLabel = statusLabels?.[status] ?? DEFAULT_STATUS_LABELS[status];317318 const [growRef, growStyle] = useAutoHeight();319320 return (321 <div322 ref={ref}323 data-slot="test-results-suite"324 data-status={status}325 data-state={open ? 'open' : 'closed'}326 className={cn('flex flex-col', className)}327 {...props}328 >329 <button330 type="button"331 id={`${id}-trigger`}332 aria-expanded={open}333 aria-controls={open ? `${id}-content` : undefined}334 onClick={() => setOpen(!open)}335 className="hover:bg-muted/60 focus-visible:ring-ring flex w-full items-center gap-2 rounded-md px-1.5 py-1.5 text-start font-mono text-xs transition-colors focus-visible:ring-2 focus-visible:outline-none"336 >337 <span338 key={status}339 aria-hidden="true"340 className={cn(341 'flex shrink-0',342 STATUS_TONE[status],343 status === 'running' ? 'motion-safe:animate-spin' : statusChanged && STATUS_IN,344 )}345 >346 <StatusIcon size={14} />347 </span>348 <span className="text-foreground min-w-0 flex-1 truncate">349 {name}350 <span className="sr-only">{`, ${statusLabel}`}</span>351 </span>352 <motion.span353 aria-hidden="true"354 className="text-muted-foreground flex shrink-0"355 initial={false}356 animate={shouldReduceMotion ? undefined : { rotate: open ? 180 : 0 }}357 transition={springs.snappy}358 >359 <ChevronDownIcon size={14} />360 </motion.span>361 </button>362 <AnimatePresence initial={false}>363 {open && (364 <motion.div365 id={`${id}-content`}366 role="region"367 aria-labelledby={`${id}-trigger`}368 variants={shouldReduceMotion ? INSTANT_COLLAPSE : collapse}369 initial="closed"370 animate="open"371 exit="closed"372 className="overflow-hidden"373 >374 <div style={growStyle} className={AUTO_HEIGHT}>375 <div ref={growRef} className="flow-root">376 <ul className="border-border ms-[13px] flex flex-col gap-0.5 border-s py-1 ps-2.5">377 {children}378 </ul>379 </div>380 </div>381 </motion.div>382 )}383 </AnimatePresence>384 </div>385 );386};387388TestResultsSuite.displayName = 'TestResultsSuite';389390const TestResultsTest = ({391 className,392 name,393 status = 'passed',394 duration,395 statusLabels,396 children,397 ref,398 ...props399}: TestResultsTestProps & { ref?: React.Ref<HTMLLIElement> }) => {400 const StatusIcon = STATUS_ICON[status];401 const statusChanged = useChanged(status);402 const statusLabel = statusLabels?.[status] ?? DEFAULT_STATUS_LABELS[status];403404 return (405 <li406 ref={ref}407 data-slot="test-results-test"408 data-status={status}409 className={cn('flex flex-col gap-1.5 py-1', className)}410 {...props}411 >412 <span className="flex items-center gap-2 text-xs">413 <span414 key={status}415 aria-hidden="true"416 className={cn(417 'flex shrink-0',418 STATUS_TONE[status],419 status === 'running' ? 'motion-safe:animate-spin' : statusChanged && STATUS_IN,420 )}421 >422 <StatusIcon size={12} />423 </span>424 <span425 className={cn(426 'min-w-0 flex-1 truncate',427 status === 'failed' ? 'text-foreground' : 'text-muted-foreground',428 )}429 >430 {name}431 <span className="sr-only">{`, ${statusLabel}`}</span>432 </span>433 {duration ? (434 <span className="text-muted-foreground shrink-0 font-mono tabular-nums">{duration}</span>435 ) : null}436 </span>437 {children}438 </li>439 );440};441442TestResultsTest.displayName = 'TestResultsTest';443444const TestResultsError = ({445 className,446 children,447 ref,448 ...props449}: TestResultsErrorProps & { ref?: React.Ref<HTMLPreElement> }) => {450 return (451 <pre452 ref={ref}453 data-slot="test-results-error"454 className={cn(455 'bg-destructive-surface text-destructive-text overflow-x-auto rounded-md px-2.5 py-2 font-mono text-xs leading-relaxed break-words whitespace-pre-wrap',456 className,457 )}458 {...props}459 >460 {children}461 </pre>462 );463};464465TestResultsError.displayName = 'TestResultsError';466467const TestResults = Object.assign(TestResultsRoot, {468 Header: TestResultsHeader,469 Summary: TestResultsSummary,470 Suites: TestResultsSuites,471 Suite: TestResultsSuite,472 Test: TestResultsTest,473 Error: TestResultsError,474});475476export { TestResults };477export type {478 TestResultsErrorProps,479 TestResultsHeaderProps,480 TestResultsProps,481 TestResultsSuiteProps,482 TestResultsSummaryProps,483 TestResultsSummaryValue,484 TestResultsTestProps,485 TestStatus,486 TestStatusLabels,487};
Update the import paths to match your project structure.
Anatomy
1import { TestResults } from '@/ai/test-results';
1<TestResults summary={{ passed: 12, failed: 1, skipped: 1 }}>2 <TestResults.Header duration="4.2s">src/checkout</TestResults.Header>3 <TestResults.Summary />4 <TestResults.Suites>5 <TestResults.Suite name="total.test.ts" status="failed">6 <TestResults.Test name="applies the discount before tax" status="failed">7 <TestResults.Error>expected 107.09 to be 107.1</TestResults.Error>8 </TestResults.Test>9 </TestResults.Suite>10 </TestResults.Suites>11</TestResults>
The numbers answer the first question, did it pass, before the reader reads a single test name. Failed suites open by themselves and passing ones stay closed, so the error is the first thing in view.
Use it when the agent ran tests and you have the results as data. For the raw output of the run, use Terminal.
Composition
Use the following composition to build
TestResults:TestResultsโโโ TestResults.Headerโโโ TestResults.Summaryโโโ TestResults.Suitesโโโ TestResults.Suiteโโโ TestResults.Testโโโ TestResults.Error
Accessibility
- The counts are a description list, so a screen reader pairs each number with its label.
- Suites and tests end with their status in words ("total.test.ts, Failed"); the icons alone carry no meaning.
- The colored bar under the counts is decorative and hidden from assistive tech.
API Reference
TestResults
| Prop | Type | Default | Description |
|---|---|---|---|
summary | { passed: number; failed: number; skipped?: number } | - | Counts for the summary |
className | string | - | Additional CSS classes |
TestResults.Header
| Prop | Type | Default | Description |
|---|---|---|---|
duration | ReactNode | - | How long the run took |
children | ReactNode | - | What ran, like a folder |
className | string | - | Additional CSS classes |
TestResults.Summary
| Prop | Type | Default | Description |
|---|---|---|---|
passedLabel | string | 'Passed' | Label of the count |
failedLabel | string | 'Failed' | Label of the count |
skippedLabel | string | 'Skipped' | Label of the count |
TestResults.Suite
| Prop | Type | Default | Description |
|---|---|---|---|
name | ReactNode | - | Usually the file name |
status | 'passed' | 'failed' | 'skipped' | 'running' | 'passed' | Result of the suite |
defaultOpen | boolean | true when it failed | Open state when uncontrolled |
open | boolean | - | Controlled open state |
onOpenChange | (open: boolean) => void | - | Called when the open state changes |
statusLabels | Partial<Record<Status, string>> | - | Screen reader labels for each status |
TestResults.Test
| Prop | Type | Default | Description |
|---|---|---|---|
name | ReactNode | - | Name of the test |
status | 'passed' | 'failed' | 'skipped' | 'running' | 'passed' | Result of the test |
duration | ReactNode | - | How long it took |
children | ReactNode | - | Usually a TestResults.Error |
statusLabels | Partial<Record<Status, string>> | - | Screen reader labels for each status |
Found something to improve?
Notice a bug, typo, or missing detail on this page? Help us make the documentation better by opening a GitHub issue.