Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Support generating factories #54

Closed
Xiphe opened this issue Apr 14, 2021 · 1 comment
Closed

RFC: Support generating factories #54

Xiphe opened this issue Apr 14, 2021 · 1 comment

Comments

@Xiphe
Copy link

Xiphe commented Apr 14, 2021

When using mock data in tests I would like to

  1. overwrite partial data (and have type-support for it out of the box)
  2. be able to create different variants of the same data-type

From this:

interface Person {
   email: string
   /** @mockType {name.firstName} */
   firstName: string
}
interface Family {
  /** @mockType {name.lastName} */
  name: string
  members: Person[]
}

To this:

import faker from 'faker';
import type * as Types from './types';

export function Person(overwrites: Partial<Types.Person> = {}): Types.Person {
  return {
    email: faker.internet.email(),
    firstName: faker.name.firstName(),
    ...overwrites
  }
}

export function Family(overwrites: Partial<Types.Family> = {}): Types.Family {
  return {
    name: faker.name.lastName(),
    members: Array.from({ length: faker.random.number(8) }).map(() => Person()),
    ...overwrites
  }
}

I guess supporting exactly this would be to specific for this library and possibly extend its scope but a intermediate step would be to support a "raw" outputFormat like this

{
  "Person": {
     "email": { "fake": "internet.email" },
     "firstName": { "fake": "name.firstName" }
  },
  "Family": {
    "firstName": { "fake": "name.lastName" },
    "members": { 
      "array": {
        "range": [1, 8],
        "contains": [{ "type": "Person" }]
      }
    }
  } 
}

which could then be used to create such factories.


What's your opinion on this? If it sounds interesting, I'd like to create a PR.

@Xiphe Xiphe changed the title Support generating factories RFC: Support generating factories Apr 14, 2021
@Xiphe
Copy link
Author

Xiphe commented Apr 15, 2021

Closing since i'm currently making good progress combining https://github.com/vega/ts-json-schema-generator and https://github.com/json-schema-faker/json-schema-faker to build the factories.

@Xiphe Xiphe closed this as completed Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant