or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmatching.mdrecording.mdsanitization.mdtest-integration.mdtest-modes.mdtransforms.md
tile.json

test-modes.mddocs/

Test Mode Utilities

Runtime detection and environment management utilities for different test execution modes, with support for environment variable handling and mode-specific behavior in Azure SDK testing scenarios.

Capabilities

Test Mode Detection

Functions for detecting the current test execution mode at runtime, enabling conditional behavior based on whether tests are recording, playing back, or running live.

/**
 * Checks if the test is running in live mode (making actual network calls)
 * @returns true if in live mode, false otherwise
 */
function isLiveMode(): boolean;

/**
 * Checks if the test is running in playback mode (using recorded responses)
 * @returns true if in playback mode, false otherwise
 */
function isPlaybackMode(): boolean;

/**
 * Checks if the test is running in record mode (capturing network calls)
 * @returns true if in record mode, false otherwise
 */
function isRecordMode(): boolean;

Usage Examples:

import { isLiveMode, isPlaybackMode, isRecordMode } from "@azure-tools/test-recorder";

// Conditional test behavior based on mode
if (isLiveMode()) {
  console.log("Running tests against live services");
  // Set up real Azure resources
} else if (isRecordMode()) {
  console.log("Recording test interactions");
  // Set up resources and record interactions
} else if (isPlaybackMode()) {
  console.log("Playing back recorded interactions");
  // Use recorded responses, skip resource setup
}

// Skip expensive operations in playback mode
if (!isPlaybackMode()) {
  await createLargeTestResource();
}

// Only validate live service responses in live mode
if (isLiveMode()) {
  await validateServiceHealth();
}

Environment Variable Management

Utilities for managing environment variables required for testing, with support for asserting required variables and setting up test environments.

/**
 * Gets a required environment variable, throwing an error if not found
 * @param variable - Name of the environment variable
 * @returns The environment variable value
 * @throws Error if the environment variable is not set
 */
function assertEnvironmentVariable(variable: string): string;

/**
 * Sets multiple environment variables at once
 * @param variables - Object mapping variable names to values
 */
function setEnvironmentVariables(variables: { [key: string]: string }): void;

Usage Examples:

import { assertEnvironmentVariable, setEnvironmentVariables } from "@azure-tools/test-recorder";

// Ensure required environment variables are present
const connectionString = assertEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
const clientId = assertEnvironmentVariable("AZURE_CLIENT_ID");
const tenantId = assertEnvironmentVariable("AZURE_TENANT_ID");

// Set up environment variables for testing
setEnvironmentVariables({
  "TEST_MODE": "playback",
  "AZURE_LOG_LEVEL": "warning",
  "AZURE_CLIENT_ID": "fake-client-id-for-testing"
});

// Use in Azure SDK client initialization
const blobServiceClient = new BlobServiceClient(connectionString);

Environment Access

Direct access to environment variables through a convenient interface.

/**
 * Environment variables accessor providing access to process.env
 */
const env: Record<string, string | undefined>;

Usage Examples:

import { env } from "@azure-tools/test-recorder";

// Access environment variables
const testMode = env.TEST_MODE || "playback";
const proxyPort = env.TEST_PROXY_HTTP_PORT || "5000";
const storageAccount = env.AZURE_STORAGE_ACCOUNT;

// Check for optional configuration
if (env.AZURE_LOG_LEVEL) {
  setLogLevel(env.AZURE_LOG_LEVEL);
}

// Use in conditional logic
if (env.CI === "true") {
  // Special behavior in CI environments
  console.log("Running in CI environment");
}

Testing Utilities

Utility constants and functions optimized for testing scenarios.

/**
 * Polling options optimized for testing scenarios
 */
const testPollingOptions: {
  updateIntervalInMs: number | undefined;
};

/**
 * Delays execution for the specified number of milliseconds
 * @param milliseconds - Number of milliseconds to delay
 * @returns Promise that resolves after the delay, or void in some environments
 */
function delay(milliseconds: number): Promise<void> | void;

Usage Examples:

import { testPollingOptions, delay } from "@azure-tools/test-recorder";

// Use optimized polling for tests
const poller = await client.beginOperation(request, {
  ...testPollingOptions,
  onProgress: (state) => console.log(`Operation ${state.status}`)
});

// Add delays in test scenarios
await delay(1000); // Wait 1 second

// Use in polling loops
while (!operationComplete) {
  await delay(100); // Short delay between checks
  operationComplete = await checkOperationStatus();
}

// Configure Azure SDK clients with test-optimized polling
const client = new ServiceClient(endpoint, credential, {
  ...testPollingOptions
});

Mode-Specific Behavior Patterns

Common patterns for implementing mode-specific behavior in tests using the mode detection functions.

Usage Examples:

import { isLiveMode, isRecordMode, isPlaybackMode } from "@azure-tools/test-recorder";

// Resource management based on test mode
async function setupTestResources() {
  if (isLiveMode() || isRecordMode()) {
    // Create actual Azure resources
    const resourceGroup = await createResourceGroup();
    const storageAccount = await createStorageAccount(resourceGroup);
    return { resourceGroup, storageAccount };
  } else {
    // Playback mode - return fake resource identifiers
    return {
      resourceGroup: "fake-rg",
      storageAccount: "fakestorage"
    };
  }
}

// Cleanup based on test mode
async function cleanupTestResources(resources: any) {
  if (isLiveMode()) {
    // Clean up actual resources
    await deleteStorageAccount(resources.storageAccount);
    await deleteResourceGroup(resources.resourceGroup);
  }
  // No cleanup needed in record/playback modes
}

// Validation based on test mode
async function validateResponse(response: any) {
  if (isLiveMode()) {
    // Validate against live service constraints
    expect(response.timestamp).toBeInstanceOf(Date);
    expect(response.etag).toMatch(/^"[a-f0-9]+"$/);
  } else {
    // In record/playback, just validate structure
    expect(response).toHaveProperty('timestamp');
    expect(response).toHaveProperty('etag');
  }
}

// Timing behavior based on test mode
const waitTime = isPlaybackMode() ? 0 : 5000; // No wait in playback
await delay(waitTime);