Mock.mockImplementation() not working

I had same problem as @Janos, the other answers didn’t help either. You could do two things :

  1. 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*/ })
    

     

  2. 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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)