Haris Khan
Haris Khan

Reputation: 389

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?

Upvotes: 1

Views: 744

Answers (2)

Michał Perłakowski
Michał Perłakowski

Reputation: 92501

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

Upvotes: 1

Sean Sobey
Sean Sobey

Reputation: 972

Try this: import PlanReducer from '../../../reducers/planReducer';

If you look at the resulting javascript in your planReducer you will see that 'export default function planReducer' results in 'exports.default = planReducer;'

And in the resulting javascript in the test/spec import * as PlanReducer from '../../../reducers/planReducer'; results in spyOn(planReducer, 'planReducer').and.callThrough();

But using import PlanReducer from '../../../reducers/planReducer'; results in spyOn(app_1.default, 'planReducer').and.callThrough(); which references the default export.

Upvotes: 0

Related Questions