gulp - "require not defined" when testing with proxyquireify stubs -
hello have project uses gulp build framework, , used karma jasmine testing.
i trying integrate proxyquireify mock requires, added proxyquireify browserify plugin in karma config, using karma-browserify.
but results in error when running tests, in first line, saying 'require undefined'.
what doing wrong?
here karma config
// karma configuration // generated on wed nov 26 2014 17:57:28 gmt+0530 (ist)
module.exports = function(config) { config.set({ // base path used resolve patterns (eg. files, exclude) basepath: '', // frameworks use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['browserify', 'jasmine'], // list of files / patterns load in browser files: [ './components/renderer/**/*.spec.js', './components/tracker/**/*.spec.js' ], // list of files exclude exclude: [ ], // preprocess matching files before serving them browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { './components/**/*.spec.js' : [ 'browserify' ] }, browserify: { debug:true, plugin: ['proxyquireify/plugin'] }, // test results reporter 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 output (reporters , 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 , executing tests whenever 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 tests , exits singlerun: false }); };
proxyquireify works internally substituting require function provided browserify.
in case seems new substituted require function not exposed global scope.
i went through code , found out proxyquireify creates new require function in node_modules/proxyquireify/lib/prelude.js named newrequire.
the issue having newrequire function not exposed in global scope require function, changed node_modules/proxyquireify/lib/prelude.js
// override current require new 1 return newrequire;
becomes
// override current require new 1 require = newrequire;
and newrequire exposed global scope , worked fine. since change reset every time npm install, created gulp task in case change every time before tests run, add gulp task reference
// task modify proxyquireify works properly, there bug in 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')); });
i run task before executing test tasks, this
// task run tests gulp.task('run:test', ['_patch:proxyquireify'], function() { //logic run tests };
i hope helps, thanks
Comments
Post a Comment