Skip to content

Commit

Permalink
fix(spectator): make mock template type safe (#607)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: introducing type safety for the template values in
createSpyObject and mockProvider. This might cause breaking builds
because the compiler will find issues hidden until now.
  • Loading branch information
szabyg committed May 16, 2023
1 parent 12b7ebe commit 0c42542
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions projects/spectator/src/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function installProtoMethods<T>(mock: any, proto: any, createSpyFn: Funct
/**
* @publicApi
*/
export function createSpyObject<T>(type: Type<T> | AbstractType<T>, template?: Partial<Record<keyof T, any>>): SpyObject<T> {
export function createSpyObject<T>(type: Type<T> | AbstractType<T>, template?: Partial<T>): SpyObject<T> {
const mock: any = { ...template } || {};

installProtoMethods<T>(mock, type.prototype, name => {
Expand All @@ -90,7 +90,7 @@ export function createSpyObject<T>(type: Type<T> | AbstractType<T>, template?: P
/**
* @publicApi
*/
export function mockProvider<T>(type: Type<T> | AbstractType<T>, properties?: Partial<Record<keyof T, any>>): FactoryProvider {
export function mockProvider<T>(type: Type<T> | AbstractType<T>, properties?: Partial<T>): FactoryProvider {
return {
provide: type,
useFactory: () => createSpyObject(type, properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import { RouterStub } from './router-stub';
*/
export function initialRoutingModule<S>(options: Required<SpectatorRoutingOptions<S>>): ModuleMetadata {
const moduleMetadata = initialSpectatorModule(options);
const eventsSubject = new Subject<Event>();

if (options.stubsEnabled) {
moduleMetadata.imports.push(RouterTestingModule);
moduleMetadata.providers.push(
options.mockProvider(RouterStub, {
events: new Subject<Event>(),
events: eventsSubject.asObservable(),
emitRouterEvent(event: Event): void {
this.events.next(event);
eventsSubject.next(event);
},
serializeUrl(): string {
return '/';
Expand Down

0 comments on commit 0c42542

Please sign in to comment.