7

The tested file uses a function that is imported from another file

import {myFunc} from './myFile'

How can I mock return value for this function in my tests for this file? I'm using jest.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Anna
  • 2,911
  • 6
  • 29
  • 42
  • 1
    Possible duplicate of [How can I mock an ES6 module import using Jest?](https://stackoverflow.com/questions/40465047/how-can-i-mock-an-es6-module-import-using-jest) – Agney Apr 30 '19 at 12:48

1 Answers1

9

This is what worked for me, I'm not sure if it's a good practice tho:

 import * as myFile from "./myFile";
 jest.mock("./myFile");
 myFile.myFunc = jest.fn().mockImplementation(() => {
     return "Some Value";
 });
Anna
  • 2,911
  • 6
  • 29
  • 42