feat(tests): add utility functions for testing with NestJS
Add test utilities to facilitate creating test apps, test users, generating tokens, and cleaning up test data in the backend codebase.
This commit is contained in:
parent
f30f973178
commit
52d74a754c
72
backend/test/test-utils.ts
Normal file
72
backend/test/test-utils.ts
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
import { INestApplication } from '@nestjs/common';
|
||||||
|
import { JwtService } from '@nestjs/jwt';
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { AppModule } from '../src/app.module';
|
||||||
|
import { UsersService } from '../src/modules/users/services/users.service';
|
||||||
|
import { CreateUserDto } from '../src/modules/users/dto/create-user.dto';
|
||||||
|
import { AuthService } from '../src/modules/auth/services/auth.service';
|
||||||
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a test application
|
||||||
|
*/
|
||||||
|
export async function createTestApp(): Promise<INestApplication> {
|
||||||
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||||
|
imports: [AppModule],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
const app = moduleFixture.createNestApplication();
|
||||||
|
|
||||||
|
// Apply the same middleware as in main.ts
|
||||||
|
app.useGlobalPipes(
|
||||||
|
new ValidationPipe({
|
||||||
|
whitelist: true,
|
||||||
|
transform: true,
|
||||||
|
forbidNonWhitelisted: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set global prefix as in main.ts
|
||||||
|
app.setGlobalPrefix('api');
|
||||||
|
|
||||||
|
await app.init();
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a test user
|
||||||
|
*/
|
||||||
|
export async function createTestUser(app: INestApplication) {
|
||||||
|
const usersService = app.get(UsersService);
|
||||||
|
|
||||||
|
const createUserDto: CreateUserDto = {
|
||||||
|
name: `Test User ${uuidv4().substring(0, 8)}`,
|
||||||
|
githubId: `github-${uuidv4().substring(0, 8)}`,
|
||||||
|
avatar: 'https://example.com/avatar.png',
|
||||||
|
metadata: { email: 'test@example.com' },
|
||||||
|
};
|
||||||
|
|
||||||
|
return await usersService.create(createUserDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate JWT tokens for a user
|
||||||
|
*/
|
||||||
|
export async function generateTokensForUser(app: INestApplication, userId: string) {
|
||||||
|
const authService = app.get(AuthService);
|
||||||
|
return await authService.generateTokens(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up test data
|
||||||
|
*/
|
||||||
|
export async function cleanupTestData(app: INestApplication, userId: string) {
|
||||||
|
const usersService = app.get(UsersService);
|
||||||
|
try {
|
||||||
|
await usersService.remove(userId);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to clean up test user ${userId}:`, error.message);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user