Skip to main content

@idpass/data-collect-core / NetworkSimulator

Class: NetworkSimulator

Defined in: testing/NetworkSimulator.ts:54

Simulates unreliable network conditions for testing.

Wraps fetch or Express middleware to inject latency, failures, and truncated responses. Useful for testing retry logic, timeout handling, and graceful degradation in sync code.

Constructors

Constructor

new NetworkSimulator(condition?): NetworkSimulator

Defined in: testing/NetworkSimulator.ts:60

Parameters

condition?

NetworkCondition

Returns

NetworkSimulator

Methods

setCondition()

setCondition(condition): void

Defined in: testing/NetworkSimulator.ts:69

Replace the current network condition.

Parameters

condition

NetworkCondition

Returns

void


reset()

reset(): void

Defined in: testing/NetworkSimulator.ts:76

Reset all counters and clear conditions.

Returns

void


wrapFetch()

wrapFetch(originalFetch): {(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

Defined in: testing/NetworkSimulator.ts:86

Returns a wrapped version of fetch that applies the configured network conditions.

Parameters

originalFetch

{(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

Returns

(input, init?): Promise<Response>

MDN Reference

Parameters
input

RequestInfo | URL

init?

RequestInit

Returns

Promise<Response>

(input, init?): Promise<Response>

MDN Reference

Parameters
input

string | Request | URL

init?

RequestInit

Returns

Promise<Response>


createMiddleware()

createMiddleware(): (req, res, next) => void

Defined in: testing/NetworkSimulator.ts:145

Creates Express middleware that applies the configured network conditions.

The middleware intercepts requests and can add latency, return error status codes, or pass through to the next handler.

Returns

(req, res, next): void

Parameters
req

unknown

res
status

(code) => object

next

() => void

Returns

void


getStats()

getStats(): NetworkStats

Defined in: testing/NetworkSimulator.ts:191

Returns statistics about the simulator's activity.

Returns

NetworkStats