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

How to randomise nested object key in buildList #78

Open
MRwt48 opened this issue May 1, 2023 · 1 comment
Open

How to randomise nested object key in buildList #78

MRwt48 opened this issue May 1, 2023 · 1 comment

Comments

@MRwt48
Copy link

MRwt48 commented May 1, 2023

BuildList only randomizes the first level key value.

Example:

const factory = Factory.Sync.makeFactory({
    randomItem: Factory.each(()=> radomValueGenerator())  // using faker-js
    someObject: {
        randomNestedItem: Factory.each(()=> radomValueGenerator())
    }
})

const results = factory.buildList(3);

Expected

[
    {
        randomItem: "random value 1",
        someObject: {
            randomNestedItem: "random nested value 1",
        }
    },
    {
        randomItem: "random value 2",
        someObject: {
            randomNestedItem: "random nested value 2",
        }
    },
    {
        randomItem: "random value 3",
        someObject: {
            randomNestedItem: "random nested value 3"
        }
    }
]

Actual

[
    {
        randomItem: "random value 1",
        someObject: {
            randomNestedItem: Generator,
        }
    },
    {
        randomItem: "random value 2",
        someObject: {
            randomNestedItem: Generator
        }
    },
    {
        randomItem: "random value 3",
        someObject: {
            randomNestedItem: Generator
        }
    }
]

Also tried using

const factory = Factory.Sync.makeFactory({
    ...
    someObject: Factory.Sync.makeFactory({
        randomNestedItem: Factory.each(()=> radomValueGenerator())
    }).build()
})

but I get

[
    {
        randomItem: "random value 1",
        someObject: {
            randomNestedItem: "random nested value 1", // same value repeated
        }
    },
    {
        randomItem: "random value 2",
        someObject: {
            randomNestedItem: "random nested value 1", // same value repeated
        }
    },
    {
        randomItem: "random value 3",
        someObject: {
            randomNestedItem: "random nested value 1", // same value repeated
        }
    }
]
@MRwt48 MRwt48 changed the title How to randomise nested object key on buildList How to randomise nested object key in buildList May 1, 2023
@SendDerek
Copy link

SendDerek commented Jun 26, 2023

👍 I'm seeing the same issue.

My workaround was to create a new interface for the nested item and create it's own factory:

Child object:

const childObjectFactory = Factory.Sync.makeFactory<MachineTotalUsage>({
  hours: Sync.each(() => faker.number.int({ min: 0, max: 999 })),
  distance: Sync.each(() => faker.number.float({ precision: 0.01, min: 10, max: 9999 })),
})

Then, use that factory inside the parent object:

const parentObjectFactory = Factory.Sync.makeFactory<Machine>({
  displayName: Sync.each(() => faker.internet.displayName()),
  childObject: ChildObjectFactory.build()
})

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

2 participants