Skip to content

Commit a1a579a

Browse files
committed
✔️test(categoria): implementa testes de unidade automatizados no manipulador de rota | Parte 32
- Configuranda o Módulo "main" Para Ser Reconhecido no "Vitest" - Configura Testes de Unidade no Manipulador de Rota "Categoria" - Implementa Testes de Unidade na Rota "GET api/v1/categorias/:id" - Implementa Testes de Unidade na Rota "POST api/v1/categorias"
1 parent 92d9098 commit a1a579a

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed

package-lock.json

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@
3636
"@types/express": "^4.17.21",
3737
"@types/morgan": "^1.9.9",
3838
"@types/node": "^20.3.3",
39+
"@types/supertest": "^2.0.16",
3940
"@vitest/ui": "^0.33.0",
4041
"nodemon": "^3.0.1",
4142
"prisma": "^5.3.1",
4243
"sucrase": "^3.32.0",
44+
"supertest": "^6.3.3",
4345
"tsconfig-paths": "^4.2.0",
4446
"typescript": "^5.1.6",
4547
"vitest": "^0.33.0",
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import express, { Application } from "express";
2+
import { afterEach, beforeAll, describe, expect, test, vi } from "vitest";
3+
import { MockProxy, mock, mockReset } from "vitest-mock-extended";
4+
import { RecuperarCategoriaPorIdExpressController } from "./controllers/recuperar-categoria-por-id.express.controller";
5+
import { InserirCategoriaExpressController } from "./controllers/inserir-categoria.express.controller";
6+
import { CriarCategoriaProps, ICategoria } from "@modules/catalogo/domain/categoria/categoria.types";
7+
import request from 'supertest';
8+
9+
let appMock: Application;
10+
let recuperarCategoriaPorIdControllerMock: MockProxy<RecuperarCategoriaPorIdExpressController>;
11+
let inserirCategoriaControllerMock: MockProxy<InserirCategoriaExpressController>;
12+
13+
describe('[REST] Rotas Express: Categoria', () => {
14+
15+
beforeAll(async () => {
16+
appMock = express();
17+
recuperarCategoriaPorIdControllerMock = mock<RecuperarCategoriaPorIdExpressController>();
18+
inserirCategoriaControllerMock = mock<InserirCategoriaExpressController>();
19+
});
20+
21+
afterEach(() => {
22+
vi.restoreAllMocks();
23+
mockReset(recuperarCategoriaPorIdControllerMock);
24+
mockReset(inserirCategoriaControllerMock);
25+
});
26+
27+
describe('GET api/v1/categorias/:id', () => {
28+
29+
test('Deve Retornar Status 200 e um Objeto do Tipo ICategoria no formato JSON', async () => {
30+
31+
//Dado (Given)
32+
const categoriaInputDTO: ICategoria = {
33+
id: "80830927-8c3e-4db9-9ddf-30ea191f139b",
34+
nome: "Cama"
35+
}
36+
37+
recuperarCategoriaPorIdControllerMock.recuperar.mockImplementation(async (request, response, next) => {
38+
response.status(200).json(categoriaInputDTO);
39+
});
40+
41+
appMock.use('/api/v1/categorias/:id', recuperarCategoriaPorIdControllerMock.recuperar);
42+
43+
//Quando (When)
44+
const response = await request(appMock)
45+
.get('/api/v1/categorias/80830927-8c3e-4db9-9ddf-30ea191f139b')
46+
47+
//Então (Then
48+
expect(response.status).toEqual(200);
49+
expect(response.headers["content-type"]).toMatch(/json/);
50+
expect(response.body).toEqual(categoriaInputDTO);
51+
52+
});
53+
54+
});
55+
56+
describe('POST api/v1/categorias', () => {
57+
58+
test('Deve Retornar Status 200 e um Objeto do Tipo ICategoria no formato JSON', async () => {
59+
60+
//Dado (Given)
61+
const categoriaInputDTO: CriarCategoriaProps = {
62+
nome: "Cama"
63+
};
64+
65+
const categoriaOutputDTO: ICategoria = {
66+
id: "80830927-8c3e-4db9-9ddf-30ea191f139b",
67+
nome: "Cama"
68+
}
69+
70+
inserirCategoriaControllerMock.inserir.mockImplementation(async (request, response, next) => {
71+
response.status(200).json(categoriaOutputDTO);
72+
});
73+
74+
appMock.use('/api/v1/categorias', inserirCategoriaControllerMock.inserir); // Cast to any for mocking purposes
75+
76+
//Quando (When)
77+
const response = await request(appMock)
78+
.post('/api/v1/categorias')
79+
.send(categoriaInputDTO)
80+
81+
//Então (Then
82+
expect(response.status).toEqual(200);
83+
expect(response.headers["content-type"]).toMatch(/json/);
84+
expect(response.body).toEqual(categoriaOutputDTO);
85+
86+
});
87+
88+
});
89+
90+
});

vite.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default defineConfig({
1616
find: "@shared",
1717
replacement: path.resolve(__dirname, "src/shared"),
1818
},
19+
{
20+
find: "@main",
21+
replacement: path.resolve(__dirname, "src/main"),
22+
}
1923
]
2024
}
2125
})

0 commit comments

Comments
 (0)