Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rohit1901/mockable
Mockable automates the mock generation process while maintaining type safety and ease of use. #library
https://github.com/rohit1901/mockable
ast generator mocks testing typescript
Last synced: 12 days ago
JSON representation
Mockable automates the mock generation process while maintaining type safety and ease of use. #library
- Host: GitHub
- URL: https://github.com/rohit1901/mockable
- Owner: rohit1901
- License: mit
- Created: 2023-08-20T11:49:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-05T10:56:47.000Z (over 1 year ago)
- Last Synced: 2024-11-16T19:33:40.542Z (2 months ago)
- Topics: ast, generator, mocks, testing, typescript
- Language: TypeScript
- Homepage:
- Size: 108 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# mockable: Automated Mocking for Unit Testing
![Typescript](https://shields.io/badge/TypeScript-3178C6?logo=TypeScript&logoColor=FFF&style=flat-square)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)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.
## Installation
```bash
npm install mockable --save-dev
```## Usage
### 1. Annotate Classes or Interfaces
Use the `@Mockable` decorator to indicate which classes or interfaces you want to mock. This will trigger the automated mock generation process.
```typescript
import { Mockable } from 'mockable';@Mockable
class UserService {
getUser(id: number): Promise {
// Actual implementation fetching user from a server
}
}
```### 2. Import and Use Mocks in Tests
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.
```typescript
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');
});
```### 3. Customize Mock Behavior
You can customize the behavior of the mock methods using Jest's mocking capabilities.
```typescript
MockUserService.getUser.mockResolvedValue({ id: 1, name: 'Custom Name' });
```## Features
- 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.## Examples
For more detailed examples and advanced usage, please refer to the [Examples](./src/examples) directory in this repository.
## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
## License
This project is licensed under the [MIT License](./LICENSE).