@rs-tech-hub/nestjs-test-starter
Test utilities for NestJS applications with GraphQL support. Create and remove test users and accounts for development and testing environments.
โจ Features
- ๐ฅ Test User Generation - Create multiple test users with accounts
- ๐งน Cleanup Utilities - Remove all test accounts
- ๐ Service Protected - GraphQL operations require service authentication
- ๐ GraphQL API - Mutations for test data management
- ๐ฒ Random Data - Generates unique test emails with random identifiers
- ๐ Batch Operations - Create up to 5 test users at once
- ๐๏ธ Token Cleanup - Clean expired activation, refresh, and password reset tokens
๐ Prerequisites
- Node.js >= 18
- TypeScript >= 5.1.0
- NestJS >= 11.1.6
- Prisma ORM v7.0+
- PostgreSQL database
- GraphQL support configured in your NestJS application
โ ๏ธ Important Notice
This package is intended for development and testing environments only. Do not use in production environments. Test users are identified by:
- Email addresses containing
@testing.com - Account type set to
TEST
๐ Quick Start
Getting Started
This module is included in the NestJS Auth Bundle demo application. You can find the complete implementation at:
๐ GitHub: RuffSantiDev
The demo application includes all required dependencies and provides a working example of how to use this module.
Module Registration
Import the module in your NestJS application:
import { Module } from "@nestjs/common";
import { TestStarterModule } from "@rs-tech-hub/nestjs-test-starter";
@Module({
imports: [TestStarterModule],
})
export class AppModule {}
๐ GraphQL API
Mutations
Create Test Users
mutation {
test_signUpTestUsers(count: 5) {
message
count
data
}
}
Response:
{
"data": {
"test_signUpTestUsers": {
"message": "Test users created successfully",
"count": 5,
"data": "[{\"activationKey\":\"...\",\"account\":{...}}]"
}
}
}
Remove Test Accounts
mutation {
test_removeTestUsers {
message
count
data
}
}
Response:
{
"data": {
"test_removeTestUsers": {
"message": "Test user removed successfully",
"count": 5,
"data": "{\"deletedAccounts\":{\"success\":true,\"count\":5}}"
}
}
}
Clean Expired Auth Tokens
Protected by: ServiceAuthGuard
Cleans up expired activation tokens, refresh tokens, and password reset tokens.
mutation {
test_cleanExpiredAuthTokens {
message
count
data
}
}
Response:
{
"data": {
"test_cleanExpiredAuthTokens": {
"message": "Expired auth tokens cleaned successfully",
"count": 42,
"data": "{\"success\":true,\"cleanedActivationTokens\":15,\"cleanedRefreshTokens\":20,\"cleanedPasswordResetTokens\":7}"
}
}
}
๐ป Service Usage
Inject the service in your own modules:
import { Injectable } from "@nestjs/common";
import { TestStarterService } from "@rs-tech-hub/nestjs-test-starter";
@Injectable()
export class TestingService {
constructor(private testService: TestStarterService) {}
async setupTestEnvironment() {
// Create 3 test users
const result = await this.testService.signUpTestUsers(3);
if (result.status === "COMPLETED" && result.output) {
console.log(`Created ${result.output.length} test users`);
return result.output;
}
throw new Error("Failed to create test users");
}
async teardownTestEnvironment() {
// Clean up all test accounts
const result = await this.testService.removeTestAccounts();
console.log(`Deleted ${result.deletedAccounts.count} accounts`);
return result;
}
async cleanupExpiredTokens() {
// Clean up expired tokens
const result = await this.testService.cleanExpiredAuthTokens();
console.log(`Cleaned ${result.cleanedActivationTokens} activation tokens`);
console.log(`Cleaned ${result.cleanedRefreshTokens} refresh tokens`);
console.log(
`Cleaned ${result.cleanedPasswordResetTokens} password reset tokens`,
);
return result;
}
}
๐ API Reference
TestStarterService Methods
signUpTestUsers(count?: number): Promise<ServiceResult<number, object[]>>
Create multiple test users with accounts.
Parameters:
count- Number of test users to create (default: 1, max: 5)
Returns: ServiceResult containing array of created accounts with activation keys
Generated User Format:
{
email: `testuser${rand}@testing.com`,
password: '5TrongPa$$word',
firstName: `Test ${rand}`,
lastName: `User`,
birthDate: '1972-07-12',
}
removeTestAccounts(): Promise<{ deletedAccounts }>
Remove all test accounts from the database (users are cascade deleted).
Returns:
{
deletedAccounts: {
success: boolean;
count: number;
}
}
Deletion Criteria:
- Accounts: Email contains
@testing.comOR account type isTEST
cleanExpiredAuthTokens(): Promise<object>
Clean up all expired activation tokens, refresh tokens, and password reset tokens.
Returns:
{
success: boolean;
cleanedActivationTokens?: number;
cleanedRefreshTokens?: number;
cleanedPasswordResetTokens?: number;
}
๐ Authentication
All GraphQL mutations require service authentication via ServiceAuthGuard:
@UseGuards(ServiceAuthGuard)
@Mutation(() => ResponseMessage)
async test_signUpTestUsers(@Args('count') count: number) {
// Protected operation
}
Ensure your application has the proper service authentication configured from @rs-tech-hub/nestjs-auth-core.
๐ Response Types
ResponseMessage
{
message: string; // Operation result message
count?: number; // Number of affected records
data?: string; // JSON stringified operation details
}
๐ก Best Practices
- Environment Restriction: Only use in development/testing environments
- Cleanup After Tests: Always remove test accounts after test completion
- Batch Creation: Create multiple users at once (up to 5) for efficiency
- Unique Emails: Random identifiers ensure unique test user emails
- Account Type: Test accounts are marked with
AccountType.TEST - Password Standard: All test users use password
5TrongPa$$word - Service Auth: Protect mutations with service authentication
- Token Cleanup: Use
cleanExpiredAuthTokensto clean up old tokens periodically
๐ Common Workflows
Setup and Teardown in Tests
describe("E2E Tests", () => {
let testService: TestStarterService;
let testUsers: any[];
beforeAll(async () => {
// Create test users before tests
const result = await testService.signUpTestUsers(5);
testUsers = result.output || [];
});
afterAll(async () => {
// Clean up after all tests
await testService.removeTestAccounts();
});
it("should run test with test users", () => {
// Your test logic here
expect(testUsers).toHaveLength(5);
});
});
Development Seeding
@Injectable()
export class SeedService {
constructor(private testService: TestStarterService) {}
async seedDevelopmentData() {
if (process.env.NODE_ENV !== "production") {
// Create test users for development
await this.testService.signUpTestUsers(10);
console.log("Development environment seeded with test users");
}
}
}
โ ๏ธ Generated Test Data
Each test user is created with:
- Email:
testuserXX@testing.com(XX is random number) - Password:
5TrongPa$$word - First Name:
Test XX - Last Name:
User - Birth Date:
1972-07-12 - Account Type:
TEST - Activation Key: Generated during signup
๏ฟฝ Release Notes
1.1.0
New Features
- Added
cleanExpiredAuthTokensmutation and service method - Cleans up expired activation tokens, refresh tokens, and password reset tokens
- Improved test account cleanup
1.0.2
- Fixes test_signUpTestUsers graphql endpoint
- Fixes test_removeTestUsers graphql endpoint
- Updates internal license handling
1.0.1
- Update TestServiceHandler
1.0.0
- Initial release
๐ License
This package requires a valid RS Tech Hub commercial license key. The software is protected by intellectual property laws and is provided under a commercial license agreement.
License Terms
- Commercial use requires a valid license key
- Development testing allowed with free trial
- Production deployment requires a paid license from https://rstechhub.gumroad.com
- Contact insights@rs-tech-hub.com for questions and inquiries
๐ Issues & Support
For issues, feature requests, or general questions contact insights@rs-tech-hub.com
Please review the docs first: https://rs-tech-hub.com/docs
๐ Website
Visit the website for more information: https://rs-tech-hub.com
Newsletter
Stay updated with the latest news and updates by subscribing to the newsletter at https://rs-tech-hub.kit.com/newsletter
Built with โค๏ธ by RS Tech Hub