2

testing react and typescript with jest and enzyme, how would I mock this expectsVideo function such that I can define if it returns true of false based on my test case scenerio

jest.mock('utils/media-utils', () => ({
  expectsVideo: () => true,
  myOtherFunctions: jest.fn()
}));

the utils/media-utils if a file that is being imported in the react component that I am testing. If I try to reference object from outside it gives me error stating cannot use out of scope variables. Tried various implementation i came across different blogs but no help.

1 Answer 1

5

mockImplementation should help while mocking whole module:

import * as mediaUtils from 'utils/media-utils';

mock('utils/media-utils'); // automocking whole module

it('....1...', () => {
    mediaUtils.expectsVideo.mockImplementation(() => true);
});

it('....2...', () => {
    mediaUtils.expectsVideo.mockImplementation(() => false);
});

BTW as for referencing variables from outer that returns error(from section for jest.doMock documentation):

When using babel-jest, calls to mock will automatically be hoisted to the top of the code block. Use this method if you want to explicitly avoid this behavior.

So that's why all const and let are not even defined at point when module is mocked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.