Haris Khan Haris Khan - 1 year ago 256
React JSX Question

spyOn a function not working - jasmine

import * as PlanReducer from '../../../reducers/planReducer';

describe('currentPlanReducer()', () => {

beforeAll(() => {

spyOn(PlanReducer, 'planReducer').and.callThrough();


PlanRedcuer is :

export default function planReducer(state = INITIAL_STATE, action) {....}

It throws me an error during test that planRedcuer is not a method, I tried to import other method like that (which was not export default) and it worked. Can any one help me with how I can spy on the function and test it?

Answer Source

You're not exporting the planReducer function as planReducer, but as default. Therefore, in the test you should spy on the default property:

spyOn(PlanReducer, 'default').and.callThrough();

Alternatively, you can export the planReducer as both default and planReducer:

const planReducer = function planReducer(state = INITIAL_STATE, action) {...}
export {planReducer}
export default planReducer
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download