@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

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

๐Ÿ”’ 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 users after test completion
  3. Batch Creation: Create multiple users at once (up to 10) 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 123123123123
  7. 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: