Xiaohe Dong
Xiaohe Dong

Reputation: 5023

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
  });

});

Upvotes: 13

Views: 20031

Answers (2)

Henrik Andersson
Henrik Andersson

Reputation: 47222

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                                                                                                                                                                                                
    });                                                                                                                                                                                                                                      
}); 

Upvotes: 5

CaptainCasey
CaptainCasey

Reputation: 1371

The line

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

gets evaluated in the describe and stored in foo.

Afterwards, in the it block, you're mocking that out, but this isn't applying to the old reference foo.

Putting foo after the mockImplementation call will fix the error.

//Test code

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


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(foo.getResult()); // undefined
    console.log(reference.result()); // 123
  });

});

Upvotes: 5

Related Questions