diff --git a/lib/__tests__/factory-builders.test.ts b/lib/__tests__/factory-builders.test.ts index 5127fad..d8cec32 100644 --- a/lib/__tests__/factory-builders.test.ts +++ b/lib/__tests__/factory-builders.test.ts @@ -211,6 +211,11 @@ describe('associations', () => { }); describe('params', () => { + interface AdminUser extends User { + admin: true; + adminPrivileges: string[]; + } + it('adds parameters that are then used for build', () => { const user = userFactory.params({ admin: true }).build(); expect(user.admin).toBe(true); @@ -226,6 +231,38 @@ describe('params', () => { expect(adminFactory.build().admin).toBe(true); expect(adminFactory.build().admin).toBe(true); }); + + it('adds parameters for a sub-type which are then also accepted in build()', () => { + const adminFactory = userFactory.params({ + admin: true, + adminPrivileges: ['any-privilege'], + }); + + expect(adminFactory.build()).toEqual({ + admin: true, + adminId: null, + adminPrivileges: ['any-privilege'], + firstName: 'Yussef', + id: '1', + lastName: 'Sanchez', + memberId: null, + post: { id: '1' }, + registered: false, + }); + expect( + adminFactory.build({ adminPrivileges: ['changed-privilege'] }), + ).toEqual({ + admin: true, + adminId: null, + adminPrivileges: ['changed-privilege'], + firstName: 'Yussef', + id: '1', + lastName: 'Sanchez', + memberId: null, + post: { id: '1' }, + registered: false, + }); + }); }); describe('transient', () => { diff --git a/lib/factory.ts b/lib/factory.ts index a7070da..f1bd771 100644 --- a/lib/factory.ts +++ b/lib/factory.ts @@ -124,10 +124,12 @@ export class Factory { * @param params * @returns a new factory */ - params(params: DeepPartial): this { + params( + params: DeepPartial, + ): Factory { const factory = this.clone(); factory._params = merge({}, this._params, params, mergeCustomizer); - return factory; + return factory as unknown as Factory; } /**