Skip to content

You are currently viewing documentation for0.1.0-beta.1

How to simulate unavailable API dependencies in tests

Choose the boundary your test needs

To simulate an unavailable API dependency, replace the network boundary with a controlled failure and assert the application’s expected behavior. A function stub is enough to test an error branch. A local HTTP server exercises status codes and decoding. A transport interceptor tests a real client without contacting the provider. Choose the smallest boundary that reproduces the failure you need to understand.

FailureSimulationAssert
Connection failureReject the injected transport promise.Application returns its defined unavailable result.
HTTP 503Return an actual 503 response.Client checks status before treating the body as success.
Malformed responseReturn truncated JSON with a successful status.Parsing failure remains distinct from a valid response.
Deadline or cancellationUse an abort-aware delayed transport.The deadline aborts work and releases resources.

Run a dependency-free Node example

Runnable with Node.js 22+. Save this JavaScript as dependency.test.mjs and run node --test dependency.test.mjs. No packages, credentials or listening server are required. The injected callable stands in for a transport. Both tests should pass: one for an HTTP 503 and one for a connection failure. Node’s built-in test runner supplies the test harness; this example uses dependency injection rather than patching a global fetch.

ts
import test from 'node:test';
import assert from 'node:assert/strict';

async function loadCustomer(send) {
  let response;
  try {
    response = await send();
  } catch {
    return { kind: 'unavailable' };
  }
  if (!response.ok) {
    return { kind: 'http_error', status: response.status };
  }
  return { kind: 'ok', customer: await response.json() };
}

test('reports HTTP 503 separately', async () => {
  const result = await loadCustomer(async () => new Response('', { status: 503 }));
  assert.deepEqual(result, { kind: 'http_error', status: 503 });
});

test('reports connection failure', async () => {
  const result = await loadCustomer(async () => { throw new TypeError('offline'); });
  assert.deepEqual(result, { kind: 'unavailable' });
});

Extend the failure matrix deliberately

Add a successful control and cases for the failures your client actually handles. Test JSON parsing errors separately: the example intentionally leaves those errors visible to the caller. Do not model a timeout merely by throwing an error with timeout in its name; exercise the AbortSignal or cancellation mechanism used by the real client. Keep attempts bounded and verify the number of dispatches. For mutating requests, a lost response can follow a completed provider action, so a transport failure is insufficient evidence that retrying is safe.

Keep fixtures and provider evidence separate

A passing simulation proves the assertions for the chosen fixture and boundary. It does not show how the live provider behaves, whether its contract has changed, or whether a write persisted. Revisit fixtures after a reviewed provider change and retain their version and provenance. Use a separately authorized provider test environment for behavior that a local substitute cannot establish.

How this differs from AGINDOW simulation

AGINDOW’s local SDK simulation evaluates supplied recorded values through its configured local path. It does not make an unavailable provider available or provide a general network fault server. Offline source inventory is advisory. The browser playground collects real GET responses only when started by the user, and reports comparisons without applying repairs. Use application transport tests for outage handling and configured representative evidence for drift detection.

About this guide

Written and reviewed 27 November 2026 by the AGINDOW documentation team. Product behavior follows the current beta documentation edition. Evaluate integration behavior locally before relying on it in production.

To issue a scoped API key, open the dashboard, select your organization and environment, then go to Settings → API Keys.