Testing and Debugging
This guide covers strategies for testing components that use PikaCSS and debugging common issues.
Testing Strategies
Unit Testing Components
PikaCSS transforms styles at build time, so your tests see the transformed class names.
With Vitest + React Testing Library
pnpm add -D vitest @testing-library/react @testing-library/jest-dom// Button.test.tsx
import { render, screen } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import { Button } from './Button'
describe('Button', () => {
it('renders with correct classes', () => {
render(<Button>Click me</Button>)
const button = screen.getByRole('button')
// PikaCSS generates atomic class names
expect(button.className).toBeTruthy()
expect(button.className).toMatch(/\w+/) // Has classes
})
it('applies styles correctly', () => {
render(<Button>Click me</Button>)
const button = screen.getByRole('button')
// Check computed styles instead of class names
const styles = window.getComputedStyle(button)
expect(styles.backgroundColor).toBe('rgb(59, 130, 246)') // #3b82f6
})
})Key insight: Test computed styles, not class names (atomic classes are implementation details).
Testing with Shortcuts
// Button.tsx
export function Button({ variant = 'primary' }) {
return (
<button className={pika(`btn-${variant}`)}>
Click
</button>
)
}// Button.test.tsx
import { render } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
describe('Button variants', () => {
it('renders primary variant', () => {
const { container } = render(<Button variant="primary" />)
const button = container.querySelector('button')
// Test computed styles
const styles = window.getComputedStyle(button!)
expect(styles.backgroundColor).toBe('rgb(59, 130, 246)')
})
it('renders secondary variant', () => {
const { container } = render(<Button variant="secondary" />)
const button = container.querySelector('button')
const styles = window.getComputedStyle(button!)
expect(styles.backgroundColor).toBe('rgb(107, 114, 128)')
})
})Integration Testing
Test that styles are generated and applied correctly:
// integration.test.ts
import { describe, expect, it } from 'vitest'
import fs from 'fs'
import path from 'path'
describe('PikaCSS Integration', () => {
it('generates CSS file', () => {
const cssPath = path.join(__dirname, '../src/pika.gen.css')
expect(fs.existsSync(cssPath)).toBe(true)
})
it('generates TypeScript definitions', () => {
const tsPath = path.join(__dirname, '../src/pika.gen.ts')
expect(fs.existsSync(tsPath)).toBe(true)
})
it('CSS contains expected styles', () => {
const cssPath = path.join(__dirname, '../src/pika.gen.css')
const css = fs.readFileSync(cssPath, 'utf-8')
// Check for specific property-value pairs
expect(css).toContain('color')
expect(css).toContain('background-color')
})
})E2E Testing
Use tools like Playwright or Cypress to test visual appearance:
// button.spec.ts (Playwright)
import { expect, test } from '@playwright/test'
test('button has correct styles', async ({ page }) => {
await page.goto('/components/button')
const button = page.locator('button').first()
// Check computed styles
const bgColor = await button.evaluate(
el => window.getComputedStyle(el).backgroundColor
)
expect(bgColor).toBe('rgb(59, 130, 246)')
// Visual regression
await expect(button).toHaveScreenshot('button.png')
})
test('hover state works', async ({ page }) => {
await page.goto('/components/button')
const button = page.locator('button').first()
await button.hover()
const bgColor = await button.evaluate(
el => window.getComputedStyle(el).backgroundColor
)
expect(bgColor).toBe('rgb(37, 99, 235)') // hover color
})Snapshot Testing
// Button.test.tsx
import { render } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
describe('Button snapshots', () => {
it('matches snapshot', () => {
const { container } = render(<Button>Click me</Button>)
expect(container).toMatchSnapshot()
})
})Note: Atomic class names may change. Consider using toMatchInlineSnapshot() or testing computed styles instead.
Debugging Techniques
1. Verify Generated Files
First step: Check if files are generated correctly.
# Check if files exist
ls -la src/pika.gen.css
ls -la src/pika.gen.ts
# View content
cat src/pika.gen.css
cat src/pika.gen.ts2. Browser DevTools
Inspect Elements
1. Right-click element → Inspect
2. Check "Styles" panel for applied CSS
3. Look for atomic class names (.a, .b, .c, etc.)
4. Verify properties are appliedCheck CSS Loading
// Open browser console
console.log(document.styleSheets)
// Check if pika.gen.css is loaded
Array.from(document.styleSheets).forEach(sheet => {
console.log(sheet.href)
})Debug Style Specificity
// Get computed styles for an element
const el = document.querySelector('.my-component')
const styles = window.getComputedStyle(el)
console.log(styles.backgroundColor) // Current value
console.log(styles.color) // Current value
// Check all applied styles
for (let prop of styles) {
console.log(prop, styles.getPropertyValue(prop))
}3. Enable Debug Logging
// vite.config.ts
export default defineConfig({
plugins: [
pikacss({
// Add logging
scan: {
include: ['src/**/*.{ts,tsx}'],
exclude: ['node_modules/**']
}
})
],
// Enable debug mode
logLevel: 'info'
})# Run with debug flag
DEBUG=pikacss:* pnpm dev4. Check Transformation
Verify that pika() calls are being transformed:
// Original code
const styles = pika({ color: 'red' })
// After transformation (check browser sources)
const styles = "a" // Should be transformed to class nameHow to check:
- Open browser DevTools → Sources
- Find your component file
- Look at the transformed code
pika()calls should be replaced with strings
5. Inspect Network Tab
Check if CSS is loading correctly:
1. Open DevTools → Network
2. Filter by "CSS"
3. Look for pika.gen.css
4. Check Status: 200 OK
5. Check Size: Should be non-zero
6. Preview: Should contain CSS rulesCommon Issues and Solutions
Issue: Styles Not Applied
Symptom
Elements have class names but no visual styles.
Debugging Steps
- Check if CSS is imported:
// ✅ Must be imported in entry file
import 'pika.css'- Verify CSS file exists:
ls -la src/pika.gen.cssCheck browser DevTools:
- Network tab: Is
pika.gen.cssloaded? - Elements tab: Are classes applied?
- Console: Any CSS loading errors?
- Network tab: Is
Check for CSS conflicts:
/* Another stylesheet might override */
* { color: black !important; } /* Kills all colors */Solutions
- Import
pika.cssin root/entry file - Check build configuration includes PikaCSS plugin
- Verify no CSS resets are overriding styles
- Use
__important: trueif needed
Issue: pika is not defined
Symptom
ReferenceError: pika is not definedDebugging Steps
- Check TypeScript reference:
/// <reference path="./pika.gen.ts" />- Verify generated file:
cat src/pika.gen.ts
# Should export: export function pika(...)- Check imports:
// If not using Nuxt, import explicitly
import { pika } from './pika.gen'Solutions
- Ensure
pika.gen.tsis generated (run dev server once) - Add TypeScript reference in config file
- For Nuxt: Check module is loaded in
nuxt.config.ts - Restart TypeScript server:
Cmd+Shift+P→ "Restart TS Server"
Issue: Build Fails
Symptom
Error: Cannot find module 'pika.css'Debugging Steps
- Check plugin configuration:
// vite.config.ts
plugins: [
pikacss() // Must be present
]- Verify file generation:
# Run dev mode first to generate files
pnpm dev
# Then build
pnpm build- Check for syntax errors:
// pika.config.ts
export default defineEngineConfig({
// Make sure no syntax errors
})Solutions
- Add PikaCSS plugin to build configuration
- Run dev mode once before building
- Fix any syntax errors in config
- Clear cache:
rm -rf node_modules/.vite
Issue: TypeScript Errors
Symptom
Property 'pika' does not exist on type 'Window'Debugging Steps
- Check reference directive:
/// <reference path="./src/pika.gen.ts" />- Verify in tsconfig.json:
{
"include": ["src/**/*", "pika.gen.ts"]
}- Check file generation:
cat src/pika.gen.tsSolutions
- Add reference directive to config or entry file
- Update
tsconfig.jsoninclude patterns - Restart TypeScript server
- Regenerate files:
rm src/pika.gen.ts && pnpm dev
Issue: Styles Flicker (FOUC)
Symptom
Flash of Unstyled Content on page load.
Debugging Steps
- Check CSS loading order:
<head>
<!-- PikaCSS should load early -->
<link rel="stylesheet" href="/pika.gen.css">
</head>- Verify critical CSS:
// Check if CSS is inlined or loaded externallySolutions
- Inline critical CSS in HTML head
- Use preload hint:
<link rel="preload" href="/pika.gen.css"> - Ensure CSS loads before JavaScript
- For SSR: Import CSS in layout component
Issue: HMR Not Working
Symptom
Changes to styles don't hot reload.
Debugging Steps
- Check plugin configuration:
plugins: [
pikacss({
scan: {
include: ['src/**/*.{ts,tsx}'] // Must include your files
}
})
]- Verify file watching:
# Check if files are being watched
# Output should show file changes- Check browser console:
[vite] hmr update /src/Component.tsxSolutions
- Ensure files are in
scan.includepattern - Restart dev server
- Clear browser cache
- Check for syntax errors blocking HMR
Issue: Wrong Styles Applied
Symptom
Styles different from expected.
Debugging Steps
- Check CSS specificity:
/* More specific selector wins */
.parent .child { color: blue; } /* Wins over */
.child { color: red; }Inspect element in DevTools:
- Which styles are applied?
- Which are overridden?
- Check specificity
Check for !important:
.other { color: green !important; } /* Overrides everything */Solutions
- Use
__important: trueto override - Increase specificity with selectors
- Check for conflicting global styles
- Use
$.classnamefor more specific selectors
Debugging Workflow
Step-by-Step Debugging Process
- Verify Files Generated
ls src/pika.gen.css src/pika.gen.ts- Check Import
import 'pika.css' // Present in entry?Inspect Element
- Right-click → Inspect
- Check classes applied
- Check computed styles
Check Network
- Is CSS loaded?
- Status 200?
- Non-zero size?
Check Console
- Any errors?
- Any warnings?
Check Sources
- Is code transformed?
- Are pika() calls replaced?
Check Config
- Plugin loaded?
- Scan patterns correct?
- Syntax errors?
Best Practices for Testing
- ✅ Test computed styles, not class names
- ✅ Use data-testid for reliable selectors
- ✅ Test behavior, not implementation
- ✅ Use E2E for visual testing
- ✅ Mock styles in unit tests when needed
- ✅ Test responsive behavior with viewport changes
- ✅ Visual regression testing for critical components
- ✅ Test accessibility alongside styles
Advanced Debugging Tools
Custom Debug Component
// DebugStyles.tsx
export function DebugStyles({ children, label }: {
children: React.ReactNode
label?: string
}) {
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
if (ref.current) {
const styles = window.getComputedStyle(ref.current)
console.group(label || 'Debug Styles')
console.log('Classes:', ref.current.className)
console.log('Computed styles:', {
color: styles.color,
backgroundColor: styles.backgroundColor,
padding: styles.padding,
margin: styles.margin
})
console.groupEnd()
}
}, [label])
return <div ref={ref}>{children}</div>
}
// Usage
<DebugStyles label="My Button">
<Button>Click me</Button>
</DebugStyles>Style Inspector Hook
// useStyleDebug.ts
export function useStyleDebug(ref: RefObject<HTMLElement>, enabled = true) {
useEffect(() => {
if (!enabled || !ref.current) return
const el = ref.current
const styles = window.getComputedStyle(el)
console.group('Style Debug')
console.log('Element:', el)
console.log('Classes:', el.className)
console.log('Styles:', {
display: styles.display,
position: styles.position,
width: styles.width,
height: styles.height,
padding: styles.padding,
margin: styles.margin,
backgroundColor: styles.backgroundColor,
color: styles.color
})
console.groupEnd()
}, [ref, enabled])
}
// Usage
const ref = useRef<HTMLDivElement>(null)
useStyleDebug(ref, process.env.NODE_ENV === 'development')
return <div ref={ref} className={pika({ color: 'red' })}>Debug me</div>Troubleshooting Checklist
When something goes wrong, check:
- [ ]
pika.gen.cssexists and is non-empty - [ ]
pika.gen.tsexists and exportspikafunction - [ ]
import 'pika.css'is present in entry file - [ ] Plugin is configured in build tool
- [ ] Files are in
scan.includepatterns - [ ] No syntax errors in
pika.config.ts - [ ] TypeScript reference is present
- [ ] Dev server is running (for HMR)
- [ ] Browser cache is cleared
- [ ] CSS is loaded in Network tab
- [ ] Elements have class names
- [ ] Computed styles are correct
- [ ] No conflicting CSS
- [ ] Console has no errors
Getting Help
If you're still stuck:
- Check GitHub Issues
- Review API Reference
- Ask in GitHub Discussions
- Provide:
- PikaCSS version
- Build tool and version
- Framework and version
- Minimal reproduction
- Error messages
- Configuration files
Next Steps
- Review Troubleshooting Guide for more solutions
- Check Performance Guide for optimization
- Explore Examples for working patterns