lundi 22 mars 2021

Jest spyOn not works

So I have a file that consists of a couple of functions. And I wrote a test for getOpeningHours.

  • The getOpeningHours use isLocationOrderAheadAvailable function
  • The isLocationOrderAheadAvailable function is not using getOpeningHours so it's not a circular dep issue
  • Both getOpeningHours and isLocationOrderAheadAvailable are exported functions
  • Both functions came from same file.
import * as locationUtils from './location';
import {
    isLocationOrderAheadAvailable,
    getProductItemsFromMenu,
    resolveOpeningHours,
    getOpenedTimeRanges,
    getOpeningHours,
} from './location';


describe('getOpeningHours', () => {
        it('should return same locationHours if isLocationOrderAheadAvailable is false', () => {
            jest.spyOn(locationUtils, 'isLocationOrderAheadAvailable').mockImplementation(
                jest.fn().mockReturnValue(false)
            );

            const openingHours = getOpeningHours(validLocationEntry);
            expect(openingHours).toEqual(locationHours);
        });
});

So eventually the isLocationOrderAheadAvailable not been mocked at all. The wired thing is that I did exactly the same in a different test and it worked.

Sorry, I can't give a working example of this.

Aucun commentaire:

Enregistrer un commentaire