cooky-cutter
Version:
Object factories for testing in TypeScript
26 lines (25 loc) • 1.22 kB
TypeScript
import { DerivedFunction } from "./derive";
import { ArrayFactory } from "./array";
import { ArrayElement } from "./utils";
declare type Config<T> = {
[Key in keyof T]: T[Key] | AttributeFunction<T[Key]> | Factory<T[Key]> | DerivedFunction<T, T[Key]> | ArrayFactory<ArrayElement<T[Key]>>;
};
declare type AttributeFunction<T> = (invocation: number) => T;
declare type FactoryConfig<T> = Partial<Config<T>>;
declare const FACTORY_FUNCTION_KEY = "factory";
interface Factory<T> {
(override?: FactoryConfig<T>): T;
__cooky_cutter: typeof FACTORY_FUNCTION_KEY;
resetSequence(): void;
}
/**
* Define a new factory function. The return value is a function that can be
* invoked as many times as needed to create a given type of object. Use the
* config param to define how the object is generated on each invocation.
*
* @param config An object that defines how the factory should generate objects.
* Each key can either be a static value, a function that receives the
* invocation count as the only parameter or another factory.
*/
declare function define<Result>(config: Config<Result>): Factory<Result>;
export { define, AttributeFunction, Config, Factory, FactoryConfig, FACTORY_FUNCTION_KEY };