@nestjs-cognito/testing

Testing utilities to mock Cognito authentication in NestJS applications.

Installation

npm install -D @nestjs-cognito/testing

Functions

createCognitoTestingModule(options?: TestingModuleOptions)

Creates a testing module with mocked Cognito authentication.

Parameters:

interface TestingModuleOptions {
  enabled?: boolean;
  user?: CognitoTestUser;
  jwtVerifier?: CognitoJwtVerifierOptions;
  identityProvider?: CognitoIdentityProviderOptions;
}

Returns: DynamicModule

Usage:

Test.createTestingModule({
  imports: [
    createCognitoTestingModule({
      user: {
        username: 'testuser',
        email: 'test@example.com',
        groups: ['users'],
      },
    }),
  ],
})

Types

CognitoTestUser

interface CognitoTestUser {
  username: string;
  email?: string;
  groups?: string[];
  attributes?: Record<string, string>;
}

TestingModuleOptions

interface TestingModuleOptions {
  enabled?: boolean;
  user?: CognitoTestUser;
  jwtVerifier?: CognitoJwtVerifierOptions;
  identityProvider?: CognitoIdentityProviderOptions;
}

Mock Endpoints

The testing module provides these endpoints when enabled:

POST /cognito-testing-login

Generate mock JWT tokens.

Request:

{
  username: string;
  password: string;
  clientId: string;
}

Response:

{
  AccessToken: string;
  IdToken: string;
  RefreshToken: string;
}

POST /config

Update mock configuration.

Request:

{
  enabled?: boolean;
  user?: CognitoTestUser;
}

GET /config

Get current mock configuration.

Response:

{
  enabled: boolean;
  user: CognitoTestUser;
}

Exports

Functions:

  • createCognitoTestingModule(options?: TestingModuleOptions): DynamicModule

Types:

  • CognitoTestUser
  • TestingModuleOptions

Guides

  • Testing - Complete testing guide with examples