factory.ts
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -1,5 +0,5 @@ | ||
export declare type RecPartial<T> = { | ||
[P in keyof T]?: RecPartial<T[P]>; | ||
}; | ||
export declare type FactoryFunc<T> = (item: RecPartial<T>) => T | Promise<T>; | ||
import { RecPartial } from "./shared"; | ||
export declare type FactoryFunc<T, U = T> = (item?: RecPartial<T>) => Promise<U>; | ||
export declare type ListFactoryFunc<T, U = T> = (count: number, item?: RecPartial<T>) => Promise<U[]>; | ||
export declare function lift<T>(t: T | Promise<T>): Promise<T>; | ||
export declare class Generator<T> { | ||
@@ -16,4 +16,4 @@ readonly func: (seq: number) => T | Promise<T>; | ||
export interface IFactory<T, U> { | ||
build(item?: RecPartial<T>): Promise<U>; | ||
buildList(count: number, item?: RecPartial<T>): Promise<U[]>; | ||
build: FactoryFunc<T, U>; | ||
buildList: ListFactoryFunc<T, U>; | ||
} | ||
@@ -29,3 +29,3 @@ export declare class Factory<T> implements IFactory<T, T> { | ||
combine<U>(other: Factory<U>): Factory<T & U>; | ||
transform<U>(fn: (t: T) => U | Promise<U>): IFactory<T, U>; | ||
transform<U>(fn: (t: T) => U | Promise<U>): TransformFactory<T, U>; | ||
withDerivation<KOut extends keyof T>(kOut: KOut, f: (v1: T, seq: number) => T[KOut] | Promise<T[KOut]>): Factory<T>; | ||
@@ -32,0 +32,0 @@ withDerivation1<K1 extends keyof T, KOut extends keyof T>(kInput: [K1], kOut: KOut, f: (v1: T[K1], seq: number) => T[KOut] | Promise<T[KOut]>): Factory<T>; |
@@ -22,2 +22,3 @@ "use strict"; | ||
} | ||
exports.lift = lift; | ||
class Generator { | ||
@@ -24,0 +25,0 @@ constructor(func) { |
@@ -5,2 +5,5 @@ import * as Async from "./async"; | ||
export { Sync }; | ||
export { RecPartial, FactoryFunc, Generator, Derived, Factory, Builder, val, each, makeFactory } from "./sync"; | ||
import * as Pipeline from "./pipeline"; | ||
export { Pipeline }; | ||
export { RecPartial } from "./shared"; | ||
export { FactoryFunc, Generator, Derived, Factory, Builder, val, each, makeFactory } from "./sync"; |
@@ -7,2 +7,4 @@ "use strict"; | ||
exports.Sync = Sync; | ||
const Pipeline = require("./pipeline"); | ||
exports.Pipeline = Pipeline; | ||
// for now, for backwards compat | ||
@@ -9,0 +11,0 @@ var sync_1 = require("./sync"); |
@@ -1,5 +0,3 @@ | ||
export declare type RecPartial<T> = { | ||
[P in keyof T]?: RecPartial<T[P]>; | ||
}; | ||
export declare type FactoryFunc<T> = (item: RecPartial<T>) => T; | ||
import { RecPartial } from "./shared"; | ||
export declare type FactoryFunc<T> = (item?: RecPartial<T>) => T; | ||
export declare class Generator<T> { | ||
@@ -6,0 +4,0 @@ readonly func: (seq: number) => T; |
{ | ||
"name": "factory.ts", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A Typescript test data factory similar to factory_bot and rosie", |
@@ -7,4 +7,6 @@ # factory.ts | ||
Version 0.3.2 introduces a new set of async factory methods for cases where asynchronicity is required to generate values. | ||
Version 0.3.2 introduces a new set of async factory methods for cases where asynchronicity is required to generate values. See sync.spec.ts for examples. | ||
Version 0.3.3 introduces a pipeline mechanism for building up a key-value set of data. See pipeline.spec.ts for an example. | ||
## Example | ||
@@ -123,1 +125,16 @@ | ||
Async factories also have a `transform()` method which can take a function that goes from `T => U` or from `T => Promise<U>`. This creates an object with the factory interface for building only, and is meant to be a "last step" transform. The idea is that the output of the last step may be a different type. For example, you may have an Unsaved and a Saved type for database records, so you can pass in your `insert(u: Unsaved): Promise<Saved>` method and get a factory which will asynchronously build a persisted `Saved` object. | ||
### Pipelines | ||
Async factories can be put into pipeline, where at each step in the pipeline you add one or more top-level keys to an object meant to hold your test data. | ||
This feature is designed to help with bootstrapping complex data structures for tests (e.g. several database records). | ||
Each step in the pipeline can accept: | ||
- a raw object with keys and values to merge into the pipeline data, OR | ||
- a function (optionally asynchronous) returning the same, and which can depend on all data in the pipeline up to that point, OR | ||
- an Async Factory (or TransformFactory or FactoryFunc), along with: | ||
- a partial for the factory, OR | ||
- a function (optionally synchronous) returning a partial for the factory | ||
As noted above, each step can depend on the previous steps' data to make its contribution, and each step can be asynchronous. In the end you just await on the pipeline, and through the magic of Typescript you have a type-safe object whose keys are all the values you want to use in your test. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43490
18
515
139