1

This is my first time attempting to unit test some code and I'm getting this error when using Karma and Jasmine:

PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
ReferenceError: Can't find variable: require
at unit/tests.js:1

I tried npm install karma-browserify --save-dev but that didn't solve the issue.

Any idea how to sort this?

My Karma conf file:

// Karma configuration
// Generated on Tue Nov 08 2016 03:14:50 GMT+0000 (GMT Standard Time)

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: ['jasmine'],


// list of files / patterns to load in the browser
files: [
   // '../shopping-basket.js',
    'unit/*.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: {
     //'test/**/*.js': ['browserify']
},


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


// 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: true,


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


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

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
 })
}

tests.js

var myCode = require('./shopping-basket-functions');

describe('tests', function(){
    describe('testFunction', function(){
        it('should return 1', function(){
            // Call the exported function from the module
            myCode.testFunction().should.equal(1);
        })
    })
})

shopping-basket-functions.js

function testFunction () {
    return 1;
}

// If we're running under Node, 
if(typeof exports !== 'undefined') {
    exports.testFunction = testFunction;
}
user1532669
  • 2,288
  • 4
  • 36
  • 72

2 Answers2

0

Have you tried using the template here: https://www.npmjs.com/package/grunt-template-jasmine-nml as specified in this answer?

Long story short, what it does is allow you to use the CommonJS syntax that NodeJS uses in order to reference and pull in other modules. See an example below of how you would reference it in your jasmine config file (again, per the other user's answer) :

jasmine: {
    options: {
        template: require('grunt-template-jasmine-nml'),
        helpers: 'spec/helpers/**/*.js',
        specs: 'spec/**/*.spec.js'
    }
}
Community
  • 1
  • 1
prestonsmith
  • 758
  • 1
  • 9
  • 29
0

had same issue trying to build an app, phanotmjs kept on creeping the undefined variable require, solved it by: npm install grunt-template-jasmine-nml --save-dev

this will update the right template and it works after. trying: grunt test -dd finished without error

Samir Ouldsaadi
  • 1,404
  • 2
  • 9
  • 3