agentleFS
chawkitariqchawkitariq/fidely/.claude/skills/testing-patterns/SKILL.md

testing-patterns

Skill0 starsChanged 8 months agoMITView on GitHub

Patterns de tests Fidely : Vitest + @nuxt/test-utils + fake-indexeddb + happy-dom. Déclencher quand : écriture de test, ajout composable (useDB, useClient), nouveau composant Vue, modification d'une fonction dans utils/, isolation IndexedDB, mock Nuxt (useToast, navigateTo). Mots-clés : describe, it, expect, beforeEach, IDBFactory, resetDB, mountSuspended, mockNuxtImport, fake-indexeddb, useDB.test.ts, CardItem.test.ts, CardForm.test.ts, updatePoints, consumeReward, addCard, getOrCreateClientId.

  • Installs packages

44 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use intelligent-routing, bash-linux and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • SKB3002Skill

    intelligent-routing

    Automatic agent selection and intelligent task routing. Analyzes user requests and automatically selects the best specialist agent(s) without requiring explicit user mentions.

    Also in 5 projects that use this

  • xenitV1Skill

    bash-linux

    Bash/Linux terminal patterns. Critical commands, piping, error handling, scripting. Use when working on macOS or Linux systems.

    Also in 6 projects that use this

  • xenitV1Skill

    red-team-tactics

    Red team tactics principles based on MITRE ATT&CK. Attack phases, detection evasion, reporting.

    Also in 6 projects that use this

  • ChrisWilesSkill

    formik-patterns

    Formik form handling with validation patterns. Use when building forms, implementing validation, or handling form submission.

    Also in 3 projects that use this

  • SKB3002Skill

    nextjs-react-expert

    React and Next.js performance optimization from Vercel Engineering. Use when building React components, optimizing performance, eliminating waterfalls, reducing bundle size, reviewing code for performance issues, or implementing server/client-side optimizations.

    Also in 3 projects that use this

  • adybaSkill

    safe-workflow

    SAFe Agentic Workflow orchestrator. Teaches the full SAFe methodology adapted for AI agents: pattern discovery, exit states, quality gates, agent personas, and specs-driven development. Use when setting up a project with SAFe workflow, understanding the multi-agent methodology, or coordinating work across agent personas. Triggers: "SAFe workflow", "agent workflow", "safe harness", "specs-driven", "exit states", "quality gates", "multi-agent workflow".

    Also in 3 projects that use this

  • +4 more from xenitV1

The file

Files
SKILL.md

Skill : Écrire et exécuter des tests pour Fidely

Ressources disponibles

  • templates/composable.test.ts — template complet pour tester un composable IndexedDB
  • templates/component.test.ts — template complet pour tester un composant Vue
  • reference/test-examples.md — exemples réels tirés des tests du projet

Règles obligatoires

Toujours lancer les tests après toute modification de code

pnpm vitest run          # lancer tous les tests
pnpm vitest run app/composables/useDB.test.ts   # lancer un fichier spécifique

Toute nouvelle feature doit être accompagnée de tests :

  • Nouveau composable ou nouvelle fonction → fichier .test.ts co-localisé dans app/composables/
  • Nouveau composant avec logique → fichier .test.ts co-localisé dans app/components/
  • Nouvelle règle métier → test du cas nominal + cas limite

Quand lancer les tests :

  • Après chaque modification d'un composable (useDB.ts, useClient.ts)
  • Après chaque modification d'un composant (CardItem.vue, CardForm.vue)
  • Après chaque ajout ou modification d'une fonction dans utils/
  • Avant de marquer une tâche comme ✅ dans TASKS.md

Seuil d'acceptation :

  • 0 test en échec — jamais valider une tâche avec des tests rouges
  • Si un test échoue suite à un changement intentionnel, mettre à jour le test en même temps que le code

Setup (déjà configuré)

pnpm add -D @nuxt/test-utils vitest @vue/test-utils fake-indexeddb happy-dom

vitest.config.ts à la racine :

import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig({
  test: {
    environment: 'nuxt',
    setupFiles: ['./app/setup.ts']   // ← app/setup.ts (pas setup.test.ts)
  }
})

app/setup.ts :

import 'fake-indexeddb/auto'

Structure des fichiers de test

Les tests sont co-localisés avec les sources — même dossier que le fichier testé, suffixe .test.ts :

app/
├── composables/
│   ├── useDB.ts
│   ├── useDB.test.ts        ← test co-localisé
│   ├── useClient.ts
│   └── useClient.test.ts
├── components/
│   ├── CardItem.vue
│   ├── CardItem.test.ts     ← test co-localisé
│   ├── CardForm.vue
│   └── CardForm.test.ts
└── utils/
    ├── uuid.ts
    └── uuid.test.ts

Ne jamais créer de dossier tests/ séparé — les tests vivent à côté du code qu'ils testent.


Isolation IndexedDB entre les tests

Obligatoire : créer une nouvelle instance IDBFactory dans beforeEach pour que chaque test parte d'une DB vide :

import { IDBFactory } from 'fake-indexeddb'
import { resetDB } from '~/composables/useDB'

beforeEach(() => {
  globalThis.indexedDB = new IDBFactory()
  resetDB() // reset le cache de connexion dans useDB.ts
})

Ne jamais appeler indexedDB.deleteDatabase() — cette API est asynchrone et peut provoquer des timeouts si non attendue correctement.


Pattern pour tester les composables (useDB, useClient)

import { describe, it, expect, beforeEach } from 'vitest'
import { IDBFactory } from 'fake-indexeddb'
import { resetDB, useDB } from '~/composables/useDB'

beforeEach(() => {
  globalThis.indexedDB = new IDBFactory()
  resetDB()
})

describe('getAllCards', () => {
  it('retourne un tableau vide par défaut', async () => {
    const { getAllCards } = useDB()
    const cards = await getAllCards()
    expect(cards).toEqual([])
  })
})

describe('updatePoints', () => {
  it('ne descend jamais en dessous de 0', async () => {
    const { addCard, updatePoints } = useDB()
    const card = await addCard({ title: 'T', reward_points: 5, reward: 'R' })
    const points = await updatePoints('client-1', card.id, -99)
    expect(points).toBe(0)
  })
})

Pattern pour tester les composants Vue

import { describe, it, expect } from 'vitest'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import CardItem from '~/components/CardItem.vue'
import type { Card } from '~/composables/useDB'

const card: Card = {
  id: 'card-1',
  title: 'Carte café',
  reward_points: 10,
  reward: '1 café offert',
  created_at: Date.now()
}

describe('CardItem — merchant mode', () => {
  it('affiche le titre', async () => {
    const wrapper = await mountSuspended(CardItem, {
      props: { card, merchantMode: true }
    })
    expect(wrapper.text()).toContain('Carte café')
  })

  it('émet deleted avec le cardId au clic supprimer', async () => {
    const wrapper = await mountSuspended(CardItem, {
      props: { card, merchantMode: true }
    })
    await wrapper.find('[aria-label="Supprimer"]').trigger('click')
    expect(wrapper.emitted('deleted')?.[0]).toEqual(['card-1'])
  })
})

Structure describe / it standardisée

describe('NomDuSujet')
  describe('nomDeLaFonction ou contexte')
    it('fait X dans le cas normal')
    it('gère le cas limite Y')
    it('lance une erreur si Z')

Phrases it : toujours en anglais, décrivent le comportement attendu (pas l'implémentation).


Cas à toujours couvrir

Composables :

  • Happy path (cas nominal)
  • Valeurs limites (0 points, tableau vide, client inexistant)
  • Comportement idempotent (appels multiples retournent le même résultat)
  • Erreurs attendues (rejects.toThrow(...))

Composants :

  • Rendu avec les props minimales
  • Chaque emit déclenché par l'action utilisateur correspondante
  • États désactivés / conditionnels (:disabled, v-if)
  • Props optionnelles non fournies (comportement par défaut)

Mocking des APIs Nuxt

// useToast et navigateTo sont auto-mockés par @nuxt/test-utils dans l'environment 'nuxt'
// Pas besoin de mock manuel dans la plupart des cas

// Si besoin de vérifier un appel toast ou navigateTo :
import { mockNuxtImport } from '@nuxt/test-utils/runtime'

const toastMock = vi.fn()
mockNuxtImport('useToast', () => () => ({ add: toastMock }))

Confirmation en fin de tâche

✅ testing-patterns appliqués

  • Isolation : [beforeEach IDBFactory + resetDB en place]
  • Couverture : [happy path + cas limite + erreurs attendues]
  • Structure : [describe/it en anglais, comportemental]
  • Résultat : [pnpm vitest run → 0 test en échec]

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.