Skip to content

Commit

Permalink
feat(global): add provideAutoSpy util for Angular tests
Browse files Browse the repository at this point in the history
this saves time by shortening the repetitive syntax of providing an auto spy in Angular tests.
Credit for the idea goes to Benjamin Taylor.

closes #26
  • Loading branch information
shairez committed Dec 22, 2020
1 parent 7d9a2c8 commit c0680f7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/jasmine-auto-spies/src/angular-provider-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ClassType, OnlyMethodKeysOf } from '@hirez_io/auto-spies-core';
import { ClassSpyConfiguration, createSpyFromClass } from './create-spy-from-class';
import { Spy } from './jasmine-auto-spies.types';

export type AngularValueProvider<T> = {
provide: ClassType<T>;
useValue: Spy<T>;
};

export function provideAutoSpy<T>(
ObjectClass: ClassType<T>,
methodsToSpyOnOrConfig?: OnlyMethodKeysOf<T>[] | ClassSpyConfiguration<T>
): AngularValueProvider<T> {
return {
provide: ObjectClass,
useValue: createSpyFromClass(ObjectClass, methodsToSpyOnOrConfig),
};
}
1 change: 1 addition & 0 deletions packages/jasmine-auto-spies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './jasmine-auto-spies.types';
export { createSpyFromClass, ClassSpyConfiguration } from './create-spy-from-class';
export { createFunctionSpy } from './create-function-spy';
export { createObservableWithValues } from '@hirez_io/auto-spies-core';
export * from './angular-provider-helper';
1 change: 1 addition & 0 deletions packages/jasmine-auto-spies/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function getWebpackConfig(forTest: boolean = false): webpack.Configuratio
/node_modules/,
/dist/,
/fake-classes-to-test.ts/,
/angular-provider-helper.ts/,
/error-handler.ts/,
/create-observable-with-values.ts/,
],
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-auto-spies/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module.exports = {
statements: 90,
},
},
coveragePathIgnorePatterns: ['/node_modules/', 'src/tests'],
coveragePathIgnorePatterns: [
'/node_modules/',
'src/tests',
'src/angular-provider-helper.ts',
],
setupFilesAfterEnv: [
'../../node_modules/@hirez_io/jest-given/dist/jest-given.js',
'../../node_modules/@hirez_io/observer-spy/dist/setup-auto-unsubscribe.js',
Expand Down
18 changes: 18 additions & 0 deletions packages/jest-auto-spies/src/angular-provider-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ClassType, OnlyMethodKeysOf } from '@hirez_io/auto-spies-core';
import { ClassSpyConfiguration, createSpyFromClass } from './create-spy-from-class';
import { Spy } from './jest-auto-spies.types';

export type AngularValueProvider<T> = {
provide: ClassType<T>;
useValue: Spy<T>;
};

export function provideAutoSpy<T>(
ObjectClass: ClassType<T>,
methodsToSpyOnOrConfig?: OnlyMethodKeysOf<T>[] | ClassSpyConfiguration<T>
): AngularValueProvider<T> {
return {
provide: ObjectClass,
useValue: createSpyFromClass(ObjectClass, methodsToSpyOnOrConfig),
};
}
1 change: 1 addition & 0 deletions packages/jest-auto-spies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './jest-auto-spies.types';
export { createSpyFromClass, ClassSpyConfiguration } from './create-spy-from-class';
export { createFunctionSpy } from './create-function-spy';
export { createObservableWithValues } from '@hirez_io/auto-spies-core';
export * from './angular-provider-helper';

0 comments on commit c0680f7

Please sign in to comment.