web:api:prisma-fastify:p6

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
web:api:prisma-fastify:p6 [2026/04/01 08:26] – [6. Bonnes pratiques] jcheronweb:api:prisma-fastify:p6 [2026/04/01 09:28] (Version actuelle) – [1. Rendre l'application testable] jcheron
Ligne 15: Ligne 15:
 <sxh bash;gutter:false> <sxh bash;gutter:false>
 npm install -D vitest supertest npm install -D vitest supertest
 +npm install -D @types/supertest
 </sxh> </sxh>
  
Ligne 48: Ligne 49:
 on exporte une fonction pour créer une instance propre à chaque test on exporte une fonction pour créer une instance propre à chaque test
 </WRAP> </WRAP>
 +
 +Mettre le lancement du serveur dans un fichier à part :
 +
 +<sxh ts>
 +import {buildApp} from "./app";
 +
 +const app = buildApp()
 +
 +app.listen({ port: 3000 }).then(() => {
 +    console.log("Server running on http://localhost:3000")
 +})
 +</sxh>
  
 ===== 2. Premier test ===== ===== 2. Premier test =====
 Créer ''/tests/health.test.ts'' : Créer ''/tests/health.test.ts'' :
 <sxh ts> <sxh ts>
-import { describe, it, expect } from "vitest" +let app:FastifyInstance
-import request from "supertest" +
-import { buildApp } from "../src/app"+
  
 +beforeAll(async () => {
 +    app = buildApp()
 +    await app.ready()
 +})
 +
 +afterAll(async () => {
 +    await app.close()
 +})
 describe("GET /health", () => { describe("GET /health", () => {
-  it("should return status ok", async () => { +    it("should return status ok", async () => { 
-    const app = buildApp() +        const res = await request(app.server) 
-const res = await request(app.server) +            .get("/health")
-  .get("/health")+
  
-expect(res.status).toBe(200) +        expect(res.status).toBe(200) 
-expect(res.body.status).toBe("ok"+        expect(res.body.status).toBe("ok"
-  })+    })
 }) })
 </sxh> </sxh>
  • web/api/prisma-fastify/p6.1775024805.txt.gz
  • Dernière modification : il y a 5 jours
  • de jcheron