Skip to content

Commit

Permalink
Utilise supertest pour tester le serveur Express
Browse files Browse the repository at this point in the history
... car lors de notre phase de test, un problème de concurrence entre les tests se pose. Notre implémentation ne permettait pas d'avoir plusieurs instances d'Express en parallèle. Le `fetch` d'un test A tapait le serveur d'un test B.
`Supertest` semble être fait pour gérer ce genre de problème.
  • Loading branch information
ThibaudMZN committed Oct 8, 2024
1 parent 4971bcc commit 400f96e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion anssi-nis2-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"jest-extended": "^4.0.2",
"prettier": "^2.8.8",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"supertest": "^6.3.4",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
Expand Down
3 changes: 2 additions & 1 deletion anssi-nis2-api/src/serveur.express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { routesApi } from "./routes/routesApi";
export async function creeServeurExpress(
port: number,
dependances: DependanceServeur,
): Promise<ServeurMonEspaceNIS2> {
): Promise<ServeurMonEspaceNIS2 & { app: Express }> {
const app = express();

dependances.adaptateurGestionErreur.initialise(app);
Expand All @@ -37,6 +37,7 @@ export async function creeServeurExpress(
serveur = app.listen(port, callbackSucces);
},
arrete: () => serveur.close(),
app,
};
}

Expand Down
24 changes: 12 additions & 12 deletions anssi-nis2-api/src/test/routes/routesApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import {
ImplementationDuServeur,
ServeurMonEspaceNIS2,
} from "../../serveur.types";
import { creeServeur } from "../../serveur";
import { AdaptateurPersistanceMemoire } from "../../adaptateurs/adaptateurPersistance.memoire";
import { DonneesFormulaireSimulateur } from "~core/src/Domain/Simulateur/services/DonneesFormulaire/DonneesFormulaire.definitions";
import { donneesFormulaireSimulateurVide } from "~core/src/Domain/Simulateur/services/DonneesFormulaire/DonneesFormulaire.constantes";
Expand All @@ -16,9 +11,11 @@ import { CreeInformationsEmailDto } from "../../informations-emails/dto/cree-inf
import { AdaptateurCrmMemoire } from "../../adaptateurs/adaptateurCrm.memoire";
import { AdaptateurGestionErreurMemoire } from "../../adaptateurs/adaptateurGestionErreur.memoire";
import { AdaptateurProtectionMemoire } from "../../adaptateurs/adaptateurProtection.memoire";
import { creeServeurExpress } from "../../serveur.express";
import { Express } from "express";

describe("Le routeur '/api/", () => {
let serveur: ServeurMonEspaceNIS2;
let serveur: { app: Express };
let adaptateurPersistance: AdaptateurPersistanceMemoire;
let adaptateurJournal: AdaptateurJournalMemoire;
let adaptateurCrm: AdaptateurCrmMemoire;
Expand All @@ -28,19 +25,17 @@ describe("Le routeur '/api/", () => {
adaptateurPersistance = new AdaptateurPersistanceMemoire();
adaptateurJournal = new AdaptateurJournalMemoire();
adaptateurCrm = new AdaptateurCrmMemoire();
serveur = await creeServeur(1234, ImplementationDuServeur.Express, {
serveur = await creeServeurExpress(1234, {
adaptateurPersistance,
adaptateurJournal,
adaptateurCrm,
adaptateurGestionErreur: new AdaptateurGestionErreurMemoire(),
adaptateurProtection: new AdaptateurProtectionMemoire(),
});
serveur.ecoute();
});

afterEach(() => {
jest.useRealTimers();
serveur.arrete();
});

describe("sur la route POST '/api/simulateur-reponse'", () => {
Expand All @@ -53,6 +48,7 @@ describe("Le routeur '/api/", () => {
};

const reponse = await POST(
serveur.app,
"/api/simulateur-reponse",
donneesFormulaireSimulateurVide,
);
Expand Down Expand Up @@ -87,7 +83,11 @@ describe("Le routeur '/api/", () => {
evenementRecu = evenement;
};

await POST(`/api/simulateur-reponse`, donneesFormulaireSimulateurVide);
await POST(
serveur.app,
`/api/simulateur-reponse`,
donneesFormulaireSimulateurVide,
);

expect(evenementRecu.type).toBe("REPONSE_SIMULATEUR_RECUE");
expect(evenementRecu.date).toEqual(aujourdhui);
Expand Down Expand Up @@ -118,7 +118,7 @@ describe("Le routeur '/api/", () => {
donneesRecues = donnees;
};

const reponse = await POST("/api/informations-emails", {
const reponse = await POST(serveur.app, "/api/informations-emails", {
email: "jean@mail.com",
nomOrganisation: "Entreprise",
accepteInfolettreNis2: true,
Expand All @@ -142,7 +142,7 @@ describe("Le routeur '/api/", () => {
inscription = donnees;
};

await POST("/api/informations-emails", {
await POST(serveur.app, "/api/informations-emails", {
email: "jean@mail.com",
nomOrganisation: "Entreprise",
accepteInfolettreNis2: true,
Expand Down
14 changes: 5 additions & 9 deletions anssi-nis2-api/src/test/utilitaires/http.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export const POST = async (path: string, body: object) =>
await fetch(`http://localhost:1234${path}`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
import * as request from "supertest";
import { Express } from "express";

export const POST = async (app: Express, path: string, body: object) =>
await request(app).post(path).send(body).set("Accept", "application/json");
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 400f96e

Please sign in to comment.