@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.
๐ Table of Contents
- ๐ License
- โจ Features
- ๐ Prerequisites
- โ ๏ธ Important Notice
- ๐ฆ Installation
- ๐ Module Registration
- ๐ GraphQL API
- ๐ป Service Usage
- ๐ API Reference
- ๐ Authentication
- ๐ Response Types
- ๐ก Best Practices
- ๐ Common Workflows
- โ ๏ธ Generated Test Data
- ๐ License
- ๐ Release Notes
- ๐ Support
๐ Related Documentation
- Getting Started - Initial setup and testing
- Auth Starter - Authentication system used by test users
- Account Starter - Account creation
- User Module - User management
- Auth Core - Service authentication
๐ License
This package requires a valid commercial license. A valid license key must be configured to use this package.
Visit https://rstechhub.gumroad.com to purchase a license.
โจ Features
- ๐ฅ Test User Generation - Create multiple test users with accounts
- ๐งน Cleanup Utilities - Remove all test users and 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 10 test users at once
๐ Prerequisites
- Node.js โฅ 18
- TypeScript โฅ 5.1.0
- NestJS โฅ 11.1.6
- Prisma ORM v7.0+
- 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
๐ฆ Installation
npm install @rs-tech-hub/nestjs-test-starter \
@rs-tech-hub/nestjs-account-starter \
@rs-tech-hub/nestjs-auth-core \
@rs-tech-hub/nestjs-auth-starter \
@rs-tech-hub/nestjs-license-validator \
@rs-tech-hub/nestjs-prisma \
@rs-tech-hub/nestjs-service-operation \
@rs-tech-hub/nestjs-user
yarn add @rs-tech-hub/nestjs-test-starter \
@rs-tech-hub/nestjs-account-starter \
@rs-tech-hub/nestjs-auth-core \
@rs-tech-hub/nestjs-auth-starter \
@rs-tech-hub/nestjs-license-validator \
@rs-tech-hub/nestjs-prisma \
@rs-tech-hub/nestjs-service-operation \
@rs-tech-hub/nestjs-user
pnpm add @rs-tech-hub/nestjs-test-starter \
@rs-tech-hub/nestjs-account-starter \
@rs-tech-hub/nestjs-auth-core \
@rs-tech-hub/nestjs-auth-starter \
@rs-tech-hub/nestjs-license-validator \
@rs-tech-hub/nestjs-prisma \
@rs-tech-hub/nestjs-service-operation \
@rs-tech-hub/nestjs-user
๐ 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 Users
mutation {
test_removeTestUsers {
message
count
data
}
}
Response:
{
"data": {
"test_removeTestUsers": {
"message": "Test user removed successfully",
"count": 5,
"data": "{\"deletedUsers\":{\"success\":true,\"count\":5},\"deletedAccounts\":{\"success\":true,\"count\":5}}"
}
}
}
๐ป 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 users and accounts
const result = await this.testService.removeTestUsers();
console.log(`Deleted ${result.deletedUsers.count} users`);
console.log(`Deleted ${result.deletedAccounts.count} accounts`);
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: 10)
Returns: ServiceResult containing array of created accounts with activation keys
Generated User Format:
{
email: `testuser${random}@testing.com`,
password: '123123123123',
firstName: `Test ${random}`,
lastName: 'User',
birthDate: '1972-07-12'
}
removeTestUsers(): Promise<{ deletedUsers, deletedAccounts }>
Remove all test users and accounts from the database.
Returns:
{
deletedUsers: {
success: boolean;
count: number;
}
deletedAccounts: {
success: boolean;
count: number;
}
}
Deletion Criteria:
- Users: Email contains
@testing.com - Accounts: Email contains
@testing.comOR account type isTEST
๐ 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 users after test completion
- Batch Creation: Create multiple users at once (up to 10) 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
123123123123 - Service Auth: Protect mutations with service authentication
๐ 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.removeTestUsers();
});
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:
123123123123 - First Name:
Test XX - Last Name:
User - Birth Date:
1972-07-12 - Account Type:
TEST - Activation Key: Generated during signup
๐ License
This package requires a valid commercial license. See LICENSE.txt for details. By using this software, you agree to the terms outlined in the Software License Agreement (SLA.md). The license grants you specific rights to use, modify, and deploy the software within the scope defined in the agreement. For full terms, conditions, and restrictions, please refer to the Software License Agreement.
๐ Release Notes
1.0.0
- Initial release
1.0.1
- Update TestServiceHandler
1.0.2
- Fixes test_signUpTestUsers graphql endpoint
- Fixes Test_removeTestUsers graphql endpoint
- Updates internal license handling
๐ Support
For technical support and inquiries:
- Email: insights@rs-tech-hub.com
- Website: https://rs-tech-hub.com