Closed Beta — Demo trading only. No real funds. Help us find bugs!
    BETA
    FeaturesPricingAbout
    Sign InJoin Beta

    Developer Docs

    Getting StartedBot EnginesExchange IntegrationsArchitectureSecurityDatabaseDeploymentTesting

    User Guide

    Getting StartedTrading BotsAI BotDCA BotSignal BotArbitrage BotPump BotScalping BotGrid BotCombo BotTradingDeFiPortfolioMarketplaceSubscriptionsSettingsAdmin

    Testing Guide

    This document covers the full testing strategy for BlockbotX, including unit tests, API integration tests, E2E browser tests, and load tests.


    Overview

    BlockbotX uses three test layers to ensure correctness at every level:

    LayerFrameworkScope
    Unit + API integrationJest (ts-jest)Business logic, utilities, API endpoints
    End-to-endPlaywrightFull browser-based user flows
    Loadk6Stress, soak, and spike testing

    The test suite includes:

    • 380+ Jest tests across unit and API integration test files
    • 10 Playwright E2E spec files covering critical user journeys
    • 19 k6 load test scripts with 6 helper modules

    All test types require PostgreSQL 16 and Redis 7 to be running.


    Running Tests

    pnpm test           # All Jest tests (unit + API)
    pnpm test:unit      # Unit tests only (__tests__/ directory)
    pnpm test:api       # API integration tests (requires running server on localhost:3000)
    pnpm test:e2e       # Playwright E2E tests (requires running server on localhost:3000)
    

    Prerequisites

    For unit tests, you need PostgreSQL and Redis running. The development Docker Compose file provides both:

    docker compose up -d postgres redis
    

    For API and E2E tests, you also need the Next.js application running:

    pnpm build && pnpm start
    

    Playwright will auto-start a dev server locally if one is not already running (configured via webServer in playwright.config.ts).


    Unit Tests (tests/)

    Unit tests live in the __tests__/ directory and mirror the lib/ source structure. There are 43 unit test files covering the following domains:

    Test Domains

    DomainFilesWhat is tested
    auth/password.test.ts, jwt.test.tsPassword hashing, JWT generation and verification
    bot/engine/dca-bot.test.ts, position-manager.test.ts, scheduler.test.ts, execution-context.test.ts, bot-executor.test.tsBot execution lifecycle, position management, scheduling
    bot/backtest/backtest-executor.test.ts, backtest-reporter.test.ts, backtester.test.tsBacktesting engine and report generation
    bot/performance/metrics-calculator.test.ts, performance-tracker.test.ts, trade-analyzer.test.ts, portfolio-correlation.test.tsPerformance metrics, trade analysis, portfolio correlation
    bot/risk/stop-loss-manager.test.ts, position-sizer.test.ts, risk-validator.test.ts, take-profit-manager.test.ts, drawdown-tracker.test.tsRisk management, position sizing, stop-loss/take-profit
    bot/strategies/base-strategy.test.ts, strategy-factory.test.ts, breakout.test.tsTrading strategy implementations
    bot/indicators/indicators.test.ts, macd.test.ts, cache.test.tsTechnical indicators and indicator caching
    defi/il-calculator.test.ts, farming-calculations.test.ts, staking-calculations.test.tsDeFi calculations (impermanent loss, farming, staking)
    models/decimal-precision.test.tsDecimal(65,30) precision for all financial fields
    notifications/notification-service.test.tsNotification delivery
    portfolio/calculator.test.tsPortfolio value calculations
    referrals/reward-calculator.test.tsReferral reward calculations
    security/csrf.test.ts, sanitize.test.ts, security-logger.test.ts, env-validation.test.ts, auth-middleware.test.ts, cors.test.ts, encryption.test.tsCSRF protection, input sanitization, encryption, CORS
    trading/validator.test.ts, operations.test.ts, balance.test.tsTrade validation, operations, balance calculations
    webhooks/stripe.test.tsStripe webhook signature verification and event handling

    Decimal Precision Tests

    The decimal-precision.test.ts file is critical for financial accuracy. It verifies that all financial fields (prices, quantities, P&L, percentages, APY, TVL) use Decimal(65,30) precision and that calculations do not suffer from floating-point rounding errors. This test is run as a separate explicit step in CI in addition to the standard unit test suite.

    Configuration

    Unit tests use the ts-jest preset with the following key settings from jest.config.js:

    • Test roots: __tests__/ and tests/
    • Module aliases: @/ maps to the project root (matches tsconfig.json)
    • Test timeout: 10,000ms (10 seconds)
    • Coverage collection: Focuses on lib/ modules with business logic
    • Coverage thresholds: branches 55%, functions 65%, lines 68%, statements 68%
    • Mocks: clearMocks: true, resetMocks: false, restoreMocks: false (preserves manual mocks)

    Mocking Strategy

    • Logger and Redis: Mocked globally in tests/jest-setup.js (runs before test files load)
    • Console: console.error and console.warn are mocked in tests/setup.ts to reduce test noise
    • Fetch: Global fetch is wrapped to automatically include the x-test-mode: true header for CSRF bypass
    • External services: MSW (Mock Service Worker) is used for mocking HTTP requests to external APIs

    API Integration Tests (tests/api/)

    API integration tests run against a live Next.js server on localhost:3000 with real PostgreSQL and Redis backends. There are 23 test files covering every API domain.

    Test Files

    FileDomain
    auth.api.test.tsRegistration, login, logout, password reset
    auth-2fa-refresh.api.test.tsTwo-factor authentication and token refresh
    authorization.api.test.tsRole-based access control and permissions
    bots.api.test.tsBot CRUD operations
    bots-extended.api.test.tsBot start/stop, backtest, performance
    dashboard.api.test.tsDashboard data aggregation
    defi.api.test.tsDeFi staking, farming, liquidity endpoints
    exchange.api.test.tsExchange connections (Binance, OKX)
    health.api.test.tsHealth check endpoint
    help.api.test.tsHelp and FAQ endpoints
    market.api.test.tsMarket data and ticker information
    marketplace.api.test.tsBot template marketplace
    notifications.api.test.tsNotification CRUD and preferences
    portfolio.api.test.tsPortfolio balances and history
    referrals.api.test.tsReferral codes and rewards
    settings.api.test.tsUser settings and preferences
    signal-providers.api.test.tsSignal provider management
    subscriptions.api.test.tsSubscription plans and Stripe integration
    support.api.test.tsSupport ticket management
    templates.api.test.tsBot template CRUD
    trading.api.test.tsOrder placement, trade history
    user.api.test.tsUser profile management
    webhooks.api.test.tsStripe webhook processing

    Authentication in Tests

    API tests use the authenticatedFetch() helper from tests/utils/test-helpers.ts:

    const response = await authenticatedFetch('http://localhost:3000/api/bots', {
      method: 'POST',
      token: userToken,
      body: JSON.stringify({ name: 'My Bot', type: 'DCA', ... }),
    });
    

    This helper:

    1. Sets Content-Type: application/json (unless the body is FormData)
    2. Sets x-test-mode: true for CSRF bypass in test mode
    3. Adds the Authorization: Bearer <token> header if a token is provided
    4. Fetches and includes the CSRF token for mutation requests (POST, PUT, DELETE, PATCH)

    Test Helpers

    The tests/utils/test-helpers.ts module provides factory functions for test data:

    HelperPurpose
    createTestUser(overrides?)Creates a user in the database with hashed password
    generateTestToken(userId, email?)Generates a valid JWT access token
    cleanupTestUser(userId)Deletes a user and ignores errors
    cleanupTestData()Deletes all test data in foreign-key-safe order
    createTestExchangeConnection(userId)Creates a Binance exchange connection
    createTestExchangeKey(userId, exchange?)Creates an exchange connection with permissions
    createTestBot(userId, token, overrides?)Creates a bot via the API
    createTestTicket(userId, token)Creates a support ticket via the API
    createTestTemplate(userId, token)Creates a bot template via the API
    createTestExchange(name?)Creates an Exchange record in the database
    createTestSignalProvider(id?)Creates a SignalProvider record
    createTestSubscription(userId, overrides?)Creates a Subscription record
    createTestMarketplaceListing(userId, overrides?)Creates a marketplace listing with template
    generateStripeWebhookSignature(payload, secret)Generates valid Stripe webhook HMAC-SHA256 signatures
    waitFor(condition, timeout?, interval?)Polls a condition until true or timeout
    getCsrfToken()Fetches and caches a CSRF token from /api/csrf
    testDataObject with generator functions for emails, passwords, API keys

    Setup and Teardown

    • tests/jest-setup.js: Runs before test files load. Sets up environment variables, mocks for logger and Redis.
    • tests/setup.ts: Runs after test framework initialization. Mocks console, wraps global fetch, and closes the database pool in afterAll.
    • tests/globalTeardown.js: Runs once after all test suites complete. Safety net to close any remaining pg pool connections.

    E2E Tests (e2e/)

    End-to-end tests use Playwright to drive a real browser against the running application. There are 10 spec files covering the major user flows.

    Spec Files

    FileFlow
    auth-flow.spec.tsSignup, login, logout, 2FA enrollment
    bot-flow.spec.tsBot creation, configuration, start/stop, deletion
    trading-flow.spec.tsExchange connection, order placement
    defi-flow.spec.tsDeFi staking, farming, liquidity provision
    subscription-flow.spec.tsPlan selection, payment, upgrade/downgrade
    marketplace-flow.spec.tsBrowse, purchase, and deploy bot templates
    analytics-flow.spec.tsDashboard analytics, performance charts
    notifications-flow.spec.tsNotification preferences, mark as read
    support-flow.spec.tsCreate and manage support tickets
    exchange-connection-flow.spec.tsConnect Binance/OKX exchanges

    Playwright Configuration

    Key settings from playwright.config.ts:

    • Test directory: ./e2e
    • Test timeout: 120 seconds per test (accounts for login, bot creation, and assertion time)
    • Parallel execution: Disabled (fullyParallel: false) to avoid state conflicts
    • Retries: 1 in CI, 0 locally
    • Workers: 2 in CI (tests are user-isolated), 1 locally
    • Base URL: http://localhost:3000 (overridable via PLAYWRIGHT_BASE_URL)

    Browser Configuration

    • CI: Chromium only (for speed)
    • Local: Chromium, Firefox, and WebKit (Safari)
      • WebKit uses extended navigation timeout (90 seconds) due to slower rendering

    Timeouts

    SettingValue
    Test timeout120 seconds
    Action timeout30 seconds
    Navigation timeout60 seconds (90 seconds for WebKit)

    Artifacts

    • Traces: Collected on first retry
    • Screenshots: Captured only on failure
    • Video: Retained only on failure
    • Reports: HTML report (playwright-report/) and JSON results (test-results/results.json)
    • CI retention: 30 days for both reports and test results

    Local Development

    When running locally, Playwright auto-starts a dev server (pnpm dev) if one is not already running. In CI, the server is started manually before tests run, and the webServer configuration is disabled.


    Load Testing (tests/load/)

    Load tests use k6 for stress, soak, and spike testing of the application under various traffic patterns.

    Test Scripts

    There are 19 load test scripts:

    ScriptTarget
    api-auth.jsAuthentication endpoints
    api-bots.jsBot management endpoints
    api-trading.jsTrading endpoints
    api-market.jsMarket data endpoints
    api-portfolio.jsPortfolio endpoints
    api-defi.jsDeFi endpoints
    api-notifications.jsNotification endpoints
    api-subscriptions.jsSubscription endpoints
    api-dashboard.jsDashboard endpoints
    api-admin.jsAdmin endpoints
    api-health.jsHealth check endpoint
    api-user-settings.jsUser settings endpoints
    websocket-stress.jsWebSocket/Socket.io connections
    db-stress.jsDatabase query performance
    redis-stress.jsRedis operations performance
    rate-limiter.jsRate limiter behavior under load
    user-journey.jsFull user journey simulation
    spike-test.jsSudden traffic spike handling
    soak-test.jsExtended duration stability

    Helper Modules

    The tests/load/helpers/ directory contains 6 shared modules:

    ModulePurpose
    config.jsBase URL, thresholds, and stage configurations
    auth.jsAuthentication helpers for load test users
    checks.jsResponse validation and assertion helpers
    metrics.jsCustom metric definitions and aggregation
    data.jsTest data generation
    seed-users.jsPre-seeds test users for load tests

    Running Load Tests

    # Run a specific load test
    k6 run tests/load/api-auth.js
    
    # Run with custom virtual users and duration
    k6 run --vus 50 --duration 5m tests/load/api-health.js
    

    Test Infrastructure

    Jest Configuration Highlights

    Key settings from jest.config.js:

    module.exports = {
      preset: 'ts-jest',
      testEnvironment: 'node',
      roots: ['<rootDir>/__tests__', '<rootDir>/tests'],
      moduleNameMapper: { '^@/(.*)$': '<rootDir>/$1' },
      testTimeout: 10000,
      clearMocks: true,
      resetMocks: false,
      restoreMocks: false,
      setupFiles: ['<rootDir>/tests/jest-setup.js'],
      setupFilesAfterEnv: ['<rootDir>/tests/setup.ts'],
      globalTeardown: '<rootDir>/tests/globalTeardown.js',
    };
    
    • E2E exclusion: The testPathIgnorePatterns setting excludes e2e/ from Jest (Playwright handles those)
    • ESM support: transformIgnorePatterns allows transforming specific ESM-only node_modules packages (jsdom, next, noble, etc.)

    Test Identification

    All test requests include the x-test-mode: true header. This allows the application to:

    • Bypass CSRF validation in test mode
    • Identify test traffic in logs
    • Apply test-specific behavior where needed

    Required Services

    All test types (unit, API, E2E) require:

    • PostgreSQL 16: For database operations. Schema is set up via pnpm db:push in CI
    • Redis 7: For caching, rate limiting, and pub/sub

    In CI, both are provided as GitHub Actions service containers. Locally, use Docker Compose:

    docker compose up -d postgres redis
    

    Known Issues

    pg Pool Timers

    The pg connection pool (exported from lib/db.ts) creates background health-check timers. While pool.end() is called in afterAll (in tests/setup.ts) and in globalTeardown.js, these internal timers may not fully clear within Jest's default wait period.

    The current configuration relies on .unref() timers and pool.end() in setup/teardown to let workers exit cleanly once all pool clients are idle. The pool does not keep the worker alive when idle.

    If you observe "Jest did not exit" warnings, all test resources are properly cleaned up -- the warning is cosmetic and does not indicate a leak.


    CI Integration

    Parallel Execution

    Tests run in parallel across 7 CI jobs in the test.yml workflow:

    1. Unit Tests -- Jest unit tests + Decimal precision + Codecov upload
    2. API Tests -- Build, start server, run API integration tests
    3. E2E Tests -- Install Playwright Chromium, build, start server, run E2E specs
    4. Code Quality -- tsc --noEmit + pnpm lint
    5. Security Audit -- pnpm audit + optional Snyk
    6. Build Check -- pnpm build + verify .next/ output
    7. Docker Build -- Build image + verify container starts

    All 7 jobs must pass for the all-tests-passed gate, which is required before merging.

    Service Containers

    CI uses GitHub Actions service containers:

    services:
      postgres:
        image: postgres:16-alpine
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
      redis:
        image: redis:7-alpine
        ports: ["6379:6379"]
        options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
    

    Coverage

    • Unit test coverage is collected via pnpm test:unit --coverage
    • Coverage reports are uploaded to Codecov with the unittests flag
    • On pull requests, coverage is commented directly on the PR via lcov-reporter-action
    • Coverage thresholds are enforced in jest.config.js (branches 55%, functions 65%, lines 68%, statements 68%)

    Artifacts

    • Playwright report: HTML report uploaded as playwright-report artifact (30-day retention)
    • Test results: JSON results uploaded as test-results artifact (30-day retention)
    • Coverage: Uploaded to Codecov (external service)

    AI-powered crypto trading platform in closed beta. 8 bot engines, DeFi integration, and demo trading.

    Product

    • Features
    • Pricing
    • About
    • Join Beta

    Resources

    • Documentation
    • API Reference
    • Support
    • Blog
    • Status

    Legal

    • Terms of Service
    • Privacy Policy
    • Disclaimer

    © 2026 BlockbotX. All rights reserved.

    Built with AI · Powered by Next.js