简体   繁体   English

如何在electronJS中模拟需求

[英]How to mock require in electronJS

I'm trying to write unit test for my electron js application and I'm stuck at one place. 我正在尝试为我的electronic js应用程序编写单元测试,但被困在一个地方。

For example 例如

Lets say my app code is like below 可以说我的应用代码如下

var app= (function () {
    function app() {
        var _this = this;
        this.open = function (data) {
            var deferred = Q.defer();
            try {
                // some code 
            }
            catch (ex) {
                deferred.reject(ex);
            }
            return deferred.promise;
        };

    }
}());
exports.app = app

Now if I want to run it on electron client it will run perfectly fine as electron module is installed on client's PC 现在,如果我要在电子客户端上运行它,因为电子模块已安装在客户端的PC上,它将可以完美运行

The problem is when I'm trying to write unit test cases for the above as electron is not installed here on dev machine like below 问题是当我试图编写上述单元测试用例时,因为像下面这样在开发机上没有安装电子,

    import { app} from '../../app'
    import { Rights } from '../../Rights'
    describe('app', () => {
        let app: app;
        beforeEach(() => {
            app= new app();
        })

        it('should call open() and return error for null content', function (done) {
            let output = app.open(null);
            output
            .then((res) => { 
                 //expectation 
                 done(); 
            })
            .catch((err)=>{
                //expectation 
                done();
           })
        })
    })

Getting following error 出现以下错误

Error: Cannot find module 'electron'                                                                                                                                                      
    at Function.Module._resolveFilename (module.js:469:15)                                                                                                                                
    at Function.Module._load (module.js:417:25)                                                                                                                                           
    at Module.require (module.js:497:17)                                                                                                                                                  
    at require (internal/module.js:20:19)                                                                                                                                                 
    at Object.<anonymous> (<project pat>/app.js:2:16)                                                                    
    at Module._compile (module.js:570:32)                                                                                                                                                 
    at Object.Module._extensions..js (module.js:579:10)                                                                                                                                   
    at Module.load (module.js:487:32)                                                                                                                                                     
    at tryModuleLoad (module.js:446:12)                                                                                                                                                   
    at Function.Module._load (module.js:438:3)                                                                                                                                            
npm ERR! Test failed.  See above for more details.   

Question

  • How to mock require for any module which is not installed on dev PC but required for actual execution(installed on client's PC). 如何模拟需要开发PC上未安装但实际执行需要的任何模块(安装在客户端PC上)。

You can intercept require calls by overriding module._load : 您可以通过覆盖module._load截获require调用:

const m = require('module');
const originalLoader = m._load;
const stubs = { electron : {} };

m._load = function hookedLoader(request, parent, isMain) {
  const stub = stubs[request];
  return stub || originalLoader(request, parent, isMain);
};

I have had similar issue with mocha. 摩卡咖啡也有类似的问题。 My basic approach was to: * require electron in before hook, * override it with local default * require app * clean in after hook 我的基本方法是:*钩子之前需要输入电子,*本地默认值将其覆盖*要求应用程序*钩子之后清除

Here's a sample: 这是一个示例:

 var el = require('../../modules/electron'); describe('app', function () { 'use strict'; var el_override = { post: function () { }, get: function () { } }, app; before(function () { /* Since we already have electron required, we override it from node cache by: require.cache['/path/to/file/name'].exports = el_override */ // Require app // This will import overridden electron App = require('app-location'); }); beforeEach(function () { // init app app = new App(); }); after(function () { // Clean Up both electron override and app by // delete requre.cache['/path/to/file/names'] }); it('should test for batch', function () { // Call you functions }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM