test: add e2e jwt smoke tests and load env for jest

This commit is contained in:
gieacrm 2026-01-22 12:29:31 +01:00
parent bc3e929759
commit bb4fa9cfb3
4 changed files with 60 additions and 2 deletions

View File

@ -69,11 +69,14 @@
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"rootDir": ".",
"testRegex": "test/.*\\.(spec|e2e-spec)\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"setupFiles": [
"<rootDir>/test/jest.setup.ts"
],
"collectCoverageFrom": [
"**/*.(t|j)s"
],

View File

@ -3,6 +3,7 @@
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"setupFiles": ["<rootDir>/jest.setup.ts"],
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}

3
test/jest.setup.ts Normal file
View File

@ -0,0 +1,3 @@
import * as dotenv from 'dotenv';
dotenv.config({ path: '/opt/crm/.env' });

51
test/jwt.e2e-spec.ts Normal file
View File

@ -0,0 +1,51 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from '../src/app.module';
describe('Auth/JWT (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleRef.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('/health (GET) should be public', async () => {
await request(app.getHttpServer()).get('/health').expect(200);
});
it('/auth/login (POST) should return tokens', async () => {
const res = await request(app.getHttpServer())
.post('/auth/login')
.send({ email: 'admin@crm.local', password: 'admin123' })
.expect(201);
expect(res.body).toHaveProperty('accessToken');
expect(res.body).toHaveProperty('refreshToken');
expect(res.body).toHaveProperty('user');
expect(res.body.user).toHaveProperty('email', 'admin@crm.local');
});
it('/users (GET) should be protected', async () => {
const login = await request(app.getHttpServer())
.post('/auth/login')
.send({ email: 'admin@crm.local', password: 'admin123' })
.expect(201);
const token = login.body.accessToken as string;
await request(app.getHttpServer())
.get('/users')
.set('Authorization', `Bearer ${token}`)
.expect(200);
});
});