I had same problem as @Janos, the other answers didn’t help either. You could do two things :
-
If you need to mock only a function from Service, in your test file:
import service from './Service'; jest.mock('./Service', () => jest.fn()); service.yourFunction = jest.fn(() => { /*your mock*/ })
-
If you need to mock the entire Module:
Say your service.js is in javascript/utils, create a javascript/utils/_mocks_ and inside it create a service.js file, you can then mock the entire class in this file, eg:
const myObj = {foo: "bar"} const myFunction1 = jest.fn(() => { return Promise.resolve(myObj) }) const myFunction2 = ... module.exports = { myFunction1, myFunction2 }
then in your test file you just add:
jest.mock('./javascript/utils/service')
…functions exported from the mockfile will be then hit through your test file execution.