From 27d8eb5e70351d17a5ff2c824bb71a4c1a88ee42 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Fri, 10 Jun 2016 07:20:13 +0200 Subject: [PATCH] improvement: add mocks helper module for test env no issue - add methods to mock/unmock not existent files --- core/test/utils/index.js | 3 +++ core/test/utils/mocks/index.js | 1 + core/test/utils/mocks/utils.js | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 core/test/utils/mocks/index.js create mode 100644 core/test/utils/mocks/utils.js diff --git a/core/test/utils/index.js b/core/test/utils/index.js index afa3d2c0a0..06b491d781 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -14,6 +14,7 @@ var Promise = require('bluebird'), filterData = require('./fixtures/filter-param'), API = require('./api'), fork = require('./fork'), + mocks = require('./mocks'), config = require('../../server/config'), fixtures, @@ -596,6 +597,8 @@ module.exports = { initData: initData, clearData: clearData, + mocks: mocks, + fixtures: fixtures, DataGenerator: DataGenerator, diff --git a/core/test/utils/mocks/index.js b/core/test/utils/mocks/index.js new file mode 100644 index 0000000000..263a39b459 --- /dev/null +++ b/core/test/utils/mocks/index.js @@ -0,0 +1 @@ +exports.utils = require('./utils'); diff --git a/core/test/utils/mocks/utils.js b/core/test/utils/mocks/utils.js new file mode 100644 index 0000000000..26e6eb14e7 --- /dev/null +++ b/core/test/utils/mocks/utils.js @@ -0,0 +1,20 @@ +var Module = require('module'), + originalRequireFn = Module.prototype.require; + +/** + * helper fn to mock non existent modules + * mocks.utils.mockNotExistingModule(/pattern/, mockedModule) + */ +exports.mockNotExistingModule = function mockNotExistingModule(modulePath, module) { + Module.prototype.require = function (path) { + if (path.match(modulePath)) { + return module; + } + + return originalRequireFn.apply(this, arguments); + }; +}; + +exports.unmockNotExistingModule = function unmockNotExistingModule() { + Module.prototype.require = originalRequireFn; +};