Skip to content

Testing

← Back to Examples Overview

Testing utilities for mock server setup and Vitest integration.

ExampleDescription
Configuration Constants

This example demonstrates the configuration constants available in the used to configure mock servers for testing Algorand client integrations.

No mock server is required - this example just displays the constants.

Test Data Constants

This example demonstrates the test data constants available in the the pre-recorded HAR file responses used by the mock server.

No mock server is required - this example just displays the constants.

Health Check Utility

This example demonstrates how to use checkServerHealth() to verify mock server availability before running tests. The function checks if a server is reachable by performing a health check request.

This example works whether or not mock servers are running - it just reports the status of each server.

Mock Server Connection

This example demonstrates how to use getMockServer() to connect to a mock server and understand the MockServer interface. It shows:

  • The MockServer interface properties (baseUrl and clientType)
  • How getMockServer reads from environment variables
  • Proper error handling for missing env vars and unreachable servers

This example requires MOCK_ALGOD_URL environment variable and a running mock server for a successful connection - otherwise it shows helpful error messages.

Vitest Global Setup Factory

This example demonstrates how to use createGlobalSetup() to create custom Vitest global setup functions for mock server lifecycle management. It shows:

  • The function signature: createGlobalSetup(clientType: ClientType)
  • Creating custom global setups for algod, indexer, and kmd
  • The returned async setup function and its teardown mechanism
  • Example vitest.config.ts usage with the globalSetup option
  • Environment variables for configuration and debugging

NOTE: This example documents the factory pattern without invoking the setup (no running server needed). The setup functions are designed for use in vitest.config.ts, not direct invocation.

Pre-built Global Setups

This example demonstrates the pre-built global setup functions available for common client types and how to use them in Vitest configuration. It shows:

  • algodGlobalSetup, indexerGlobalSetup, and kmdGlobalSetup imports
  • How these are pre-configured setup functions ready to use
  • Example vitest.config.ts configurations for each setup
  • Example tests/globalSetup.ts files that re-export the setup
  • The difference between pre-built setups and createGlobalSetup()

NOTE: This example documents usage patterns without invoking setups (no running server needed). The setup functions are designed for use in vitest.config.ts, not direct invocation.

Integration Example - Complete Test Setup

This example demonstrates how all testing package components work together for a real test scenario. It shows:

  • Importing all major exports from the testing package
  • Checking mock server availability with checkServerHealth()
  • Connecting to mock server using getMockServer()
  • Constructing client configuration using MockServer.baseUrl and DEFAULT_TOKEN
  • Displaying which test data constants can be used with the connected mock server
  • Helpful setup instructions when the server is unavailable

This example works with or without a running mock server, providing helpful guidance in either case.

Run any example from the repository’s examples directory:

Terminal window
cd examples
npm run example testing/01-*.ts