factory.ts
Advanced tools
Comparing version 0.2.2 to 0.3.0
{ | ||
"name": "factory.ts", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
"description": | ||
"A Typescript test data factory similar to factory_girl and rosie", | ||
"description": "A Typescript test data factory similar to factory_bot and rosie", | ||
"scripts": { | ||
"test": | ||
"NODE_ENV=test mocha --require spec/setup.js --require ts-node/register", | ||
"testAll": | ||
"NODE_ENV=test mocha --require spec/setup.js --require ts-node/register 'spec/**/*.spec.ts*'" | ||
"test": "NODE_ENV=test mocha --require spec/setup.js --require ts-node/register", | ||
"testAll": "NODE_ENV=test mocha --require spec/setup.js --require ts-node/register 'spec/**/*.spec.ts*'" | ||
}, | ||
@@ -34,8 +31,8 @@ "repository": "https://github.com/willryan/factory.ts", | ||
"mocha-junit-reporter": "^1.12.1", | ||
"ts-loader": "^0.9.5", | ||
"ts-node": "^2.1.0", | ||
"typescript": "2.1.6", | ||
"ts-loader": "^4.4.2", | ||
"ts-node": "^7.0.0", | ||
"typescript": "2.9.2", | ||
"webpack": "^1.14.0", | ||
"webpack-dev-server": "^1.16.2" | ||
} | ||
} | ||
} |
# factory.ts | ||
A library to ease creation of factories for test data for Typescript | ||
@@ -6,2 +7,4 @@ | ||
Version 0.3.0 introduces a new set of async factory methods for cases where asynchronicity is required to generate values. | ||
## Example | ||
@@ -13,7 +16,7 @@ | ||
interface Person { | ||
id: number | ||
firstName: string | ||
lastName: string | ||
fullName: string | ||
age: number | ||
id: number; | ||
firstName: string; | ||
lastName: string; | ||
fullName: string; | ||
age: number; | ||
} | ||
@@ -25,9 +28,9 @@ ``` | ||
```typescript | ||
import * as Factory from 'factory.ts' | ||
import * as Factory from "factory.ts"; | ||
const personFactory = Factory.makeFactory<Person>({ | ||
const personFactory = Factory.Sync.makeFactory<Person>({ | ||
id: Factory.each(i => i), | ||
firstName: 'Bob', | ||
lastName: 'Smith', | ||
fullName: 'Robert J. Smith, Jr.', | ||
firstName: "Bob", | ||
lastName: "Smith", | ||
fullName: "Robert J. Smith, Jr.", | ||
age: Factory.each(i => 20 + (i % 10)) | ||
@@ -37,3 +40,3 @@ }); | ||
For each property of Person, you can specify a default value, or call `Factory.each`. `Factory.each` takes a lambda with a sequence number that is incremented automatically between generating instances of your type (`Person` in our example). | ||
For each property of Person, you can specify a default value, or call `Factory.Sync.each`. `Factory.Sync.each` takes a lambda with a sequence number that is incremented automatically between generating instances of your type (`Person` in our example). | ||
@@ -43,6 +46,9 @@ You can call `personFactory.build` with a subset of field data (`Partial<Person>`) to override defaults, and the output will be an object that conforms to Person using the definition specified in `makeFactory`. | ||
```typescript | ||
const james = personFactory.build({firstName: 'James', fullName: 'James Smith'}); | ||
const james = personFactory.build({ | ||
firstName: "James", | ||
fullName: "James Smith" | ||
}); | ||
// { id: 1, firstName: 'James', lastName: 'Smith', fullName: 'James Smith', age: 21 }; | ||
const youngBob = personFactory.build({age: 5}); | ||
const youngBob = personFactory.build({ age: 5 }); | ||
// { id: 2, firstName: 'Bob', lastName: 'Smith', fullName: 'Robert J. Smith, Jr.', age: 5 }; | ||
@@ -60,3 +66,3 @@ ``` | ||
```typescript | ||
const theBradyBunch = personFactory.buildList(8, { lastName: 'Brady' }); | ||
const theBradyBunch = personFactory.buildList(8, { lastName: "Brady" }); | ||
``` | ||
@@ -70,3 +76,3 @@ | ||
const anyAgeFactory = personFactory.extend({ | ||
age: Factory.each(() => randomAge(0,100)), // randomAge(min:number, max:number) => number | ||
age: Factory.each(() => randomAge(0, 100)) // randomAge(min:number, max:number) => number | ||
}); | ||
@@ -84,6 +90,12 @@ | ||
```typescript | ||
const autoFullNameFactory = personFactory.withDerivation2(['firstName', 'lastName'], 'fullName', | ||
(fName, lName) => `${lName}, ${fName} ${lName}`); | ||
const autoFullNameFactory = personFactory.withDerivation2( | ||
["firstName", "lastName"], | ||
"fullName", | ||
(fName, lName) => `${lName}, ${fName} ${lName}` | ||
); | ||
const jamesBond = autoFullNameFactory.build({ firstName: 'James', lastName: 'Bond' }); | ||
const jamesBond = autoFullNameFactory.build({ | ||
firstName: "James", | ||
lastName: "Bond" | ||
}); | ||
// { id: 1, firstName: 'James', lastName: 'Bond', fullName: 'Bond, James Bond', age: 21 }; | ||
@@ -99,4 +111,6 @@ ``` | ||
```typescript | ||
const autoFullNameFactory = personFactory.withDerivation('fullName', (person) => | ||
`${person.lName}, ${person.fName} ${person.lName}`); | ||
const autoFullNameFactory = personFactory.withDerivation( | ||
"fullName", | ||
person => `${person.lName}, ${person.fName} ${person.lName}` | ||
); | ||
``` | ||
@@ -110,1 +124,8 @@ | ||
## Async Factories | ||
Async factories support all the same methods as sync factories, but you can also provide generators that create Promise<T> instead of T. Consequently each property may or may not use asynchronicity for generation, but the final factory requires only one await. | ||
### `transform()` | ||
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. |
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
17809
122