"require not defined" when testing with proxyquireify stubs

2.7k Views Asked by At

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
  });
};
1

There are 1 best solutions below

1
Jiby Jose On BEST ANSWER

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