Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
mock
✓ import { mock } from 'ts-mockito';
✗ const { mock } = require('ts-mockito');
CommonJS require() syntax is outdated for modern TypeScript projects; use ES Module imports.
instance
✓ import { instance } from 'ts-mockito';
✗ import instance from 'ts-mockito';
This is a named export, not the default export. Importing as default is incorrect.
when
✓ import { when } from 'ts-mockito';
✗ const when = require('ts-mockito').when;
Prefer named ES Module imports over CommonJS destructuring for better static analysis and tree-shaking.
verify
✓ import { verify } from 'ts-mockito';
✗ import { Verify } from 'ts-mockito';
The function name is `verify` (lowercase), not `Verify`.
deepEqual
✓ import { deepEqual } from 'ts-mockito';
Commonly used matcher. Type checking was improved in v2.5.0, potentially causing new compilation errors.
Demonstrates basic ts-mockito usage: creating a mock, stubbing method calls and getters, handling async operations, and verifying method invocations with specific arguments and call counts.
import { mock, instance, when, verify, anyNumber } from 'ts-mockito';
class FooService {
getBar(value: number): string {
throw new Error('Should not be called directly');
}
async getAsyncData(id: string): Promise<string> {
return Promise.resolve(`Data for ${id}`);
}
get sampleGetter(): string {
return 'realValue';
}
}
// Create a mock instance of FooService
const mockedFooService: FooService = mock(FooService);
// Stub a method call to return a specific value
when(mockedFooService.getBar(3)).thenReturn('three');
when(mockedFooService.getBar(anyNumber())).thenReturn('anyNumberResult');
// Stub a getter property
when(mockedFooService.sampleGetter).thenReturn('mockedGetterValue');
// Stub an async method to resolve a promise
when(mockedFooService.getAsyncData('testId')).thenResolve('resolvedData');
// Get the mock instance to use in your code
const fooService: FooService = instance(mockedFooService);
// Use the mocked instance
console.log('Call with 3:', fooService.getBar(3)); // Should print 'three'
console.log('Call with 5:', fooService.getBar(5)); // Should print 'anyNumberResult'
console.log('Getter value:', fooService.sampleGetter); // Should print 'mockedGetterValue'
fooService.getAsyncData('testId').then(data => {
console.log('Async data:', data); // Should print 'resolvedData'
});
// Verify that methods were called as expected
verify(mockedFooService.getBar(3)).once();
verify(mockedFooService.getBar(anyNumber())).thrice(); // 3, 5, and the `anyNumber()` for the `5` call
verify(mockedFooService.getAsyncData('testId')).called();
// Optionally, verify that an unexpected call was NOT made (e.g., if you only stubbed 'testId')
// verify(mockedFooService.getAsyncData('otherId')).never();
Errors
Common errors & fixes
Maximum call stack size exceeded
Specific scenarios involving deeply nested calls or complex mocking setups could trigger a stack overflow in earlier versions.
fixThis issue was fixed in v2.2.9. Ensure you are using ts-mockito version 2.2.9 or newer.
Type 'Foo' is not assignable to type 'Partial<Foo>'. Property 'bar' is missing in type 'Partial<Foo>' but required in type 'Foo'.
A common type error when using the `deepEqual` matcher after v2.5.0 if the expected value's type does not perfectly align with the actual argument's type, especially when the expected type has more required fields.
fixCast the expected argument to `Partial<YourType>` or `any` if a partial match is intended, e.g., `verify(mockedService.method(deepEqual(expectedParams as Partial<ServiceType>))).called();`
Error: Expected "methodName(arg)" to be called X time(s). But has been called Y time(s).
This is a runtime error from ts-mockito's `verify` function indicating a mismatch between the expected number of calls for a specific method/argument combination and the actual calls made to the mock.
fixReview your test logic to ensure the mock method is being called the expected number of times or adjust your `verify` expectation (e.g., `once()`, `twice()`, `times(count)`, `atLeast(count)`).
TypeError: Cannot read properties of undefined (reading '__ts_mockito_args')
Can occur if `instance()` is not called to retrieve the mockable object before passing it to the code under test, or if the `mock()` function is passed an invalid argument.
fixAlways ensure you call `instance(mockedObject)` to get the actual mocked object to interact with. Double-check that `mock()` is receiving a valid class or abstract class.
Audit
Dependencies
No dependency data recorded yet.