willryan / factory.ts

A library to ease creation of factories for test data for Typescript
MIT License
431 stars 23 forks source link

Default merging of objects within factories? #84

Open agroebe opened 11 months ago

agroebe commented 11 months ago

Hello!

First off, thanks for making and maintaining this project. We've enjoyed using these factories for improving our test data.

This isn't a bug per-say, but I had a question about something I've noticed while using factory.ts. Specifically, it seems that, when adding nested objects to factories, there is some sort of merging of data that happens with the default provided.

i.e. consider this example:

const personFactory = Factory.makeFactory<Person>({
    firstName: "John",
    lastName: "Doe",
    age: 45,
    extras: {
        occupation: "Accountant",
        country: "USA"
    }
})

Now, let's say I want to OVERRIDE entirely what is stored in extras, with the following example:

const person = personFactory.build({
    extras: {
        married: true,
        weight: 150
    }
})

The problem is, this doesn't actually override what is stored in the default factory, and instead it does some kind of merging to leave me with this result:

{
    firstName: "John",
    lastName: "Doe",
    age: 45,
    extras: {
        occupation: "Accountant",
        country: "USA",
        married: true,
        weight: 150
    }
}

In other words, my custom overridden has been merged into one with the default data.

My question is: what is the motivation behind this and is there anyway to opt out of this? What would be the recommended approach if I wanted to be able to actually override something? I'd hate to make an entirely new factory just for this one case.