javascript - Mocking a nested module in Node.js? - Stack Overflow

admin2025-04-18  0

I have these files:

File1.js

var mod1 = require('mod1');
mod1.someFunction()
...

File2.js

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

Now while writing unit tests for File2, is it possible to have mod1 mocked, so that I don't make calls to mod1.someFunction()?

I have these files:

File1.js

var mod1 = require('mod1');
mod1.someFunction()
...

File2.js

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

Now while writing unit tests for File2, is it possible to have mod1 mocked, so that I don't make calls to mod1.someFunction()?

Share Improve this question edited Jun 14, 2018 at 6:51 Jesse 3,6526 gold badges27 silver badges42 bronze badges asked Feb 1, 2016 at 19:40 arjunarjun 1031 silver badge10 bronze badges 4
  • 2 Yes it is possible. You should read something about dependency injection. And have a look at sinon.js. – crackmigg Commented Feb 1, 2016 at 19:44
  • Take a look at this tutorial on mocking: youtube./watch?v=fgqh-OZjpYY It shows a technique you can use just for this. And yeah, definitely check out sinon. – Tad Donaghe Commented Feb 1, 2016 at 19:48
  • how will sinon address this? – dm03514 Commented Feb 1, 2016 at 19:49
  • Sinon helps with creating stubs for the functions being created, so that it's easy to verify whether they were called correctly, return the correct values, etc. – arjun Commented Feb 4, 2016 at 3:01
Add a ment  | 

2 Answers 2

Reset to default 5

I'm usually using mockery module like following:

lib/file1.js

var mod1 = require('./mod1');
mod1.someFunction();

lib/file2.js

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

lib/mod1.js

module.exports.someFunction = function() {
  console.log('hello from mod1');
};

test/file1.js

/* globals describe, before, beforeEach, after, afterEach, it */

'use strict';

//var chai = require('chai');
//var assert = chai.assert;
//var expect = chai.expect;
//var should = chai.should();

var mockery = require('mockery');

describe('config-dir-all', function () {

  before('before', function () {
    // Mocking the mod1 module
    var mod1Mock = {
      someFunction: function() {
        console.log('hello from mocked function');
      }
    };

    // replace the module with mock for any `require`
    mockery.registerMock('mod1', mod1Mock);

    // set additional parameters
    mockery.enable({
      useCleanCache:      true,
      //warnOnReplace:      false,
      warnOnUnregistered: false
    });
  });

  beforeEach('before', function () {

  });

  afterEach('after', function () {

  });

  after('after', function () {
    // Cleanup mockery
    after(function() {
      mockery.disable();
      mockery.deregisterMock('mod1');
    });
  });

  it('should throw if directory does not exists', function () {

    // Now File2 will use mock object instead of real mod1 module
    var file2 = require('../lib/file2');

  });

});

As it was suggested before, sinon module is very convenient to build the mocks.

Absolutely. There are 2 very popular node.js libraries exclusively for the purpose of mocking requires.

https://github./jhnns/rewire

https://github./mfncooper/mockery

Both of them have different API, and rewire has some strange caveats

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744965243a277157.html

最新回复(0)