Javascript 获得';未定义require';当用因果报应的angularjs进行测试时

Javascript 获得';未定义require';当用因果报应的angularjs进行测试时,javascript,angularjs,karma-runner,browserify,Javascript,Angularjs,Karma Runner,Browserify,我正在使用angulajs和browersify构建一个应用程序。为了测试它,我想用因果报应。 我将conf文件设置为: module.exports = function(config) { config.set({ basePath: '', frameworks: ['jasmine', 'browserify'], files: [ 'node_modules/angular/angular.js', 'node_modules/

我正在使用angulajs和browersify构建一个应用程序。为了测试它,我想用因果报应。 我将conf文件设置为:

module.exports = function(config) {
  config.set({

    basePath: '',

    frameworks: ['jasmine', 'browserify'],

    files: [
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js',
      'src/app/*',
      'src/app/*/*'
    ],

    exclude: [
      'src/app/*/*.jade'
    ],

    reporters: ['progress'],

    port: 9876,

    colors: true,
    logLevel: config.LOG_INFO,

    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,

    browserify: {
      debug: true
    },

    preprocessors: {'src/app/*/*.js': ['browserify']}
  });
};
    require('angular')
require('angular-mocks')

var uiRouter          = require('angular-ui-router')
var serices    = require('./services')
var directives  = require('./directives')
var controllers = require('./controllers')
var routes            = require('./routes')

angular.module('myApp', [uiRouter, 'ngMocks'])
  // load Routes
  .config(routes)

  // Services
  .service('someService', services.someService)

  // Controllers
  .controller('myCtrl', controllers.myCtrl)

  // Directives
  .directive('myDirective', directives.myDirective);
我的app.js文件如下所示:

module.exports = function(config) {
  config.set({

    basePath: '',

    frameworks: ['jasmine', 'browserify'],

    files: [
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js',
      'src/app/*',
      'src/app/*/*'
    ],

    exclude: [
      'src/app/*/*.jade'
    ],

    reporters: ['progress'],

    port: 9876,

    colors: true,
    logLevel: config.LOG_INFO,

    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,

    browserify: {
      debug: true
    },

    preprocessors: {'src/app/*/*.js': ['browserify']}
  });
};
    require('angular')
require('angular-mocks')

var uiRouter          = require('angular-ui-router')
var serices    = require('./services')
var directives  = require('./directives')
var controllers = require('./controllers')
var routes            = require('./routes')

angular.module('myApp', [uiRouter, 'ngMocks'])
  // load Routes
  .config(routes)

  // Services
  .service('someService', services.someService)

  // Controllers
  .controller('myCtrl', controllers.myCtrl)

  // Directives
  .directive('myDirective', directives.myDirective);
我正在使用
karma browserify
,但在运行测试时仍然出现以下错误:

'require is not defined'

如何修复此问题?

您需要在单元测试文件上运行browserify并将其包含在文件中。我在这里包括bundled.js,这是一个浏览器化的应用程序代码

这一个离你的很近,很有效

(function() {
    'use strict';
    // Karma configuration

    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: ['mocha', 'chai-as-promised', 'chai', 'browserify'],

            // list of files / patterns to load in the browser
            files: [
                // app-specific code. This should be generated by gulp browserify. Includes Angular
                'webapp/bundled.js',
                // 3rd-party resources
                'node_modules/angular-mocks/angular-mocks.js',
                // test files
                'unit/**/*.js'
            ],
            // list of files to exclude
            exclude: ['karma.conf.js', 'protractor-conf.js'],
            // Browserify config
            browserify: {
                watch: true,
                debug: true
            },
            // preprocess matching files before serving them to the browser
            // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
            preprocessors: {
                'unit/**/*.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

            // - IE
            // - Chrome
            // - ChromeCanary
            // - Firefox
            // - Opera
            // - Safari
            // - PhantomJS
            browsers: ['Chrome', 'IE', 'Firefox'],

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

你的业力文件中可能有你的地球模式错误。我想您可能需要src/app/*.js和src/app/***.js之类的东西