Reputation: 33
I am new to TypeScript, earlier worked on Java. I am looking for java junit(Mockito) equivalent to TypeScript. In junit in each test we can define the dependency behavior and return the response as per the test case demand. is there similar way available in jest also ? in which i can simply define. when(dependencyFunction()).then(mockResponse1);
and in different test when(dependencyFunction()).then(mockResponse2);
Here is how my typescript class look like:
class ABC {
static fun1(){
const xyz = await dependency();
return xyz === 'DONE';
}
}
Here i want to write test cases in which i can define the mocked response in each test cases.
Upvotes: 0
Views: 404
Reputation: 1069
from the documentation you can use mock functions for this
and use mockReturnValueOnce
or mockReturnValue
for your responses
const myMock = jest.fn();
myMock
.mockReturnValueOnce(10) // set the response once
.mockReturnValueOnce('x')
.mockReturnValue(true); // set a persistent response
console.log(myMock(), myMock(), myMock(), myMock());
// > 10, 'x', true, true
Upvotes: 1