1

Hello i have a project that uses gulp for the build framework, and used karma with jasmine for the testing.

I am trying to integrate proxyquireify to mock the requires, i just added proxyquireify as browserify plugin in karma config, as i am using karma-browserify.

But this results in an error when running the tests, in the first line, saying 'require is undefined'.

What am i doing wrong?

here is my karma config

// Karma configuration // Generated on Wed Nov 26 2014 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
  });
};
Jiby Jose
  • 3,745
  • 4
  • 24
  • 32
  • it seems, proxyquireify replaces the require function, but doesnt set the function on window, i added require = to the function defined in lib/prelude.js and it seems everything works, is this a bug? – Jiby Jose Nov 28 '14 at 05:20
  • Can you elaborate on how you solved it? – Eli White Dec 10 '14 at 08:53
  • 1
    @EliWhite OK will add the solution i implemented as an answer if that would be useful for you. – Jiby Jose Dec 10 '14 at 20:13

1 Answers1

1

proxyquireify works internally by substituting the require function provided by browserify.

In this case it seems the new substituted require function was not exposed to global scope.

I went through the code and found out proxyquireify creates the new require function in node_modules/proxyquireify/lib/prelude.js named as newRequire.

the issue i was having was that the newRequire function was not exposed in the global scope as the require function, so i changed node_modules/proxyquireify/lib/prelude.js so that

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

becomes

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

and the newRequire was properly exposed to global scope and everything worked fine. Since this change is reset every time i do a npm install, i created a gulp task in my case which does this change every time before tests are run, i will add the gulp task for reference

// 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'));
});

I run this task before executing the test tasks, like this

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

I hope this helps, thanks

Jiby Jose
  • 3,745
  • 4
  • 24
  • 32
  • 3
    This solution causes [other problems](https://github.com/thlorenz/proxyquireify/issues/21#issuecomment-69747496) and the `require not defined` issue was fixed properly with proxyquireify v1.2. – Thorsten Lorenz Jan 13 '15 at 15:32