I'm exploring ways to streamline the creation of mock data for unit testing within an Angular solution. Currently, I am defining interfaces such as:
export interface ReferenceDataItemCommon {
codeDescription?: string;
code: string;
deleted?: boolean;
}
These interfaces are used as data types in our application. At the moment, I am using Factory.ts along with Faker to generate fake objects for testing purposes:
export const fakeReferenceDataItemCommon = Factory.Sync.makeFactory<ReferenceDataItemCommon>({
code: Factory.each(() => Faker.lorem.word()),
codeDescription: Factory.each(() => Faker.lorem.sentence(4)),
});
However, I am interested in simplifying this process further to quickly create objects for testing purposes. Is it feasible in Typescript to have a generic method that can return an object of a specific datatype?
const fake = createFake<ReferenceDataItemCommon>();
My initial idea involves iterating through keys of an object and generating random values based on the type. For more complex objects, the method would be called recursively. Is this approach possible, and if so, what would be a better way to implement it as I'm feeling a bit overwhelmed by the concept?