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

fix(jest/lib): template mutation with createSpyObject #209

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
});
});
});