I am writing a test for an API which calls a nested api multiple times to get a key value pair. The value will always be a boolean
and I am trying to mock this service aka KeyValueService
in the code below. These and other more booleans are used in the PhotoService
and I would like to mock these values so I can change the test to match these values.
I have mocked the booleans and also tried setting mockResolveValuetwice
to true
twice thinking that it may apply true
for both variables valueA
and valueB
, but it did not work. I will be using this nested service multiple times and not just twice. So far none of the solutions worked. How can I get a desired value for each key value pair? TIA!
jest.mock('../../service/keyValue.service', () => ({
valueA: false,
valueB: false
}));
describe('PhotosService', () => {
let service: PhotosService;
let keyValueService: KeyValueService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PhotosService],
}).compile();
service = module.get<PhotosService>(PhotosService);
keyValueService.get.mockResolveValue(() => true);
});
it('should be defined', () => {
expect(service).toBeDefined();
valueA.mockResolveValue(() => true);
});
});
But the values doesnt change. I also tried the following,
it('should be defined', () => {
keyValueService.get.mockResolveValue(true);
keyValueService.get.mockResolveValue(true);
expect(service).toBeDefined();
valueA.mockResolveValue(() => true);
});
Alright one thing that worked for me was to set
keyValueService.get
again in mytest
orit
block tojest.fn()
again which worked for me to resolve this issue.Upto to use what you want. Either
mockImplementation
if need be ormockReturnValue
etc. My guess is that it just reassigns theget
function to a new mock value for that particularit/test
block.