@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.com OR account type is TEST

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

  1. Environment Restriction: Only use in development/testing environments
  2. Cleanup After Tests: Always remove test accounts after test completion
  3. Batch Creation: Create multiple users at once (up to 5) for efficiency
  4. Unique Emails: Random identifiers ensure unique test user emails
  5. Account Type: Test accounts are marked with AccountType.TEST
  6. Password Standard: All test users use password 5TrongPa$$word
  7. Service Auth: Protect mutations with service authentication
  8. Token Cleanup: Use cleanExpiredAuthTokens to 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 cleanExpiredAuthTokens mutation 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

๐Ÿ› 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