GenericMockClass
is a TypeScript class that provides a convenient way to create mock objects with spies and overrides for testing purposes.
It is designed to be flexible and easy to use in various testing scenarios.
The GenericMockClass
is designed to harness the capabilities of TypeScript generics, enabling autocomplete suggestions for methods and properties on the mock that mirror those available on the actual object.
- Create mock objects with predefined overrides.
- Automatically generates spies for methods or properties.
- Easily override specific methods or properties.
- Mirror the interface of the actual object.
Install jest-generic-mock-class
using npm:
npm install jest-generic-mock-class
interface ServiceToMock {
methodToBeSpied: () => void;
}
const mock = GenericMockClass.create<ServiceToMock>();
// Call the method
mock.methodToBeSpied();
expect( mock.getSpyFor( 'methodToBeSpied' ) ).toHaveBeenCalled();
interface ServiceToMock {
methodToBeSpied: ( foo: string, bar: string ) => void;
}
const mock = GenericMockClass.create<ServiceToMock>();
// Call the method with arguments
mock.methodToBeSpied( 'foo', 'bar' );
expect( mock.getSpyFor( 'methodToBeSpied' ) ).toHaveBeenCalledWith( 'foo', 'bar' );
expect( mock.getSpyFor( 'methodToBeSpied' ) ).not.toHaveBeenCalledWith( 'baz' );
interface ServiceToMock {
methodToBeSpied: () => number;
}
const mock = GenericMockClass.create<ServiceToMock>( { methodToBeSpied: jest.fn().mockReturnValue( 123 ) } );
const returnValue = mock.methodToBeSpied();
expect( returnValue ).toBe( 123 );
interface ServiceToMock {
methodToBeSpied: () => number;
}
const mock = GenericMockClass.create<ServiceToMock>( { methodToBeSpied: jest.fn().mockReturnValue( 123 ) } );
// Change the return value
mock.override({ methodToBeSpied: jest.fn().mockReturnValue( 456 ) } );
// Call method
const returnValue = mock.methodToBeSpied();
expect( returnValue ).toBe( 456 );
expect( mock.getSpyFor( 'methodToBeSpied' ) ).toHaveBeenCalled();
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.