Skip to content

Commit

Permalink
fix(jest/lib): template mutation with createSpyObject (#209)
Browse files Browse the repository at this point in the history
Fix bug causing the template object to mutate when creating spy object for jest
  • Loading branch information
eweap authored and NetanelBasal committed Oct 15, 2019
1 parent 093d0c9 commit e23af88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion projects/spectator/jest/src/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type SpyObject<T> = BaseSpyObject<T> & { [P in keyof T]: T[P] & (T[P] ext
* @internal
*/
export function createSpyObject<T>(type: InjectableType<T>, template?: Partial<Record<keyof T, any>>): SpyObject<T> {
const mock: any = template || {};
const mock: any = { ...template } || {};

installProtoMethods(mock, type.prototype, () => {
const jestFn = jest.fn();
Expand Down
16 changes: 16 additions & 0 deletions projects/spectator/jest/test/mock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { mockProvider } from '@ngneat/spectator/jest';

import { WidgetService } from '../../test/widget.service';

describe('mockProvider', () => {
it('should not modify the object passed in 2nd argument when running the mock factory', () => {
const customPropertiesAndMethods: Partial<Record<keyof WidgetService, any>> = {
testingProperty: 'overriden'
};
const { useFactory: factory } = mockProvider(WidgetService, customPropertiesAndMethods);
factory();
expect(customPropertiesAndMethods).toEqual({
testingProperty: 'overriden'
});
});
});

0 comments on commit e23af88

Please sign in to comment.