# Testing React (React / Next.js) > React Testing Library, Jest, Vitest, component testing, mocking, user interactions, coverage - 20 questions d'entretien - Confirmé - [Questions d'entretien: React / Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien.md) ## 1. Quelle est la différence principale entre getBy, queryBy et findBy dans React Testing Library ? **Réponse** getBy lance une erreur si l'élément n'existe pas (assertions synchrones), queryBy retourne null si l'élément n'existe pas (vérifier l'absence), et findBy retourne une Promise pour les éléments asynchrones. Utiliser getBy par défaut pour les éléments qui doivent exister, queryBy pour vérifier qu'un élément n'est pas présent, et findBy pour attendre l'apparition d'un élément après un délai. ## 2. Quel est l'avantage principal de getByRole par rapport à getByTestId ? **Réponse** getByRole encourage les bonnes pratiques d'accessibilité en ciblant les éléments par leur rôle ARIA sémantique (button, textbox, heading), ce qui améliore l'accessibilité du composant pour les lecteurs d'écran. getByTestId nécessite d'ajouter des attributs data-testid artificiels qui n'apportent aucune valeur pour l'utilisateur final. React Testing Library recommande de privilégier getByRole, getByLabelText et getByText avant getByTestId. ## 3. Comment tester l'affichage conditionnel d'un élément qui n'est PAS présent dans le DOM ? **Réponse** Utiliser queryBy pour vérifier l'absence d'un élément, car il retourne null au lieu de lancer une erreur. Exemple : expect(screen.queryByText('Error')).toBeNull() ou expect(screen.queryByText('Error')).not.toBeInTheDocument(). getBy lancerait une erreur et ferait échouer le test, tandis que findBy attend l'apparition de l'élément (timeout si absent). ## 17 questions supplémentaires disponibles - Pourquoi React Testing Library recommande-t-il de NE PAS tester les détails d'implémentation ? - Quelle est la différence entre render et screen dans React Testing Library ? S'inscrire gratuitement: https://sharpskill.dev/fr/login ## Autres sujets d'entretien React / Next.js - [Les bases de JavaScript](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/javascript-essentials.md): 25 questions, Junior - [Fondamentaux React](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-fundamentals.md): 20 questions, Junior - [React Hooks](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-hooks.md): 22 questions, Junior - [Cycle de vie des composants](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/component-lifecycle.md): 18 questions, Junior - [React Router](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-router.md): 20 questions, Junior - [Gestion d'état avec Context](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/state-management-context.md): 18 questions, Junior - [Formulaires & Composants contrôlés](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/forms-controlled-components.md): 20 questions, Confirmé - [Fetching de données & API](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/fetching-data-api.md): 20 questions, Confirmé - [React Query (TanStack Query)](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-query-tanstack.md): 20 questions, Confirmé - [Styling & CSS-in-JS](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/styling-css-in-js.md): 18 questions, Confirmé - [Fondamentaux Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-fundamentals.md): 25 questions, Confirmé - [TypeScript avec React](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/typescript-react.md): 20 questions, Confirmé - [Data Fetching Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-data-fetching.md): 24 questions, Confirmé - [Server Actions Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-server-actions.md): 20 questions, Confirmé - [Routing & Navigation Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-routing-navigation.md): 22 questions, Confirmé - [API Routes Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-api-routes.md): 20 questions, Confirmé - [Metadata & SEO Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-metadata-seo.md): 18 questions, Confirmé - [Middleware & Auth Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-middleware-auth.md): 22 questions, Confirmé - [Zustand State Management](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/zustand-state-management.md): 18 questions, Confirmé - [Optimisation Performance React](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-performance-optimization.md): 22 questions, Senior - [Error Boundaries & Error Handling](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-error-boundaries.md): 18 questions, Senior - [Advanced React Patterns](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/advanced-react-patterns.md): 20 questions, Senior - [Fonctionnalités avancées Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-advanced-features.md): 24 questions, Senior - [Déploiement & Production Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-deployment-production.md): 20 questions, Senior - [Architecture & Design Patterns](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-architecture-patterns.md): 22 questions, Senior - [React Server Components](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-server-components.md): 26 questions, Senior - [Internationalisation Next.js](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/nextjs-internationalization.md): 20 questions, Senior - [Sécurité & Best Practices React](https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-security-best-practices.md): 22 questions, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/fr/technologies/react-next/questions-entretien/react-testing