Using Jest I've mockimplemented the method and testcase is getting succeeded and for testcase2 I need to implement same mock method with different input and I'm unable to do that.
jest.mock('../../lib/testService', () => ({
TestService : jest.fn(() => ({
callAPIProd: jest.fn().mockImplementation(() => Promise.resolve([{ DateTime: '2022-01-01'}])),
getAuthToken: jest.fn().mockImplementation(() => Promise.resolve({ data: 'mock token' })),
checkIfExistsInDB: jest.fn().mockImplementation(() => Promise.resolve(false)),
})),
}));
describe('getRates', () => {
it('should insert rates into DB if not already available', async () => {
// Arrange
const req = {} as Request;
const res = {
send: jest.fn(),
} as unknown as Response;
// Act
await integration.getRates(req, res);
// Assert
expect(res.send).toHaveBeenCalledWith('New Rates Received');
});
it('should send response if rates are already available in DB', async () => {
// Arrange
const req = {} as Request;
const res = {
send: jest.fn(),
} as unknown as Response;
// Act
await integration.getRates(req, res);
// Assert
expect(res.send).toHaveBeenCalledWith('Rates already available in DB');
});
});
In my getRates method I'm calling the "checkIfExistsInDB" service method which ret=turns true/false. First testcase is getting succeeded as I've mocked the implementation with false value. I want to clear the mockimplementation and set new mockimplementation with true value. How to achieve that?
Tried by re-defining the mock implementation in specific testcase
it('should send response if rates are already available in DB', async () => {
// Arrange
const req = {} as Request;
const res = {
send: jest.fn(),
} as unknown as Response;
//#1 - Tried to re-define in this way but it didn't work it's still taking value as false
jest.mock('../../lib/testService', () => ({
TestService : jest.fn(() => ({
callAPIProd: jest.fn().mockImplementation(() => Promise.resolve([{ DateTime: '2022-01-01'}])),
getAuthToken: jest.fn().mockImplementation(() => Promise.resolve({ data: 'mock token' })),
checkIfExistsInDB: jest.fn().mockImplementation(() => Promise.resolve(true)),
})),
}));
//#2 - Tried to re-define in this way but it didn't work it's still taking value as false
TestService.prototype.checkIfExistsInDB = jest.fn().mockImplementation(() => Promise.resolve(true));
// Act
await integration.getRates(req, res);
// Assert
expect(res.send).toHaveBeenCalledWith('Rates already available in DB');
});