Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest mock implementation is not working with require('')

I want to test one JS which is using one 3rd lib to fetch data, so I am using jest mock that implementation. It is working when I call it directly in the test. However, it is not working when it is used in source code.

Here is the code

//Source implementation

var reference = require('./reference');

module.exports = {
  getResult: function() {
    return reference.result();
  }
};


//Test code

jest.dontMock('./foo');
jest.dontMock('console');

describe('descirbe', function() {
  var foo = require('./foo');

  it('should ', function() {
    var reference = require('./reference');

    reference.result.mockImplementation(function (a, b, c) {
      return '123'
    });

    console.log(foo.getResult()); // undefined
    console.log(reference.result()); // 123
  });

});
like image 275
Xiaohe Dong Avatar asked Sep 07 '15 04:09

Xiaohe Dong


1 Answers

Your order of requires are wrong. When you require ./foo before setting up your mock reference then foos reference will be undefined as per Jest automocking.

jest.dontMock('./foo');                                                                                                                                                                                                                

describe('describe', function() {                                                                                                                                                                                                          
    it('should ', function () {                                                                                                                                                                                                            
        var reference = require('./reference');                                                                                                                                                                                               
        reference.result.mockImplementation(function (a, b, c) {
            return '123';
        });                                                                                                                                                                                               
        var foo = require('./foo');                                                                                                                                                                                                        

       console.log('ferr', foo.getResult());  // ferr 123                                                                                                                                                                                                
    });                                                                                                                                                                                                                                      
}); 
like image 99
Henrik Andersson Avatar answered Sep 27 '22 21:09

Henrik Andersson