How to mock a Node.js module loaded with dojo/node

我的未来我决定 提交于 2019-12-23 06:05:24

问题


I have an application with the server code running on Node.js and using Dojo. I have a config module defined like:

define([
    'dojo/node!nconf',
    'dojo/_base/config'
], function (nconf, dojoConfig) {
    nconf.argv().file({
        file: dojoConfig.baseDir + '/config.json'
    });
    console.log('-- file name:', dojoConfig.baseDir + '/config.json');
    console.log('-- context:', nconf.get('context'));
    // ... logic here ...
    return nconf.get(nconf.get('context'));
});

To be able to unit test this module, I've written two mocks: one for the nconf native module and one for dojoConfig. Here is the test:

define([
    'require',
    'intern!object',
    'intern/chai!assert'
], function (require, registerSuite, assert) {
    registerSuite({
        name: 'config utility',
        'load default settings': function () {
            require.undef('dojo/node!nconf');
            require.undef('dojo/_base/config');
            require({ map: {
                '*': {
                    'dojo/node!nconf': 'server/utils/tests/nconfMock',
                    'dojo/_base/config': 'server/utils/tests/dojoConfigMock'
                }
            }});
            require(['../config', './nconfMock'], this.async(1000).callback(
                function (config, nconfMock) {
                    assert.isNotNull(config);
                    assert.isNotNull(nconf);
                    // assert.deepEqual(config, nconfMock.contextSettings.test);
                }
            ));
        }
    });
});

I can see that my mock of dojoConfig is correctly loaded, but not the mock of the nconf module. During a webcast on Intern, Dylan mentioned that the mapping does not consider the plugin, that there's the way to force dojo/node module to load this nconfMock. Would you mind to give me more details?

Obviously, this is verbose, so if this continues to be a common request, we’ll probably do something to make it simpler in the future.

Important note: Without mapping dojo/node to intern/node_modules/dojo/node, the loading of my initial config module as defined above fails in the Intern environment. The mapping is done in the intern.js file. The reported error is:

Error: node plugin failed to load because environment is not Node.js
    at d:/git/fco2/src/libs/dojo/node.js:3:9
    at execModule (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:512:54)
    at d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:579:7
    at guardCheckComplete (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:563:4)
    at checkComplete (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:571:27)
    at onLoadCallback (d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:653:7)
    at d:\git\fco2\node_modules\intern\node_modules\dojo\dojo.js:758:5
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

Solution: As suggested by Colin Snover below, I now use Mockery. I also do NOT use the contextual require, only the default one. Here is a (simplified) solution working with the version 1.9.3 of the Dojo toolkit.

define([
    'intern!object',
    'intern/chai!assert',
    'intern/node_modules/dojo/node!mockery',
    './nconfMock'
], function (registerSuite, assert, mockery, nconfMock) {
    registerSuite({
        name: 'config utility',
        teardown: function () {
            mockery.disable();
            mockery.deregisterAll();
            require({ map: { '*': { 'dojo/_base/config': 'dojo/_base/config' } } });
            require.undef('dojo/_base/config');
            require.undef('server/utils/config');
        },
        'load default settings': function () {
            mockery.enable();
            mockery.registerMock('nconf', nconfMock);
            require({ map: { '*': { 'dojo/_base/config': 'server/utils/tests/dojoConfigMock' } } });
            require.undef('dojo/_base/config');
            require.undef('server/utils/config');
            require(
                ['server/utils/config'],
                this.async(1000).callback(function (config) {
                    assert.isNotNull(config);
                    assert.deepEqual(config, nconfMock.contextSettings.test);
                })
            );
        }
    });
});

Thanks, Dom


回答1:


In order to mock a Node.js dependency, you will probably want to simply use one of the various available projects for mocking Node.js modules. Mockery is a good choice since it’s stand-alone.

Since it looks like you’re using dojo/node and not the one from Intern, in your case, you’d do it like this:

define([
  'intern!object', 'dojo/node!mockery', 'dojo/Deferred', 'require'
], function (registerSuite, mockery, Deferred, require) {
  var moduleUsingMock;
  registerSuite({
    setup: function () {
      var dfd = new Deferred();
      mockery.enable();
      mockery.registerMock('module-to-mock', mockObject);

      require([ 'module-using-mock' ], function (value) {
        moduleUsingMock = value;
        dfd.resolve();
      });

      return dfd.promise;
    },
    teardown: function () {
      mockery.disable();
    },
    'some test': function () {
      moduleUsingMock.whatever();
      // ...
    }
  });
});


来源:https://stackoverflow.com/questions/22572008/how-to-mock-a-node-js-module-loaded-with-dojo-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!