The mockable
library simplifies unit testing by automating the generation of mock implementations for TypeScript classes and interfaces. With mockable
, you can quickly create mock objects that adhere to the type definitions of your original code, reducing the need for manual mock creation and maintenance.
npm install mockable --save-dev
Use the @Mockable
decorator to indicate which classes or interfaces you want to mock. This will trigger the automated mock generation process.
import { Mockable } from 'mockable';
@Mockable
class UserService {
getUser(id: number): Promise<User> {
// Actual implementation fetching user from a server
}
}
In your test files, import the generated mock implementations and use them in your tests. The mock implementations will match the methods and properties of the original class/interface.
import { MockUserService } from 'mockable';
// Use the mock in your tests
jest.mock('UserService', () => MockUserService);
// Example test using the mock
test('should return a user', async () => {
MockUserService.getUser.mockResolvedValue({ id: 1, name: 'John' });
const user = await someFunctionThatUsesUserService();
expect(user.name).toBe('John');
});
You can customize the behavior of the mock methods using Jest's mocking capabilities.
MockUserService.getUser.mockResolvedValue({ id: 1, name: 'Custom Name' });
- Automatic generation of mock implementations for annotated classes/interfaces.
- Retains type safety by inferring method signatures and return types from the original code.
- Customizable mock behavior using Jest's mocking features.
- Reduces boilerplate and speeds up unit test writing and maintenance.
For more detailed examples and advanced usage, please refer to the Examples directory in this repository.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.