🃏 MockChain Class
🃏 MockChain API Reference
MockChain is a mockable subclass of Promise designed for testing asynchronous operations. It preserves the full promise interface while providing methods to manually control resolution, rejection, and chaining.
Constructor
new MockChain(executor?: (resolve: Function, reject: Function) => void, autoStart?: boolean, parent?: MockChain | null)
executor– Optional executor function, like in native Promise.autoStart– Iffalse, execution is deferred until.mock()or.fail()is manually called.parent– Internal link to the previous node in the chain.
Creates a new MockChain instance. Use this when you need deterministic control over asynchronous behavior.
Properties
.root
- Type:
MockChain - Description: The first (root) node of the chain. Useful for triggering resolution or rejection at the beginning of a chain.
.parent
- Type:
MockChain | undefined | null - Description: Reference to the previous node in the chain.
.autoStart
- Type:
boolean - Description: Indicates whether this chain node executes automatically upon creation.
External Control Methods
.mock(value: any): void
Resolve the chain at this node with a value.
.fail(error: any): void
Reject the chain at this node with an error.
.mockAfter(ms: number, value: any): void
Resolve the node after a delay in milliseconds.
.failAfter(ms: number, error: any): void
Reject the node after a delay in milliseconds.
Note:
.mock()and.fail()can be called on any node, not just.root.
Promise Overrides
MockChain overrides standard Promise methods to preserve chain mockability.
.then(onFulfilled?, onRejected?)
Returns a new MockChain node.
.catch(onRejected?)
Returns a new MockChain node.
.finally(onFinally?)
Returns a new MockChain node.
All chain nodes maintain
.parentlinks and can be mocked individually.
Static Methods
MockChain.resolve(value)
Returns a MockChain resolved with value.
MockChain.reject(reason)
Returns a MockChain rejected with reason.
MockChain.from(promise | value, autoStart?: boolean, parent?: MockChain | null)
Wraps any promise or value in a MockChain. Supports deferred execution via the autoStart flag.
MockChain.fromResponse(url: string | undefined, opts?: RequestInit, autoStart?: boolean)
Creates a MockChain from a fetch request. If url is undefined, returns a placeholder chain. Execution can be deferred.
MockChain.fromMessage(ws: WebSocket | undefined, isMessage?: Function, attach?: boolean, autoStart?: boolean)
Creates a MockChain from WebSocket messages, with an optional message filter predicate. If ws is undefined, returns a non-auto-start placeholder chain.
MockChain.fromEvent(emitter: EventTarget | EventEmitter | undefined, eventName: string, isEvent?: Function, attach?: boolean, autoStart?: boolean)
Wraps a Node.js or browser event in a mockable chain, allowing for custom filtering via isEvent. If emitter is undefined, returns a non-auto-start placeholder chain.
All static constructors now support an
autoStartflag for consistent asynchronous control.
Example Usage
import { MockChain } from '@fizzwiz/mockchain';
// Wrap a fetch request in a mockable chain
const chain = MockChain.from(fetch('https://api/data'))
.then(res => res.json())
.then(console.log);
// Mock the response at the root of the chain
chain.root.mock(
new Response(JSON.stringify({ ok: true }))
);
// -> { ok: true }
Comments
Post a Comment