https://github.com/atlassian-labs/unstoppable-mockery
Unstoppable Mockery is a lightweight utility for creating type-safe, fully mocked classes and interfaces for Jest tests. Unlike jest.mock('<module-path>'), it allows you to mock classes and interfaces directly, with full TypeScript support and without relying on module boundaries.
https://github.com/atlassian-labs/unstoppable-mockery
jest mocking mockito testing unit-testing
Last synced: 11 days ago
JSON representation
Unstoppable Mockery is a lightweight utility for creating type-safe, fully mocked classes and interfaces for Jest tests. Unlike jest.mock('<module-path>'), it allows you to mock classes and interfaces directly, with full TypeScript support and without relying on module boundaries.
- Host: GitHub
- URL: https://github.com/atlassian-labs/unstoppable-mockery
- Owner: atlassian-labs
- License: apache-2.0
- Created: 2025-09-01T00:18:57.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-09-17T02:04:14.000Z (about 1 month ago)
- Last Synced: 2025-09-19T10:28:01.904Z (28 days ago)
- Topics: jest, mocking, mockito, testing, unit-testing
- Language: TypeScript
- Homepage:
- Size: 71.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# unstoppable-mockery
[](LICENSE) [](CONTRIBUTING.md)
Unstoppable Mockery is a lightweight utility for creating type-safe, fully mocked classes and interfaces for Jest tests. Unlike `jest.mock('')`, it allows you to mock classes and interfaces directly, with full TypeScript support and without relying on module boundaries. This makes your tests more maintainable, readable, and less brittle to refactoring.
## Usage
### Mocking Classes
```typescript
import { mockClass } from 'unstoppable-mockery';class MyService {
doSomething() { /* ... */ }
getValue() { return 42; }
}const mock = mockClass(MyService, { getValue: () => 100 });
// All prototype methods are jest.fn()
expect(jest.isMockFunction(mock.doSomething)).toBe(true);// Overridden property
expect(mock.getValue()).toBe(100);
```### Mocking Interfaces
```typescript
import { mockInterface } from 'unstoppable-mockery';interface ApiClient {
fetchData: (id: string) => Promise;
isConnected: boolean;
}const mockApi = mockInterface({ isConnected: true });
// Dynamically created jest.fn() for interface methods
mockApi.fetchData.mockResolvedValue({ data: 'test' });// Override property is used
expect(mockApi.isConnected).toBe(true);// Any property access creates a mock function
expect(jest.isMockFunction(mockApi.fetchData)).toBe(true);
```### Why use unstoppable-mockery instead of jest.mock?
- **Type Safety:** Directly mock classes and interfaces with full TypeScript support.
- **No Module Boundaries:** Mock any class/interface, even if not exported from a module.
- **Refactor-Friendly:** Your tests won't break if you move or rename files/classes.
- **Fine-Grained Control:** Override specific methods/properties easily.
- **Cleaner Tests:** No need for manual mock implementations or resetting modules.## Installation
```sh
npm install unstoppable-mockery --save-dev
```## Documentation
See the source code and TSDoc comments for API details. For advanced usage, refer to the examples in the repository.
## Tests
To run tests:
```sh
npm test
```## Contributions
Contributions to unstoppable-mockery are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
## License
Copyright (c) 2025 Atlassian US., Inc.
Apache 2.0 licensed, see [LICENSE](LICENSE) file.[](https://www.atlassian.com)