|
1 | | -import { randomBytes } from 'node:crypto'; |
2 | | -import type { IncomingMessage, Server, ServerResponse } from 'node:http'; |
| 1 | +import { randomBytes } from 'crypto'; |
3 | 2 |
|
4 | | -import type { NestExpressApplication } from '@nestjs/platform-express'; |
5 | | -import request from 'supertest'; |
6 | | -import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { beforeEach } from 'vitest'; |
7 | 4 |
|
8 | | -import appContainer from './app.js'; |
| 5 | +import { e2e } from '../src/testing/index.js'; |
9 | 6 |
|
10 | | -import type { BaseEnv } from '../src/schemas/env.schema.js'; |
| 7 | +import type { EndToEndContext } from '../src/testing/index.js'; |
11 | 8 | import type { CreateCatDto } from './cats/dto/create-cat.dto.js'; |
12 | 9 |
|
13 | | -const env = { |
14 | | - API_PORT: '5500', |
15 | | - DEBUG: 'false', |
16 | | - MONGO_URI: 'mongodb://localhost:27017', |
17 | | - NODE_ENV: 'test', |
18 | | - SECRET_KEY: '2622d72669dd194b98cffd9098b0d04b', |
19 | | - VERBOSE: 'false' |
20 | | -} satisfies { [K in keyof BaseEnv]?: string }; |
21 | | - |
22 | | -describe('e2e (example)', () => { |
23 | | - let app: NestExpressApplication; |
24 | | - let server!: Server<typeof IncomingMessage, typeof ServerResponse>; |
25 | | - |
26 | | - beforeAll(async () => { |
27 | | - Object.entries(env).forEach(([key, value]) => { |
28 | | - vi.stubEnv(key, value); |
29 | | - }); |
30 | | - app = appContainer.getApplicationInstance(); |
31 | | - await app.init(); |
32 | | - server = app.getHttpServer(); |
33 | | - }); |
34 | | - |
35 | | - afterAll(async () => { |
36 | | - if (app) { |
37 | | - await app.close(); |
38 | | - app.flushLogs(); |
39 | | - } |
40 | | - }); |
41 | | - |
42 | | - describe('/spec.json', () => { |
43 | | - it('should configure the documentation', async () => { |
44 | | - const response = await request(server).get('/spec.json'); |
| 10 | +e2e((describe) => { |
| 11 | + describe('/spec.json', (it) => { |
| 12 | + it('should configure the documentation', async ({ api, expect }) => { |
| 13 | + const response = await api.get('/spec.json'); |
45 | 14 | expect(response.status).toBe(200); |
46 | 15 | }); |
47 | 16 | }); |
48 | 17 |
|
49 | | - describe('/auth/login', () => { |
50 | | - it('should return status code 400 if the request body does not include login credentials', async () => { |
51 | | - const response = await request(server).post('/v1/auth/login'); |
| 18 | + describe('/auth/login', (it) => { |
| 19 | + it('should return status code 400 if the request body does not include credentials', async ({ api, expect }) => { |
| 20 | + const response = await api.post('/v1/auth/login'); |
52 | 21 | expect(response.status).toBe(400); |
53 | 22 | }); |
54 | | - it('should return status code 400 if the request body does not include a username', async () => { |
55 | | - const response = await request(server).post('/v1/auth/login').send({ username: 'admin' }); |
| 23 | + it('should return status code 400 if the request body does not include a username', async ({ api, expect }) => { |
| 24 | + const response = await api.post('/v1/auth/login').send({ username: 'admin' }); |
56 | 25 | expect(response.status).toBe(400); |
57 | 26 | }); |
58 | | - it('should return status code 400 if the request body does not include a password', async () => { |
59 | | - const response = await request(server).post('/v1/auth/login').send({ password: 'password' }); |
| 27 | + it('should return status code 400 if the request body does not include a password', async ({ api, expect }) => { |
| 28 | + const response = await api.post('/v1/auth/login').send({ password: 'password' }); |
60 | 29 | expect(response.status).toBe(400); |
61 | 30 | }); |
62 | | - it('should return status code 400 if the request body includes a username and password, but are empty strings', async () => { |
63 | | - const response = await request(server).post('/v1/auth/login').send({ password: '', username: '' }); |
| 31 | + it('should return status code 400 if username and password are empty strings', async ({ api, expect }) => { |
| 32 | + const response = await api.post('/v1/auth/login').send({ password: '', username: '' }); |
64 | 33 | expect(response.status).toBe(400); |
65 | 34 | }); |
66 | | - it('should return status code 400 if the request body includes a username and password, but password is a number', async () => { |
67 | | - const response = await request(server).post('/v1/auth/login').send({ password: 123, username: 'admin' }); |
| 35 | + it('should return status code 400 if password is a number', async ({ api, expect }) => { |
| 36 | + const response = await api.post('/v1/auth/login').send({ password: 123, username: 'admin' }); |
68 | 37 | expect(response.status).toBe(400); |
69 | 38 | }); |
70 | | - it('should return status code 401 if the user does not exist', async () => { |
71 | | - const response = await request(server).post('/v1/auth/login').send({ password: 'password', username: 'user' }); |
| 39 | + it('should return status code 401 if the user does not exist', async ({ api, expect }) => { |
| 40 | + const response = await api.post('/v1/auth/login').send({ password: 'password', username: 'user' }); |
72 | 41 | expect(response.status).toBe(401); |
73 | 42 | }); |
74 | | - it('should return status code 200 and an access token if the credentials are correct', async () => { |
75 | | - const response = await request(server).post('/v1/auth/login').send({ password: 'password', username: 'admin' }); |
| 43 | + it('should return status code 200 and an access token if the credentials are correct', async ({ api, expect }) => { |
| 44 | + const response = await api.post('/v1/auth/login').send({ password: 'password', username: 'admin' }); |
76 | 45 | expect(response.status).toBe(200); |
77 | 46 | expect(response.body).toStrictEqual({ |
78 | 47 | accessToken: expect.stringMatching(/^[A-Za-z0-9-_]+\.([A-Za-z0-9-_]+)\.[A-Za-z0-9-_]+$/) |
79 | 48 | }); |
80 | 49 | }); |
81 | 50 | }); |
82 | 51 |
|
83 | | - describe('/cats', () => { |
| 52 | + describe('/cats', (it) => { |
84 | 53 | let accessToken: string; |
85 | 54 |
|
86 | | - beforeAll(async () => { |
87 | | - const response = await request(server).post('/v1/auth/login').send({ password: 'password', username: 'admin' }); |
| 55 | + beforeEach<EndToEndContext>(async ({ api }) => { |
| 56 | + const response = await api.post('/v1/auth/login').send({ password: 'password', username: 'admin' }); |
88 | 57 | accessToken = response.body.accessToken; |
89 | 58 | }); |
90 | 59 |
|
91 | | - it('should return status code 401 if there is no access token provided', async () => { |
92 | | - const response = await request(server).get('/v1/cats'); |
| 60 | + it('should return status code 401 if there is no access token provided', async ({ api, expect }) => { |
| 61 | + const response = await api.get('/v1/cats'); |
93 | 62 | expect(response.status).toBe(401); |
94 | 63 | }); |
95 | 64 |
|
96 | | - it('should return status code 401 if there is an invalid access token provided', async () => { |
97 | | - const response = await request(server) |
98 | | - .get('/v1/cats') |
99 | | - .set('Authorization', `Bearer ${randomBytes(12).toString('base64')}`); |
| 65 | + it('should return status code 401 if there is an invalid access token provided', async ({ api, expect }) => { |
| 66 | + const response = await api.get('/v1/cats').set('Authorization', `Bearer ${randomBytes(12).toString('base64')}`); |
100 | 67 | expect(response.status).toBe(401); |
101 | 68 | }); |
102 | 69 |
|
103 | | - it('should allow a GET request', async () => { |
104 | | - const response = await request(server).get('/v1/cats').set('Authorization', `Bearer ${accessToken}`); |
| 70 | + it('should allow a GET request', async ({ api, expect }) => { |
| 71 | + const response = await api.get('/v1/cats').set('Authorization', `Bearer ${accessToken}`); |
105 | 72 | expect(response.status).toBe(200); |
106 | 73 | }); |
107 | 74 |
|
108 | | - it('should allow a POST request', async () => { |
109 | | - const response = await request(server) |
| 75 | + it('should allow a POST request', async ({ api, expect }) => { |
| 76 | + const response = await api |
110 | 77 | .post('/v1/cats') |
111 | 78 | .set('Authorization', `Bearer ${accessToken}`) |
112 | 79 | .send({ |
|
0 commit comments