What is ts-mockito?
ts-mockito is a mocking library for TypeScript that allows developers to create mock objects and verify interactions with them. It is inspired by the Java library Mockito and provides a fluent API for creating and using mocks in unit tests.
What are ts-mockito's main functionalities?
Creating Mocks
This feature allows you to create a mock object of a class. The mock object can then be used to stub methods and verify interactions.
const myMock = mock(MyClass);
Stubbing Methods
This feature allows you to define the behavior of a mock object's method. In this example, when `someMethod` is called on `myMock`, it will return 'someValue'.
when(myMock.someMethod()).thenReturn('someValue');
Verifying Interactions
This feature allows you to verify that a method on a mock object was called a specific number of times. In this example, it verifies that `someMethod` was called exactly once.
verify(myMock.someMethod()).once();
Capturing Arguments
This feature allows you to capture the arguments passed to a mock object's method. In this example, it captures the arguments of the last call to `someMethod`.
const [arg1, arg2] = capture(myMock.someMethod).last();
Other packages similar to ts-mockito
jest
Jest is a popular testing framework for JavaScript and TypeScript that includes built-in mocking capabilities. Unlike ts-mockito, Jest provides an all-in-one solution for testing, including test runners, assertion libraries, and mocking functionalities.
sinon
Sinon is a standalone test spies, stubs, and mocks library for JavaScript. It provides similar functionalities to ts-mockito but is more focused on JavaScript and does not provide the same TypeScript-specific features.
typemoq
typemoq is another mocking library for TypeScript. It provides a similar API to ts-mockito but has different syntax and features. It is also inspired by the .NET Moq library.
ts-mockito
Mocking library for TypeScript inspired by http://mockito.org/
This is beta version!
Main features
- Strongly typed
- IDE autocomplete
- Mock creation (
mock
) - Changeing mock behavior (
when
) via:
thenReturn
- return valuethenThrowsError
- throw an error
- Checking if methods were called with given arguments (
verify
)
anything
, notNull
, anyString
etc. - for more flexible comparisiononce
, twice
, times
, atLeast
etc. - allows call count verification
- Recording multiple behaviors
Usage
Basics
let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);
foo.getBar(3);
foo.getBar(5);
verify(fooMock.getBar(3)).called();
verify(fooMock.getBar(5)).called();
Stubbing method calls
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(3)).thenReturn('three');
let foo:Foo = instance(mockedFoo);
console.log(foo.getBar(3));
console.log(foo.getBar(999));
Call count verification
let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);
foo.getBar(1);
foo.getBar(2);
foo.getBar(2);
foo.getBar(3);
verify(mockedFoo.getBar(1)).once();
verify(mockedFoo.getBar(2)).twice();
verify(mockedFoo.getBar(between(2, 3))).thrice();
verify(mockedFoo.getBar(anyNumber()).time(4);
verify(mockedFoo.getBar(2)).atLeast(2);
verify(mockedFoo.getBar(1)).atMoast(1);
verify(mockedFoo.getBar(4)).never();
Recording multiple behaviors
If more than one behavior is set, first matching is executed and removed
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');
let foo:Foo = instance(mockedFoo);
console.log(foo.getBar(1));
console.log(foo.getBar(1));
console.log(foo.getBar(1));
console.log(foo.getBar(1));
Another example with specific values
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(1)).thenReturn('one');
when(mockedFoo.getBar(1)).thenReturn('second time one');
when(mockedFoo.getBar(2)).thenReturn('two');
let foo:Foo = instance(mockedFoo);
console.log(foo.getBar(1));
console.log(foo.getBar(1));
console.log(foo.getBar(1));
console.log(foo.getBar(2));
console.log(foo.getBar(2));
Thanks