使用proxyquireify存根进行测试时,“require not defined”

时间:2014-11-27 11:52:59

标签: gulp karma-runner browserify karma-jasmine proxyquire

你好我有一个项目,它使用gulp作为构建框架,并使用karma和jasmine进行测试。

我正在尝试将proxyquireify集成到模拟需求中,我只是在karma配置中添加了proxyquireify作为browserify插件,因为我正在使用karma-browserify。

但是这会在运行测试时导致错误,在第一行中说'require is undefined'。

我做错了什么?

这是我的业力配置

// Karma配置     //生成于2014年11月26日星期三17:57:28 GMT + 0530(IST)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['browserify', 'jasmine'],


    // list of files / patterns to load in the browser
    files: [
      './components/renderer/**/*.spec.js',
      './components/tracker/**/*.spec.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        './components/**/*.spec.js'   : [ 'browserify' ]
    },

    browserify: {
        debug:true,
        plugin: ['proxyquireify/plugin']
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['spec'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

1 个答案:

答案 0 :(得分:1)

proxyquireify通过替换browserify提供的require函数在内部工作。

在这种情况下,似乎新的替代需求函数未暴露于全局范围。

我查看了代码,发现proxyquireify在node_modules / proxyquireify / lib / prelude.js中创建了名为newRequire的新require函数。

我遇到的问题是newRequire函数没有作为require函数在全局范围中公开,所以我更改了node_modules / proxyquireify / lib / prelude.js以便

// Override the current require with this new one
return newRequire;

成为

// Override the current require with this new one
require = newRequire;

并且newRequire正确地暴露于全局范围,一切正常。由于每次进行npm安装时都会重置此更改,因此我在我的情况下创建了一个gulp任务,每次运行测试之前都会执行此更改,我将添加gulp任务以供参考

// Task to modify proxyquireify so that it works properly, there is a bug in the npm library
gulp.task('_patch:proxyquireify', function() {
  gulp.src(['./node_modules/proxyquireify/lib/prelude.js'])
   .pipe(replace(/return newRequire;/g, 'require = newRequire;'))
   .pipe(gulp.dest('./node_modules/proxyquireify/lib'));
});

我在执行测试任务之前运行此任务,比如

// Task to run tests
gulp.task('run:test', ['_patch:proxyquireify'], function() {
  //logic to run tests
};

我希望这有帮助,谢谢