Jasmine Tests give error “Uncaught ReferenceError:

2019-05-04 06:09发布

I am trying to run Jasmine tests with Karma on my React site. My tests were working before, and I'm not sure what has changed, but now I get the error:

Uncaught ReferenceError: require is not defined

for Chrome and PhantomJS and Firefox give me similar errors. Please let me know if more info would be helpful. I have found a lot of similar questions on the web, but nothing that fixes the problem.

You can see the test file below and the full project is on my github repo.

Thanks in advance!

My test file looks like this:

var React = require('react/addons');
var Story = require('../../app/js/components/story.jsx');
var TestUtils = React.addons.TestUtils;
var testUtilsAdditions = require('react-testutils-additions');

  describe('Story component', function () {
    var component;

    beforeEach(function () {
      var element = React.createElement(Story);
      element.props.data = {
        storyTitle: 'front end test title',
        author : 'front end author',
        storyText : 'front end story text'
      };
      component = TestUtils.renderIntoDocument(element);
      });

    it('should display a story', function () {
      expect(component.props.data).toBeDefined();
      expect(component.props.data.storyTitle).toBeDefined();
      expect(component.props.data.storyTitle).toBe('front end test title');
      expect(component.props.data.author).toBe('front end author');
      expect(component.props.data.storyText).toBe('front end story text');
    });

  });

My karma.conf.js file looks like this:

// Karma configuration
// Generated on Thu Jul 02 2015 15:56:34 GMT-0700 (PDT)

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: [
    './node_modules/phantomjs-polyfill/bind-polyfill.js',
        'test/karma_tests/*test.js'
    ],

    //plugins to start browsers
    plugins : [
    'karma-junit-reporter',
    'karma-phantomjs-launcher',
    'karma-chrome-launcher',
    'karma-firefox-launcher',
    'karma-opera-launcher',
    'karma-ie-launcher',
    'karma-jasmine',
    'karma-chai',
    'karma-webpack'
    ],


    // 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/karma_tests/*test.js': ['webpack'],
        // 'test/**/*_test.js': ['webpack']
    },

    webpack: {
            // karma watches the test entry points
            // (you don't need to specify the entry option)
            // webpack watches dependencies

            // webpack configuration
        module: {
          loaders: [{
            test: /\.jsx$/,
            loader:'jsx-loader'
          }]
        }
        },


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


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


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

This is the error I get after making the changes @guilhebl recommended:

Firefox 40.0.0 (Ubuntu 0.0.0) ERROR
  Error: Module name "react/addons" has not been loaded yet for context: _. Use require([])
  http://requirejs.org/docs/errors.html#notloaded
  at /home/michael/repository/short-stories/node_modules/requirejs/require.js:165

Here is the karma.conf.js after I made the changes:

// Karma configuration
// Generated on Thu Jul 02 2015 15:56:34 GMT-0700 (PDT)

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: [
    './node_modules/phantomjs-polyfill/bind-polyfill.js',
    './node_modules/requirejs/require.js',
    './node_modules/karma-requirejs/lib/adapter.js',
    './test/karma_tests/*test.js'
    ],

    //plugins to start browsers
    plugins : [
    'karma-junit-reporter',
    'karma-phantomjs-launcher',
    'karma-chrome-launcher',
    'karma-firefox-launcher',
    'karma-opera-launcher',
    'karma-ie-launcher',
    'karma-jasmine',
    'karma-chai',
    'karma-webpack',
    'karma-requirejs',
    'karma-script-launcher'
    ],


    // 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/karma_tests/*test.js': ['webpack'],
        // 'test/**/*_test.js': ['webpack']
    },

    webpack: {
            // karma watches the test entry points
            // (you don't need to specify the entry option)
            // webpack watches dependencies

            // webpack configuration
        module: {
          loaders: [{
            test: /\.jsx$/,
            loader:'jsx-loader'
          }]
        }
        },


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


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


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

1条回答
趁早两清
2楼-- · 2019-05-04 06:14

You should have require configured in your karma.conf file such as example:

module.exports = function(config){
  config.set({
    frameworks: ['jasmine'],

    plugins : [               
               'karma-chrome-launcher',
               'karma-firefox-launcher',                            
               'karma-script-launcher',
               'karma-phantomjs-launcher',
               'karma-jasmine',
               'karma-requirejs'               
    ],

    files : [          
      'node_modules/requirejs/require.js',
      'node_modules/karma-requirejs/lib/adapter.js',      
      'app/js/test-main.js',

      {pattern: 'app/lib/**/*.js', included: false},
      {pattern: 'app/js/**/*.js', included: false},      
      {pattern: 'app/js/views/**/*Test.js', included: false}      
    ],

    browsers: ['PhantomJS']        
  });
};
查看更多
登录 后发表回答