Testing Skill
🎯 Quick Reference
- •node:test para lógica pura (utils, SEO, analytics).
- •Vitest para React, hooks, DOM y browser APIs.
- •Tests rápidos, aislados y sin dependencias externas reales.
- •Tests junto al código (
*.test.ts(x)).
🧭 ¿Cuándo usar cada herramienta?
node:test
- •✅ funciones puras
- •✅ transformaciones y cálculos
- •✅ schemas SEO, analytics
Vitest
- •✅ componentes React
- •✅ hooks
- •✅ jsdom / browser APIs
✅ Principios
- •AAA (Arrange-Act-Assert)
- •nombres descriptivos
- •mocks solo de dependencias externas
- •cobertura inteligente (no 100% a cualquier costo)
🧪 Ejemplos esenciales
node:test (lógica pura)
ts
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { slugify } from "./index";
describe("slugify", () => {
it("convierte a slug URL-safe", () => {
assert.equal(slugify("Mantel Floral 150x200"), "mantel-floral-150x200");
});
});
Vitest (React)
tsx
import { render, screen } from "@testing-library/react";
import { describe, it, expect } from "vitest";
import { ProductCard } from "./ProductCard";
describe("ProductCard", () => {
it("renderiza nombre y precio", () => {
render(
<ProductCard producto={{ nombre: "Mantel", precio_desde: 15000 }} />,
);
expect(screen.getByText("Mantel")).toBeInTheDocument();
});
});
Mock Supabase (Vitest)
ts
vi.mock("@/lib/supabase/client", () => ({
createClient: () => ({
from: () => ({
select: () => ({ data: [{ id: "1" }], error: null }),
}),
}),
}));
📏 Convenciones
- •tests junto al código
- •
getByRoleygetByLabelTextpara accesibilidad - •tests independientes y deterministas
✅ Checklist
- • node:test para lógica pura
- • Vitest para React/hooks
- • mocks de Supabase/fetch
- • estados loading/error cubiertos
- • nombres claros en español
- • tests rápidos (<1s por archivo)
🧰 Comandos
bash
npm run test:node npm run test:unit npm run test:watch npm run test:coverage