msw-snapshot
Version:
Transparently create an API cache for testing.
99 lines (98 loc) • 2.82 kB
TypeScript
import { DefaultBodyType, HttpHandler, StrictRequest } from "msw";
export * from './mask.js';
export type PlainObject = string | number | null | boolean | PlainObject[] | {
[k: string]: PlainObject;
};
export type Info = {
request: StrictRequest<DefaultBodyType>;
cookies: Record<string, string | string[]>;
context: typeof context;
};
export type SnapshotConfig = {
/**
* Specify msw's test pattern.
*/
test?: RegExp;
/**
* Specify snapshot directory path.
*/
basePath: string;
/**
* Specify whether to update snapshots.
* - true (all)
* - update snapshot
* - missing
* - update snapshot if snapshot is missing
* - false (none)
* - don't update snapshot
* @default false
*/
updateSnapshots?: boolean | 'none' | 'all' | 'missing';
/**
* Specify whether to ignore snapshots.
* - true
* - don't use existing snapshot
* - false
* - use existing snapshot if it exists
* @default false
*/
ignoreSnapshots?: boolean;
/**
* Create snapshot filename from request.
*/
createSnapshotPath?: (info: Info) => Promise<string>;
/**
* Callback when response is received from server.
*/
onFetchFromServer?: (info: Info, snapshot: Snapshot) => void;
/**
* Callback when response is received from snapshot.
*/
onFetchFromSnapshot?: (info: Info, snapshot: Snapshot) => void;
/**
* Callback when snapshot is updated.
*/
onSnapshotUpdated?: (info: Info, snapshot: Snapshot) => void;
};
export type Snapshot = {
request: {
method: string;
url: string;
body: PlainObject;
headers: [string, string][];
cookies: [string, string][];
};
response: {
status: number;
statusText: string;
headers: [string, string][];
body: string;
};
};
/**
* Default namespace.
*/
export declare const DEFAULT_NAMESPACE = "default";
/**
* Context of snapshotting.
* You can use it for separating snapshot files with modifying context.namespace.
*/
export declare const context: {
namespace: string;
};
/**
* Create snapshot RequestHandler.
*/
export declare const snapshot: (config: SnapshotConfig) => HttpHandler;
/**
* Get sorted array of [key, val] tuple from ***#entries.
*/
export declare function getEntries(iter: Record<string, string | string[]> | FormData | URLSearchParams | Headers): [string, string][];
/**
* Get sorted array of [key, val] tuple from ***#entries.
*/
export declare function getSortedEntries(iter: Record<string, string | string[]> | FormData | URLSearchParams | Headers): [string, string][];
/**
* Create hash string from object.
*/
export declare function toHashString(object: PlainObject): string;